diff --git a/src/frontend/apps/lti_site/components/LTIRoutes/index.tsx b/src/frontend/apps/lti_site/components/LTIRoutes/index.tsx index daae8f99ad..26350cdeb6 100644 --- a/src/frontend/apps/lti_site/components/LTIRoutes/index.tsx +++ b/src/frontend/apps/lti_site/components/LTIRoutes/index.tsx @@ -8,7 +8,6 @@ import { UploadForm, UploadHandlers, UploadManager, - UploadingObject, WithParams, builderFullScreenErrorRoute, modelName, @@ -131,12 +130,11 @@ export const LTIInnerRoutes = () => { path={UPLOAD_FORM_ROUTE.default} element={ - {({ objectId, objectType, parentType, parentId }) => + {({ objectId, objectType, parentId }) => objectId && objectType ? ( ) : ( diff --git a/src/frontend/packages/lib_components/src/common/UploadField/index.tsx b/src/frontend/packages/lib_components/src/common/UploadField/index.tsx index 8cbaee12ca..1b8de0be98 100644 --- a/src/frontend/packages/lib_components/src/common/UploadField/index.tsx +++ b/src/frontend/packages/lib_components/src/common/UploadField/index.tsx @@ -4,10 +4,7 @@ import Dropzone from 'react-dropzone'; import { defineMessages, useIntl } from 'react-intl'; import styled from 'styled-components'; -import { - UploadingObject, - useUploadManager, -} from '@lib-components/common/UploadManager'; +import { useUploadManager } from '@lib-components/common/UploadManager'; import { uploadableModelName } from '@lib-components/types/models'; import { DropzonePlaceholder } from './DropzonePlaceholder'; @@ -28,14 +25,12 @@ const DropzoneStyled = styled.div` export interface UploadFieldProps { objectType: uploadableModelName; objectId: string; - parentType?: Maybe; parentId?: Maybe; } export const UploadField = ({ objectType, objectId, - parentType, parentId, }: UploadFieldProps) => { const { addUpload } = useUploadManager(); @@ -44,7 +39,7 @@ export const UploadField = ({ const onDrop = (files: File[]) => { setFile(files[0]); - addUpload(objectType, objectId, files[0], parentType, parentId); + addUpload(objectType, objectId, files[0], parentId); }; return ( diff --git a/src/frontend/packages/lib_components/src/common/UploadForm/index.tsx b/src/frontend/packages/lib_components/src/common/UploadForm/index.tsx index c048ddd1af..1a398833dd 100644 --- a/src/frontend/packages/lib_components/src/common/UploadForm/index.tsx +++ b/src/frontend/packages/lib_components/src/common/UploadForm/index.tsx @@ -14,7 +14,6 @@ import { Loader } from '@lib-components/common/Loader'; import { UploadField } from '@lib-components/common/UploadField'; import { UploadManagerStatus, - UploadingObject, useUploadManager, } from '@lib-components/common/UploadManager'; import { builderDashboardRoute } from '@lib-components/data/routes'; @@ -114,14 +113,12 @@ const UploadFormBack = styled.div` export interface UploadFormProps { objectId: UploadableObject['id']; objectType: uploadableModelName; - parentType?: Maybe; parentId?: Maybe; } export const UploadForm = ({ objectId, objectType, - parentType, parentId, }: UploadFormProps) => { const appData = useAppConfig(); @@ -207,9 +204,7 @@ export const UploadForm = ({ /> - + diff --git a/src/frontend/packages/lib_components/src/common/UploadManager/UploadHandlers.spec.tsx b/src/frontend/packages/lib_components/src/common/UploadManager/UploadHandlers.spec.tsx index 3188d65b6b..cec93e0442 100644 --- a/src/frontend/packages/lib_components/src/common/UploadManager/UploadHandlers.spec.tsx +++ b/src/frontend/packages/lib_components/src/common/UploadManager/UploadHandlers.spec.tsx @@ -14,7 +14,7 @@ import { Thumbnail, Video } from '@lib-components/types/tracks'; import { UploadHandlers } from './UploadHandlers'; -import { UploadManagerContext, UploadManagerStatus, UploadingObject } from '.'; +import { UploadManagerContext, UploadManagerStatus } from '.'; jest.mock('data/sideEffects/updateResource', () => ({ updateResource: jest.fn(), @@ -200,7 +200,6 @@ describe('', () => { progress: 0, status: UploadManagerStatus.UPLOADING, parentId: uuidv4(), - parentType: modelName.VIDEOS as UploadingObject['parentType'], }; const thumbnail = { id: thumbnailState.objectId } as Thumbnail; mockFetchResource.mockResolvedValue(thumbnail); diff --git a/src/frontend/packages/lib_components/src/common/UploadManager/UploadHandlers.tsx b/src/frontend/packages/lib_components/src/common/UploadManager/UploadHandlers.tsx index 30090e4996..883b59287b 100644 --- a/src/frontend/packages/lib_components/src/common/UploadManager/UploadHandlers.tsx +++ b/src/frontend/packages/lib_components/src/common/UploadManager/UploadHandlers.tsx @@ -14,8 +14,7 @@ const UploadSuccessHandler = ({ }: { objectState: UploadManagerState[string]; }) => { - const { status, file, objectId, objectType, parentId, parentType } = - objectState; + const { status, file, objectId, objectType, parentId } = objectState; // once upload on S3 is finished, push new state to backend useEffect(() => { @@ -42,11 +41,10 @@ const UploadSuccessHandler = ({ }, objectType, parentId, - parentType, ); } })(); - }, [file.name, objectId, objectType, parentId, parentType, status]); + }, [file.name, objectId, objectType, parentId, status]); // update the ressource beeing uploaded useEffect(() => { @@ -55,9 +53,9 @@ const UploadSuccessHandler = ({ return; } - await fetchResource(objectType, objectId, parentType, parentId); + await fetchResource(objectType, objectId, parentId); })(); - }, [objectId, objectType, parentId, parentType, status]); + }, [objectId, objectType, parentId, status]); return null; }; diff --git a/src/frontend/packages/lib_components/src/common/UploadManager/index.spec.tsx b/src/frontend/packages/lib_components/src/common/UploadManager/index.spec.tsx index 923acf9214..163eec63dc 100644 --- a/src/frontend/packages/lib_components/src/common/UploadManager/index.spec.tsx +++ b/src/frontend/packages/lib_components/src/common/UploadManager/index.spec.tsx @@ -112,7 +112,6 @@ describe('', () => { it('uploads the file with a parent path', async () => { const objectType = modelName.THUMBNAILS; const objectId = uuidv4(); - const parentType = modelName.VIDEOS; const parentId = uuidv4(); const file = new File(['(⌐□_□)'], 'course.jpg', { type: 'image/jpeg' }); @@ -138,7 +137,7 @@ describe('', () => { { const { addUpload, uploadManagerState } = getLatestHookValues(); expect(uploadManagerState).toEqual({}); - act(() => addUpload(objectType, objectId, file, parentType, parentId)); + act(() => addUpload(objectType, objectId, file, parentId)); } { const { uploadManagerState } = getLatestHookValues(); @@ -149,7 +148,6 @@ describe('', () => { file, progress: 0, status: UploadManagerStatus.INIT, - parentType, parentId, }, }); @@ -174,7 +172,6 @@ describe('', () => { file, progress: 0, status: UploadManagerStatus.UPLOADING, - parentType, parentId, }, }); @@ -195,7 +192,6 @@ describe('', () => { file, progress: 0, status: UploadManagerStatus.SUCCESS, - parentType, parentId, }, }); diff --git a/src/frontend/packages/lib_components/src/common/UploadManager/index.tsx b/src/frontend/packages/lib_components/src/common/UploadManager/index.tsx index 9a513da7d6..c7363ed46a 100644 --- a/src/frontend/packages/lib_components/src/common/UploadManager/index.tsx +++ b/src/frontend/packages/lib_components/src/common/UploadManager/index.tsx @@ -32,11 +32,6 @@ export interface UploadingObject { progress: number; status: UploadManagerStatus; message?: string; - parentType?: - | modelName.VIDEOS - | MarkdownDocumentModelName.MARKDOWN_DOCUMENTS - | ClassroomModelName.CLASSROOMS - | FileDepositoryModelName.FileDepositories; parentId?: string; } @@ -89,7 +84,7 @@ export const UploadManager = ({ Object.values(uploadManagerState) .filter(({ status }) => status === UploadManagerStatus.INIT) - .forEach(({ file, objectId, objectType, parentId, parentType }) => { + .forEach(({ file, objectId, objectType, parentId }) => { (async () => { let presignedPost: AWSPresignedPost; try { @@ -99,7 +94,6 @@ export const UploadManager = ({ file.name, file.type, file.size, - parentType, parentId, ); } catch (error) { @@ -209,7 +203,6 @@ export const useUploadManager = () => { objectType: uploadableModelName, objectId: string, file: File, - parentType?: Maybe, parentId?: Maybe, ) => { setUploadState((state) => ({ @@ -220,7 +213,6 @@ export const useUploadManager = () => { file, progress: 0, status: UploadManagerStatus.INIT, - parentType, parentId, }, })); diff --git a/src/frontend/packages/lib_components/src/data/sideEffects/getResource/index.tsx b/src/frontend/packages/lib_components/src/data/sideEffects/getResource/index.tsx index c477292b0b..9475d1779f 100644 --- a/src/frontend/packages/lib_components/src/data/sideEffects/getResource/index.tsx +++ b/src/frontend/packages/lib_components/src/data/sideEffects/getResource/index.tsx @@ -1,12 +1,14 @@ import { Maybe } from '@lib-common/types'; -import { UploadingObject } from '@lib-components/common'; import { fetchWrapper } from '@lib-components/common/queries/fetchWrapper'; import { addResource } from '@lib-components/data/stores/generics'; import { useJwt } from '@lib-components/hooks/stores/useJwt'; import { API_ENDPOINT } from '@lib-components/settings'; import { requestStatus } from '@lib-components/types/api'; -import { uploadableModelName } from '@lib-components/types/models'; +import { + getParentType, + uploadableModelName, +} from '@lib-components/types/models'; import { UploadableObject } from '@lib-components/types/tracks'; import { report } from '@lib-components/utils/errors/report'; @@ -19,10 +21,10 @@ import { report } from '@lib-components/utils/errors/report'; export async function getResource( resourceName: uploadableModelName, resourceId: string, - parentType?: Maybe, parentId?: Maybe, ): Promise { let endpoint = `${API_ENDPOINT}/${resourceName}/${resourceId}/`; + const parentType = getParentType(resourceName); if (parentId && parentType) { endpoint = `${API_ENDPOINT}/${parentType}/${parentId}/${resourceName}/${resourceId}/`; } diff --git a/src/frontend/packages/lib_components/src/data/sideEffects/initiateUpload/index.ts b/src/frontend/packages/lib_components/src/data/sideEffects/initiateUpload/index.ts index fb74d4be7d..7e035e892d 100644 --- a/src/frontend/packages/lib_components/src/data/sideEffects/initiateUpload/index.ts +++ b/src/frontend/packages/lib_components/src/data/sideEffects/initiateUpload/index.ts @@ -1,11 +1,13 @@ import { Maybe } from '@lib-common/types'; -import { UploadingObject } from '@lib-components/common'; import { fetchWrapper } from '@lib-components/common/queries/fetchWrapper'; import { useJwt } from '@lib-components/hooks/stores'; import { API_ENDPOINT } from '@lib-components/settings'; import { AWSPresignedPost } from '@lib-components/types/AWSPresignedPost'; -import { uploadableModelName } from '@lib-components/types/models'; +import { + getParentType, + uploadableModelName, +} from '@lib-components/types/models'; import { UploadableObject } from '@lib-components/types/tracks'; /** @@ -16,7 +18,6 @@ import { UploadableObject } from '@lib-components/types/tracks'; * @param filename The name of the file we're uploading. * @param mimetype The mimetype of the file we're uploading. * @param size The size of the file we're uploading. - * @param parentType The kind of parent object for which we're uploading a file (nullable model name). * @param parentId The ID of the parent object for which we're uploading a file (nullable). */ export const initiateUpload = async ( @@ -25,10 +26,10 @@ export const initiateUpload = async ( filename: string, mimetype: string, size: number, - parentType?: Maybe, parentId?: Maybe, ) => { let input = `${API_ENDPOINT}/${objectType}/${objectId}/initiate-upload/`; + const parentType = getParentType(objectType); if (parentId && parentType) { input = `${API_ENDPOINT}/${parentType}/${parentId}/${objectType}/${objectId}/initiate-upload/`; } diff --git a/src/frontend/packages/lib_components/src/data/sideEffects/updateResource/index.ts b/src/frontend/packages/lib_components/src/data/sideEffects/updateResource/index.ts index 1caccdb643..d68984190d 100644 --- a/src/frontend/packages/lib_components/src/data/sideEffects/updateResource/index.ts +++ b/src/frontend/packages/lib_components/src/data/sideEffects/updateResource/index.ts @@ -1,19 +1,21 @@ import { Maybe } from '@lib-common/types'; -import { UploadingObject } from '@lib-components/common'; import { fetchWrapper } from '@lib-components/common/queries/fetchWrapper'; import { useJwt } from '@lib-components/hooks/stores'; import { API_ENDPOINT } from '@lib-components/settings'; -import { uploadableModelName } from '@lib-components/types/models'; +import { + getParentType, + uploadableModelName, +} from '@lib-components/types/models'; import { Resource } from '@lib-components/types/tracks'; export async function updateResource( resource: R, resourceName: uploadableModelName, parentId?: Maybe, - parentType?: Maybe, ): Promise { let endpoint = `${API_ENDPOINT}/${resourceName}/${resource.id}/`; + const parentType = getParentType(resourceName); if (parentId && parentType) { endpoint = `${API_ENDPOINT}/${parentType}/${parentId}/${resourceName}/${resource.id}/`; } diff --git a/src/frontend/packages/lib_components/src/types/models.ts b/src/frontend/packages/lib_components/src/types/models.ts index 11170e7d6a..a216012ecb 100644 --- a/src/frontend/packages/lib_components/src/types/models.ts +++ b/src/frontend/packages/lib_components/src/types/models.ts @@ -1,3 +1,5 @@ +import { Nullable } from '@lib-common/types'; + import { ClassroomModelName } from '@lib-components/types/apps/classroom/models'; import { FileDepositoryModelName } from '@lib-components/types/apps/deposit/models'; import { MarkdownDocumentModelName } from '@lib-components/types/apps/markdown/models'; @@ -18,3 +20,30 @@ export type uploadableModelName = > | Extract | Extract; + +export type parentType = + | modelName.VIDEOS + | MarkdownDocumentModelName.MARKDOWN_DOCUMENTS + | ClassroomModelName.CLASSROOMS + | FileDepositoryModelName.FileDepositories; + +export const getParentType = ( + objectType: Nullable, +): Nullable => { + switch (objectType) { + case modelName.TIMEDTEXTTRACKS: + return modelName.VIDEOS; + case modelName.SHAREDLIVEMEDIAS: + return modelName.VIDEOS; + case modelName.THUMBNAILS: + return modelName.VIDEOS; + case MarkdownDocumentModelName.MARKDOWN_IMAGES: + return MarkdownDocumentModelName.MARKDOWN_DOCUMENTS; + case FileDepositoryModelName.DepositedFiles: + return FileDepositoryModelName.FileDepositories; + case ClassroomModelName.CLASSROOM_DOCUMENTS: + return ClassroomModelName.CLASSROOMS; + default: + return null; + } +};