diff --git a/src/components/Layout/Layout.tsx b/src/components/Layout/Layout.tsx index 7712130..eb89150 100644 --- a/src/components/Layout/Layout.tsx +++ b/src/components/Layout/Layout.tsx @@ -44,7 +44,7 @@ const Layout: React.FC = ({ children, roles }) => { as='main' flex='1 0' maxW='85rem' - ml='25rem' + ml='20rem' mt='7rem' mb='1rem' pr='2rem' diff --git a/src/pages/courses/components/CourseList/CourseList.tsx b/src/pages/courses/components/CourseList/CourseList.tsx index 7682f85..5eddc81 100644 --- a/src/pages/courses/components/CourseList/CourseList.tsx +++ b/src/pages/courses/components/CourseList/CourseList.tsx @@ -85,7 +85,7 @@ const CourseList: React.FC = ({ courses, isLoading }) => { {deletingCourse && } {courses.length !== 0 ? ( = () => { const { userStore: { userService }, categoryStore: { categoryService }, + courseStore: { courseService }, } = useRootStore(); - const { register, formState, handleSubmit, control, reset } = useForm(); + const { + register, + handleSubmit, + formState: { errors }, + control, + watch, + reset, + } = useForm({ + resolver: yupResolver(CreateCourseSchema), + }); const handleError = useErrorHandler(); const toast = useToast(); + const history = useHistory(); + const isMountedRef = useIsMountedRef(); + const [isLoading, setIsLoading] = useState(false); + const [status, setStatus] = useState(undefined); + + const title = useWatch({ + control, + name: 'title', + defaultValue: '', + }); /** * Load users into select field */ - const loadUsersOptions = async (inputValue: string) => { + const loadUsersOptions = async (inputValue: string): Promise => { try { const users = await userService.fetchAll({ search: inputValue, role: UserRoleEnum.TEACHER }); - return users.data.map(user => ({ - label: user.fullname, - value: user.id, - })); + + // TODO: remove filter later + return users.data + .filter(user => helpers.userContainRoles(user.roles, [UserRoleEnum.TEACHER])) + .map(user => ({ + label: user.fullname, + value: user.id, + })); } catch (error: any) { if (error.response) { toast({ title: 'Error' }); @@ -54,7 +102,7 @@ const CreateCourseForm: React.FC = () => { /** * Load categories into select filed */ - const loadCategoriesOptions = async (inputValue: string) => { + const loadCategoriesOptions = async (inputValue: string): Promise => { try { const categories = await categoryService.fetchAll(); return categories.data.map(category => ({ @@ -74,27 +122,68 @@ const CreateCourseForm: React.FC = () => { * Submit form handler */ const onSubmit: SubmitHandler = async data => { - console.log(data); + try { + setIsLoading(true); + await courseService.create({ + title: data.title, + description: data.description, + teacher_id: data.teacher_id, + category_id: data.category_id, + image: data.image[0], + status: status ?? CourseStatusEnum.DRAFT, + }); + toast({ title: `Course successfully created.`, status: 'success' }); + history.push('/courses'); + } catch (error: any) { + if (error.response) { + if (error.response.status === 422) { + toast({ title: `${error.response.data.errors[0].message}`, status: 'error' }); + } else { + toast({ title: `${error.message}`, status: 'error' }); + } + } else { + handleError(error); + } + } finally { + if (isMountedRef.current) { + setIsLoading(false); + setStatus(undefined); + } + } }; return (
- + Title - - + + 60 ? 'red.500' : 'gray.500'}> + {title.length}/60 + + } + /> + + {errors.title?.message} + - - Image - - - - Category + + Image + + + {errors.image?.message} + + + + + Category = () => { /> )} /> + + {errors.category_id?.message} + - + Teacher = () => { /> )} /> + + {errors.teacher_id?.message} + - + Description -