diff --git a/app/redux/slices/course.ts b/app/redux/slices/course.ts index 3acbd66..0693a00 100644 --- a/app/redux/slices/course.ts +++ b/app/redux/slices/course.ts @@ -50,7 +50,6 @@ const courseSlice = createSlice({ builder.addCase(fetchCourse.fulfilled, (state, action) => { state.course.status = 'loaded' state.course.data = action.payload.data - state.course.isError = action.payload.isError }) builder.addCase(fetchCourse.rejected, (state, action) => { state.course.status = 'error' @@ -58,6 +57,10 @@ const courseSlice = createSlice({ // @ts-ignore state.course.data = null }) + builder.addCase(updateBlock.fulfilled, (state, action) => { + state.course.status = 'loaded' + state.course.isError = action.payload.isError + }) }, }) diff --git a/app/routes/edit-course.$course.tsx b/app/routes/edit-course.$course.tsx index 6b3ee20..cb94260 100644 --- a/app/routes/edit-course.$course.tsx +++ b/app/routes/edit-course.$course.tsx @@ -2,8 +2,10 @@ import type { ThunkDispatch } from '@reduxjs/toolkit' import { useParams } from '@remix-run/react' import { Spinner } from 'flowbite-react' import { useEffect, useState } from 'react' +import { useForm } from 'react-hook-form' +import toast, { Toaster } from 'react-hot-toast' import { useDispatch, useSelector } from 'react-redux' -import { fetchCourse } from '~/redux/slices/course' +import { fetchCourse, updateBlock } from '~/redux/slices/course' import { Header2, Header3, @@ -15,6 +17,15 @@ import { export default function Course() { const params = useParams() + const { register, handleSubmit } = useForm({ + defaultValues: { + Name: '', + ID: '-1', + CourseID: params.course as string, + }, + mode: 'onChange', + }) + const dispatch = useDispatch>() // @ts-ignore @@ -22,6 +33,8 @@ export default function Course() { const [isPostsLoading, setIsPostLoading] = useState(true) + console.log(course.data) + useEffect(() => { let formData = new FormData() formData.append('CourseID', params.course as string) @@ -35,8 +48,28 @@ export default function Course() { if (course.status === 'loaded') setIsPostLoading(false) if (course.status === 'loading') setIsPostLoading(true) + + if (course.status === 'error') setIsPostLoading(true) }) + const onSubmit = async (values: any) => { + try { + const data = await dispatch(updateBlock(values)) + + if (data.payload && data.payload.isError == false) { + console.log(true) + + toast.success('Block was successfully created!') + } else { + toast.error('Something went wrong!') + + return + } + } catch (error) { + toast.error('Something went wrong!') + } + } + return (
{isPostsLoading ? ( @@ -65,18 +98,43 @@ export default function Course() {
Додайти інформаційний блок -
- +
+
Додати
-
+
)} + + ) }