Skip to content

Commit

Permalink
fixup! ✨(front) add components needed for uploading thumbnail
Browse files Browse the repository at this point in the history
  • Loading branch information
roro-lv committed Jun 13, 2022
1 parent f9746a2 commit 95940a1
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,32 +1,11 @@
import { Box, Text } from 'grommet';
import React from 'react';
import { defineMessages, useIntl } from 'react-intl';
import { useIntl } from 'react-intl';

import { ThumbnailDisplayer } from 'components/DashboardVideoLiveControlPane/widgets/DashboardVideoLiveWidgetThumbnail/ThumbnailDisplayer';
import {
UploadManagerState,
UploadManagerStatus,
} from 'components/UploadManager';
import { Thumbnail, uploadState } from 'types/tracks';

const messages = defineMessages({
thumbnailError: {
defaultMessage:
'An error happened while uploading your thumbnail. Please retry.',
description: 'A text displayed when there is no uploaded thumbnail.',
id: 'components.ThumbnailManager.thumbnailError',
},
thumbnailUploaded: {
defaultMessage: 'Your image is being uploaded.',
description: 'A text displayed when the thumbnail is being uploaded.',
id: 'components.ThumbnailManager.thumbnailUploaded',
},
thumbnailProcessed: {
defaultMessage: 'Your image is being processed.',
description: 'A text displayed when the thumbnail is being processed.',
id: 'components.ThumbnailManager.thumbnailProcessed',
},
});
import { UploadManagerState } from 'components/UploadManager';
import { Thumbnail } from 'types/tracks';
import { determineMessage } from 'components/DashboardVideoLiveControlPane/widgets/DashboardVideoLiveWidgetThumbnail/utils/utils';

