Skip to content

Commit

Permalink
fixup! 🚚(frontend) allow generic request to handle parent resource
Browse files Browse the repository at this point in the history
  • Loading branch information
kernicPanel committed Aug 22, 2023
1 parent 232866b commit a12d4cb
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 48 deletions.
4 changes: 1 addition & 3 deletions src/frontend/apps/lti_site/components/LTIRoutes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
UploadForm,
UploadHandlers,
UploadManager,
UploadingObject,
WithParams,
builderFullScreenErrorRoute,
modelName,
Expand Down Expand Up @@ -131,12 +130,11 @@ export const LTIInnerRoutes = () => {
path={UPLOAD_FORM_ROUTE.default}
element={
<WithParams>
{({ objectId, objectType, parentType, parentId }) =>
{({ objectId, objectType, parentId }) =>
objectId && objectType ? (
<UploadForm
objectId={objectId}
objectType={objectType as modelName}
parentType={parentType as UploadingObject['parentType']}
parentId={parentId}
/>
) : (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -28,14 +25,12 @@ const DropzoneStyled = styled.div`
export interface UploadFieldProps {
objectType: uploadableModelName;
objectId: string;
parentType?: Maybe<UploadingObject['parentType']>;
parentId?: Maybe<string>;
}

export const UploadField = ({
objectType,
objectId,
parentType,
parentId,
}: UploadFieldProps) => {
const { addUpload } = useUploadManager();
Expand All @@ -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 (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -114,14 +113,12 @@ const UploadFormBack = styled.div`
export interface UploadFormProps {
objectId: UploadableObject['id'];
objectType: uploadableModelName;
parentType?: Maybe<UploadingObject['parentType']>;
parentId?: Maybe<string>;
}

export const UploadForm = ({
objectId,
objectType,
parentType,
parentId,
}: UploadFormProps) => {
const appData = useAppConfig();
Expand Down Expand Up @@ -207,9 +204,7 @@ export const UploadForm = ({
/>
</IframeHeadingWithLayout>
<UploadFieldContainer>
<UploadField
{...{ objectType, objectId, parentType, parentId }}
/>
<UploadField {...{ objectType, objectId, parentId }} />
</UploadFieldContainer>
</UploadFormContainer>
<UploadFormBack>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -200,7 +200,6 @@ describe('<LTIUploadHandlers />', () => {
progress: 0,
status: UploadManagerStatus.UPLOADING,
parentId: uuidv4(),
parentType: modelName.VIDEOS as UploadingObject['parentType'],
};
const thumbnail = { id: thumbnailState.objectId } as Thumbnail;
mockFetchResource.mockResolvedValue(thumbnail);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -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(() => {
Expand All @@ -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;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ describe('<UploadManager />', () => {
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' });

Expand All @@ -138,7 +137,7 @@ describe('<UploadManager />', () => {
{
const { addUpload, uploadManagerState } = getLatestHookValues();
expect(uploadManagerState).toEqual({});
act(() => addUpload(objectType, objectId, file, parentType, parentId));
act(() => addUpload(objectType, objectId, file, parentId));
}
{
const { uploadManagerState } = getLatestHookValues();
Expand All @@ -149,7 +148,6 @@ describe('<UploadManager />', () => {
file,
progress: 0,
status: UploadManagerStatus.INIT,
parentType,
parentId,
},
});
Expand All @@ -174,7 +172,6 @@ describe('<UploadManager />', () => {
file,
progress: 0,
status: UploadManagerStatus.UPLOADING,
parentType,
parentId,
},
});
Expand All @@ -195,7 +192,6 @@ describe('<UploadManager />', () => {
file,
progress: 0,
status: UploadManagerStatus.SUCCESS,
parentType,
parentId,
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 {
Expand All @@ -99,7 +94,6 @@ export const UploadManager = ({
file.name,
file.type,
file.size,
parentType,
parentId,
);
} catch (error) {
Expand Down Expand Up @@ -209,7 +203,6 @@ export const useUploadManager = () => {
objectType: uploadableModelName,
objectId: string,
file: File,
parentType?: Maybe<UploadingObject['parentType']>,
parentId?: Maybe<string>,
) => {
setUploadState((state) => ({
Expand All @@ -220,7 +213,6 @@ export const useUploadManager = () => {
file,
progress: 0,
status: UploadManagerStatus.INIT,
parentType,
parentId,
},
}));
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -19,10 +21,10 @@ import { report } from '@lib-components/utils/errors/report';
export async function getResource(
resourceName: uploadableModelName,
resourceId: string,
parentType?: Maybe<UploadingObject['parentType']>,
parentId?: Maybe<string>,
): Promise<UploadableObject | requestStatus.FAILURE> {
let endpoint = `${API_ENDPOINT}/${resourceName}/${resourceId}/`;
const parentType = getParentType(resourceName);
if (parentId && parentType) {
endpoint = `${API_ENDPOINT}/${parentType}/${parentId}/${resourceName}/${resourceId}/`;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand All @@ -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 (
Expand All @@ -25,10 +26,10 @@ export const initiateUpload = async (
filename: string,
mimetype: string,
size: number,
parentType?: Maybe<UploadingObject['parentType']>,
parentId?: Maybe<string>,
) => {
let input = `${API_ENDPOINT}/${objectType}/${objectId}/initiate-upload/`;
const parentType = getParentType(objectType);
if (parentId && parentType) {
input = `${API_ENDPOINT}/${parentType}/${parentId}/${objectType}/${objectId}/initiate-upload/`;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<R extends Resource>(
resource: R,
resourceName: uploadableModelName,
parentId?: Maybe<string>,
parentType?: Maybe<UploadingObject['parentType']>,
): Promise<R> {
let endpoint = `${API_ENDPOINT}/${resourceName}/${resource.id}/`;
const parentType = getParentType(resourceName);
if (parentId && parentType) {
endpoint = `${API_ENDPOINT}/${parentType}/${parentId}/${resourceName}/${resource.id}/`;
}
Expand Down
29 changes: 29 additions & 0 deletions src/frontend/packages/lib_components/src/types/models.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -18,3 +20,30 @@ export type uploadableModelName =
>
| Extract<FileDepositoryModelName, FileDepositoryModelName.DepositedFiles>
| Extract<ClassroomModelName, ClassroomModelName.CLASSROOM_DOCUMENTS>;

export type parentType =
| modelName.VIDEOS
| MarkdownDocumentModelName.MARKDOWN_DOCUMENTS
| ClassroomModelName.CLASSROOMS
| FileDepositoryModelName.FileDepositories;

export const getParentType = (
objectType: Nullable<uploadableModelName>,
): Nullable<parentType> => {
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;
}
};

0 comments on commit a12d4cb

Please sign in to comment.