From f31eade93ebdb8da7b9a3d1c5f2d37bd3069f8a2 Mon Sep 17 00:00:00 2001 From: danilych Date: Sun, 11 Feb 2024 18:01:55 +0100 Subject: [PATCH] feat(course): implement course editing page #wip --- app/entities/course-card.tsx | 6 +- app/redux/slices/course.ts | 64 ++++++++++++++++++++++ app/redux/store.ts | 2 + app/routes/edit-course.$course.tsx | 82 ++++++++++++++++++++++++++++ app/routes/my-account.courses.tsx | 5 +- app/routes/my-account.my-courses.tsx | 1 + app/shared/Inputs/rounded-input.tsx | 2 +- 7 files changed, 155 insertions(+), 7 deletions(-) create mode 100644 app/redux/slices/course.ts create mode 100644 app/routes/edit-course.$course.tsx diff --git a/app/entities/course-card.tsx b/app/entities/course-card.tsx index a39952f..8d5b7cd 100644 --- a/app/entities/course-card.tsx +++ b/app/entities/course-card.tsx @@ -8,11 +8,13 @@ interface Props { name: string price: string rating: string + isBought?: boolean + isForEdit?: boolean } -export function CourseCard({ id, picturePath, name, price, rating }: Props) { +export function CourseCard({ id, picturePath, name, price, rating, isBought, isForEdit }: Props) { return ( - +
{ + const { data } = await instance.post('/Course/Apply', params) + + return data + } +) + +export const fetchCourse = createAsyncThunk( + 'course/fetchCourse', + async (params: any) => { + const { data } = await instance.post('/Course/ViewCourse', params) + + return data + } +) + +export const updateBlock = createAsyncThunk( + 'course/updateBlock', + async (params: any) => { + const { data } = await instance.post('/CourseBlock/Edit', params) + + return data + } +) + +const initialState = { + course: { + data: null, + isError: false, + status: 'loading', + }, +} + +const courseSlice = createSlice({ + name: 'course', + initialState, + reducers: {}, + extraReducers: builder => { + builder.addCase(fetchCourse.pending, (state, action) => { + state.course.status = 'loading' + + // @ts-ignore + state.course.data = null + }) + 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' + + // @ts-ignore + state.course.data = null + }) + }, +}) + +export const courseItemReducer = courseSlice.reducer diff --git a/app/redux/store.ts b/app/redux/store.ts index 3b1bd61..c49a8a6 100644 --- a/app/redux/store.ts +++ b/app/redux/store.ts @@ -2,10 +2,12 @@ import { configureStore } from "@reduxjs/toolkit"; import { authReducer } from "./slices/auth"; import { courseReducer } from "./slices/courses"; import { userReducer } from "./slices/user"; +import { courseItemReducer } from "./slices/course"; const store = configureStore({ reducer: { courses: courseReducer, + course: courseItemReducer, auth: authReducer, user: userReducer, }, diff --git a/app/routes/edit-course.$course.tsx b/app/routes/edit-course.$course.tsx new file mode 100644 index 0000000..6b3ee20 --- /dev/null +++ b/app/routes/edit-course.$course.tsx @@ -0,0 +1,82 @@ +import type { ThunkDispatch } from '@reduxjs/toolkit' +import { useParams } from '@remix-run/react' +import { Spinner } from 'flowbite-react' +import { useEffect, useState } from 'react' +import { useDispatch, useSelector } from 'react-redux' +import { fetchCourse } from '~/redux/slices/course' +import { + Header2, + Header3, + Header4, + RoundedInput, + TransparentButton, +} from '~/shared' + +export default function Course() { + const params = useParams() + + const dispatch = useDispatch>() + + // @ts-ignore + const { course } = useSelector(state => state.course) + + const [isPostsLoading, setIsPostLoading] = useState(true) + + useEffect(() => { + let formData = new FormData() + formData.append('CourseID', params.course as string) + console.log(params.course) + // formData.append('UserID', window.localStorage.getItem("userId") as string) + + dispatch(fetchCourse(formData)) + }, []) + + useEffect(() => { + if (course.status === 'loaded') setIsPostLoading(false) + + if (course.status === 'loading') setIsPostLoading(true) + }) + + return ( +
+ {isPostsLoading ? ( +
+ +
+ ) : ( +
+ {course.data.name} + + + + Розділи курсу + +
+ Додайти інформаційний блок + +
+ + +
+ + Додати + +
+
+
+
+ )} +
+ ) +} diff --git a/app/routes/my-account.courses.tsx b/app/routes/my-account.courses.tsx index 6b989a4..5a77e5c 100644 --- a/app/routes/my-account.courses.tsx +++ b/app/routes/my-account.courses.tsx @@ -54,14 +54,11 @@ export default function Courses() {
) : (
- + {/* @ts-ignore */} {courses.items.map((course: any) => ( ))} - {courses.items.map((course: any) => ( - - ))}
)} diff --git a/app/routes/my-account.my-courses.tsx b/app/routes/my-account.my-courses.tsx index 7dbe3fc..62c5eb8 100644 --- a/app/routes/my-account.my-courses.tsx +++ b/app/routes/my-account.my-courses.tsx @@ -63,6 +63,7 @@ export default function MyCourses() { {/* @ts-ignore */} {courses.items.map((course: any) => (