interface ThumbnailManagerProps {
thumbnail: Thumbnail;
Expand All @@ -39,34 +18,19 @@ export const ThumbnailManager = ({
}: ThumbnailManagerProps) => {
const intl = useIntl();

let textMessage = null;

if (thumbnail.upload_state === uploadState.ERROR) {
textMessage = intl.formatMessage(messages.thumbnailError);
} else if (
(thumbnail.upload_state === uploadState.PENDING &&
uploadManagerState[thumbnail.id]?.status ===
UploadManagerStatus.UPLOADING) ||
uploadManagerState[thumbnail.id]?.status === UploadManagerStatus.INIT
) {
textMessage = intl.formatMessage(messages.thumbnailUploaded);
} else if (
thumbnail.upload_state === uploadState.PROCESSING ||
(thumbnail.upload_state === uploadState.PENDING &&
uploadManagerState[thumbnail.id]?.status === UploadManagerStatus.SUCCESS)
) {
textMessage = intl.formatMessage(messages.thumbnailProcessed);
}

if (textMessage) {
return (
<Box border={{ color: 'blue-active' }} pad="small" round="xsmall">
<Text alignSelf="center" color="blue-active" size="0.875rem">
{textMessage}
</Text>
</Box>
);
}

return <ThumbnailDisplayer rounded urlsThumbnail={thumbnail.urls} />;
const messageToDisplay = determineMessage(thumbnail, uploadManagerState);

return (
<React.Fragment>
{messageToDisplay ? (
<Box border={{ color: 'blue-active' }} pad="small" round="xsmall">
<Text alignSelf="center" color="blue-active" size="0.875rem">
{intl.formatMessage(messageToDisplay)}
</Text>
</Box>
) : (
<ThumbnailDisplayer rounded urlsThumbnail={thumbnail.urls} />
)}
</React.Fragment>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { defineMessages } from 'react-intl';

import { UploadManagerStatus } from 'components/UploadManager';
import { modelName } from 'types/models';
import { uploadState } from 'types/tracks';
import { thumbnailMockFactory } from 'utils/tests/factories';
import { determineMessage } from './utils';

jest.mock('data/appData', () => ({
appData: {
jwt: 'some token',
},
}));

const genericThumbnail = thumbnailMockFactory();
const genericUploadManagerState = {
[genericThumbnail.id]: {
file: new File(['(⌐□_□)'], 'genericFile.png'),
objectId: genericThumbnail.id,
objectType: modelName.THUMBNAILS,
progress: 50,
status: UploadManagerStatus.UPLOADING,
},
};

const messages = defineMessages({
thumbnailError: {
defaultMessage:
'An error happened while uploading your thumbnail. Please retry.',
description: 'A text displayed when there is no uploaded thumbnail.',
id: 'components.ThumbnailManager.thumbnailError',
},
thumbnailUploaded: {
defaultMessage: 'Your image is being uploaded.',
description: 'A text displayed when the thumbnail is being uploaded.',
id: 'components.ThumbnailManager.thumbnailUploaded',
},
thumbnailProcessed: {
defaultMessage: 'Your image is being processed.',
description: 'A text displayed when the thumbnail is being processed.',
id: 'components.ThumbnailManager.thumbnailProcessed',
},
});

describe('DashboardVideoLiveWidgetThumbnail/utils', () => {
it('determineMessage when thumbnail upload has an error', () => {
const thumbnailInError = {
...genericThumbnail,
upload_state: uploadState.ERROR,
};

expect(
determineMessage(thumbnailInError, genericUploadManagerState),
).toEqual(messages.thumbnailError);
});

it('determineMessage when thumbnail is being uploaded', () => {
const uploadingThumbnail = {
...genericThumbnail,
upload_state: uploadState.PENDING,
};
const initializatingUploadManagerState = {
[genericThumbnail.id]: {
...genericUploadManagerState[genericThumbnail.id],
status: UploadManagerStatus.INIT,
},
};

expect(
determineMessage(uploadingThumbnail, genericUploadManagerState),
).toEqual(messages.thumbnailUploaded);

expect(
determineMessage(uploadingThumbnail, initializatingUploadManagerState),
).toEqual(messages.thumbnailUploaded);
});

it('determineMessage when thumbnail is being processed', () => {
const uploadingThumbnail = {
...genericThumbnail,
upload_state: uploadState.PENDING,
};
const processedThumbnail = {
...genericThumbnail,
upload_state: uploadState.PROCESSING,
};
const successfulUploadManagerState = {
[genericThumbnail.id]: {
...genericUploadManagerState[genericThumbnail.id],
status: UploadManagerStatus.SUCCESS,
},
};

expect(
determineMessage(processedThumbnail, genericUploadManagerState),
).toEqual(messages.thumbnailProcessed);

expect(
determineMessage(uploadingThumbnail, successfulUploadManagerState),
).toEqual(messages.thumbnailProcessed);
});

it('determineMessage when there is no message to return', () => {
expect(
determineMessage(genericThumbnail, genericUploadManagerState),
).toEqual(null);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { defineMessages, MessageDescriptor } from 'react-intl';

import {
UploadManagerState,
UploadManagerStatus,
} from 'components/UploadManager';
import { Thumbnail, uploadState } from 'types/tracks';
import { Nullable } from 'utils/types';

const messages = defineMessages({
thumbnailError: {
defaultMessage:
'An error happened while uploading your thumbnail. Please retry.',
description: 'A text displayed when there is no uploaded thumbnail.',
id: 'components.ThumbnailManager.thumbnailError',
},
thumbnailUploaded: {
defaultMessage: 'Your image is being uploaded.',
description: 'A text displayed when the thumbnail is being uploaded.',
id: 'components.ThumbnailManager.thumbnailUploaded',
},
thumbnailProcessed: {
defaultMessage: 'Your image is being processed.',
description: 'A text displayed when the thumbnail is being processed.',
id: 'components.ThumbnailManager.thumbnailProcessed',
},
});

export const determineMessage = (
thumbnail: Thumbnail,
uploadManagerState: UploadManagerState,
): Nullable<MessageDescriptor> => {
if (thumbnail.upload_state === uploadState.ERROR) {
return messages.thumbnailError;
}
if (
(thumbnail.upload_state === uploadState.PENDING &&
uploadManagerState[thumbnail.id]?.status ===
UploadManagerStatus.UPLOADING) ||
uploadManagerState[thumbnail.id]?.status === UploadManagerStatus.INIT
) {
return messages.thumbnailUploaded;
}
if (
thumbnail.upload_state === uploadState.PROCESSING ||
(thumbnail.upload_state === uploadState.PENDING &&
uploadManagerState[thumbnail.id]?.status === UploadManagerStatus.SUCCESS)
) {
return messages.thumbnailProcessed;
}
return null;
};

0 comments on commit 95940a1

Please sign in to comment.