diff --git a/src/components/AsyncSelect/AsyncSelect.tsx b/src/components/AsyncSelect/AsyncSelect.tsx index 22d8b08..ec3b0ec 100644 --- a/src/components/AsyncSelect/AsyncSelect.tsx +++ b/src/components/AsyncSelect/AsyncSelect.tsx @@ -2,6 +2,7 @@ import React, { useEffect, useState } from 'react'; import Select from '@educt/components/Select'; import { ISelectPropsType } from '../Select/Select'; import { OptionType } from '@educt/types'; +import useIsMountedRef from '@educt/hooks/useIsMountedRef'; interface IAsyncSelectPropsType extends ISelectPropsType { loadOptions: () => Promise; @@ -10,13 +11,22 @@ interface IAsyncSelectPropsType extends ISelectPropsType { const AsyncSelect: React.FC = props => { const [options, setOptions] = useState([]); const [isLoading, setIsLoading] = useState(false); + const isMountedRef = useIsMountedRef(); const { loadOptions, ...selectProps } = props; useEffect(() => { - setIsLoading(true); + if (isMountedRef.current) setIsLoading(true); loadOptions() - .then(options => options && setOptions(options)) - .finally(() => setIsLoading(false)); + .then(options => { + if (options && isMountedRef.current) { + setOptions(options); + } + }) + .finally(() => { + if (isMountedRef.current) { + setIsLoading(false); + } + }); }, []); return - {selectedFile ? ( + {file ? ( - {selectedFile.name} + {file.name}
- {helpres.transformBytes(selectedFile.size)} + {helpres.transformBytes(file.size)}
) : ( diff --git a/src/components/Forms/CourseForm/CourseForm.tsx b/src/components/Forms/CourseForm/CourseForm.tsx index 9183e10..18bdc68 100644 --- a/src/components/Forms/CourseForm/CourseForm.tsx +++ b/src/components/Forms/CourseForm/CourseForm.tsx @@ -177,8 +177,12 @@ const CourseForm: React.FC = ({ onSubmit, buttonLabel, isLo ( - onChange(file)} supportedFormats={['JPG', 'JPEG', 'PNG', 'WEBP']} /> + render={({ field: { onChange, value: file } }) => ( + onChange(file)} + file={file} + supportedFormats={['JPG', 'JPEG', 'PNG', 'WEBP']} + /> )} /> diff --git a/src/components/Forms/CourseForm/EditCourseForm.tsx b/src/components/Forms/CourseForm/EditCourseForm.tsx index 9026d7a..5dfb45e 100644 --- a/src/components/Forms/CourseForm/EditCourseForm.tsx +++ b/src/components/Forms/CourseForm/EditCourseForm.tsx @@ -70,6 +70,7 @@ const EditFormCourse: React.FC = ({ defaultValues }) => description: data.description, teacher_id: data.teacher_id, category_id: data.category_id, + image: data.image, }); toast({ title: `Course updated.`, status: 'info' }); @@ -81,6 +82,7 @@ const EditFormCourse: React.FC = ({ defaultValues }) => description: course.data.description, teacher_id: course.data.teacher.id, category_id: course.data.category.id, + image: undefined, }); } catch (error: any) { if (error.response) { diff --git a/src/hooks/useFetchCourseQuery.ts b/src/hooks/useFetchCourseQuery.ts index c88147d..591f633 100644 --- a/src/hooks/useFetchCourseQuery.ts +++ b/src/hooks/useFetchCourseQuery.ts @@ -5,6 +5,7 @@ import { ICourse } from '@educt/interfaces'; */ import { useEffect, useState } from 'react'; import { useErrorHandler } from 'react-error-boundary'; +import useIsMountedRef from './useIsMountedRef'; import { useRootStore } from './useRootStore'; type CourseStateType = { @@ -19,6 +20,7 @@ const useFetchCourseQuery = (id: string) => { courseStore: { courseService }, } = useRootStore(); const [state, setState] = useState({ data: null, error: null, loading: false, fetched: false }); + const isMountedRef = useIsMountedRef(); const handleError = useErrorHandler(); /** @@ -29,17 +31,23 @@ const useFetchCourseQuery = (id: string) => { courseService .fetchById(id) .then(data => { - setState(s => ({ ...s, data: data.data })); + if (isMountedRef.current) { + setState(s => ({ ...s, data: data.data })); + } }) .catch(error => { if (error.response) { - setState(s => ({ ...s, error })); + if (isMountedRef.current) { + setState(s => ({ ...s, error })); + } } else { handleError(error); } }) .finally(() => { - setState(s => ({ ...s, loading: false, fetched: true })); + if (isMountedRef.current) { + setState(s => ({ ...s, loading: false, fetched: true })); + } }); }, [id]); diff --git a/src/services/CourseService.ts b/src/services/CourseService.ts index 257c6ee..dd080d1 100644 --- a/src/services/CourseService.ts +++ b/src/services/CourseService.ts @@ -76,7 +76,11 @@ export class CourseService { id: string, data: UpdateCourseParamsType ): Promise>> { - const result = await this.api.patch(`/v1/courses/${id}`, data); + const result = await this.api.patch(`/v1/courses/${id}`, helpers.transformToFormData(data), { + headers: { + 'Content-Type': 'multipart/form-data', + }, + }); return result.data; }