diff --git a/src/components/FilesUploader/FilesUploader.tsx b/src/components/FilesUploader/FilesUploader.tsx index 1907b95..6999b23 100644 --- a/src/components/FilesUploader/FilesUploader.tsx +++ b/src/components/FilesUploader/FilesUploader.tsx @@ -88,7 +88,7 @@ const FilesUploader: React.FC = ({ onChange, files, prel { - setPreloaded(prev => prev.filter(file => file.size !== file.size)); + setPreloaded(prev => prev.filter(prevFile => prevFile.size !== file.size)); onChange([]); }} aria-label='remove' diff --git a/src/components/Forms/LessonForm/EditLessonForm.tsx b/src/components/Forms/LessonForm/EditLessonForm.tsx index 72e74bc..ac843ef 100644 --- a/src/components/Forms/LessonForm/EditLessonForm.tsx +++ b/src/components/Forms/LessonForm/EditLessonForm.tsx @@ -1,5 +1,4 @@ import React from 'react'; -import * as helpers from '@educt/helpers'; import { yupResolver } from '@hookform/resolvers/yup'; import { SubmitHandler, useForm } from 'react-hook-form'; import type { InputFields } from './LessonForm'; @@ -13,8 +12,9 @@ import { EditLessonFormSchema } from './LessonForm.validator'; /** * Hooks */ -import { useHistory, useParams } from 'react-router-dom'; +import { useParams } from 'react-router-dom'; import { ILesson } from '@educt/interfaces'; +import { useUpdateLesson } from '@educt/hooks/queries'; type EditLessonFormPropsType = { lesson: ILesson; @@ -31,17 +31,22 @@ const EditLessonForm: React.FC = ({ lesson }) => { materials: undefined, }, }); - const { id: lesson_id } = useParams<{ id: string }>(); - const history = useHistory(); + const { updateLesson, isLoading } = useUpdateLesson(); + const { id } = useParams<{ id: string }>(); const onSubmit: SubmitHandler = async data => { - const updatedFields = helpers.getDirtyFields(form.formState.dirtyFields, data); - console.log(updatedFields); + await updateLesson(id, { + title: data.title, + description: data.description, + video: data.video, + duration: data.duration, + materials: data.materials?.length !== 0 ? Array.from(data.materials || []) : null, + }); }; return ( { + const toast = useToast(); + const handleError = useErrorHandler(); + + const updateLesson = async (id: string, data: UpdateLessonParamsType) => { + try { + const result = await LessonServiceInstance.update(id, data); + toast({ title: 'Lesson updated', status: 'info' }); + return result.data; + } catch (error) { + if (axios.isAxiosError(error)) { + if (error.response) { + switch (error.response.status) { + default: + toast({ title: `${error.message}`, status: 'error' }); + break; + } + } else { + handleError(error); + } + } else { + handleError(error); + } + + return Promise.reject(error); + } + }; + + const { execute, ...state } = useAsync>(updateLesson); + + return { updateLesson: execute, ...state }; +}; + +export { useUpdateLesson }; diff --git a/src/services/LessonService.ts b/src/services/LessonService.ts index 814f2c8..68c7ea9 100644 --- a/src/services/LessonService.ts +++ b/src/services/LessonService.ts @@ -2,7 +2,7 @@ import { AxiosInstance } from 'axios'; import * as helpers from '@educt/helpers'; import { IApiRespose, ILesson } from '@educt/interfaces'; import { ApiServiceInstance } from './ApiService'; -import type { CreateLessonParamsType, LessonProgress } from '@educt/types'; +import type { CreateLessonParamsType, LessonProgress, UpdateLessonParamsType } from '@educt/types'; class LessonService { public api: AxiosInstance; @@ -65,6 +65,18 @@ class LessonService { return result.data; } + /** + * Update lesson by id + * + * @param id Lesson ud + * @param data New data + * @returns Updated lesson + */ + public async update(id: string, data: UpdateLessonParamsType): Promise> { + const result = await this.api.patch(`/v1/lessons/${id}`, helpers.transformToFormData(data)); + return result.data; + } + /** * Save order of lessons * diff --git a/src/types/index.d.ts b/src/types/index.d.ts index eb09026..4ef4194 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -169,3 +169,12 @@ export type CreateLessonParamsType = { video: File; materials?: File[] | undefined; }; + +export type UpdateLessonParamsType = { + title?: string | undefined; + description?: string | undefined; + duration?: string | undefined; + course_id?: string | undefined; + video?: File | undefined; + materials?: File[] | undefined | null; +};