From eb4d97156ed7ecd552336be9d1ddecc11e825cfe Mon Sep 17 00:00:00 2001 From: danilych Date: Tue, 13 Feb 2024 09:56:55 +0100 Subject: [PATCH] feat(view-course): fetch author data #wip --- app/redux/slices/author.ts | 45 ++++++++++++++++++++++++++++++++++ app/redux/store.ts | 2 ++ app/routes/courses.$course.tsx | 14 ++++++++--- 3 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 app/redux/slices/author.ts diff --git a/app/redux/slices/author.ts b/app/redux/slices/author.ts new file mode 100644 index 0000000..82dbb73 --- /dev/null +++ b/app/redux/slices/author.ts @@ -0,0 +1,45 @@ +import { createSlice, createAsyncThunk } from '@reduxjs/toolkit' +import moment from 'moment' +import instance from '~/axios' + +export const fetchAuthor = createAsyncThunk( + 'author/fetchAuthor', + async (params: any) => { + const { data } = await instance.post('/Course/AuthorInfo', params) + + return data + } +) + +const initialState = { + author: { + data: null, + status: 'loading', + }, +} + +const authorSlice = createSlice({ + name: 'author', + initialState, + reducers: {}, + extraReducers: builder => { + builder.addCase(fetchAuthor.pending, (state, action) => { + state.author.status = 'loading' + + // @ts-ignore + state.author.data = null + }) + builder.addCase(fetchAuthor.fulfilled, (state, action) => { + state.author.status = 'loaded' + state.author.data = action.payload + }) + builder.addCase(fetchAuthor.rejected, (state, action) => { + state.author.status = 'error' + + // @ts-ignore + state.author.data = null + }) + }, +}) + +export const authorReducer = authorSlice.reducer diff --git a/app/redux/store.ts b/app/redux/store.ts index c49a8a6..a919eea 100644 --- a/app/redux/store.ts +++ b/app/redux/store.ts @@ -3,6 +3,7 @@ import { authReducer } from "./slices/auth"; import { courseReducer } from "./slices/courses"; import { userReducer } from "./slices/user"; import { courseItemReducer } from "./slices/course"; +import { authorReducer } from "./slices/author"; const store = configureStore({ reducer: { @@ -10,6 +11,7 @@ const store = configureStore({ course: courseItemReducer, auth: authReducer, user: userReducer, + author: authorReducer, }, }); diff --git a/app/routes/courses.$course.tsx b/app/routes/courses.$course.tsx index 9a235b6..32a40e4 100644 --- a/app/routes/courses.$course.tsx +++ b/app/routes/courses.$course.tsx @@ -7,6 +7,7 @@ import { useDispatch, useSelector } from 'react-redux' import { whatYouWillLearn } from '~/data/view-course' import { ListElement } from '~/features' import { CourseElement } from '~/features/course-element' +import { fetchAuthor } from '~/redux/slices/author' import { fetchCourse } from '~/redux/slices/courses' import { fetchUser } from '~/redux/slices/user' import { FilledButton, Header2, Header3, Header4, Text } from '~/shared' @@ -26,14 +27,19 @@ export default function Course() { formData.append('CourseID', params.course as string) formData.append('UserID', window.localStorage.getItem('userId') as string) dispatch(fetchCourse(formData)) - - }, []) useEffect(() => { if (course.status === 'loading') setIsPostLoading(true) if (course.status === 'loaded') setIsPostLoading(false) + + if (course.data != null) { + let authorFormData = new FormData() + authorFormData.append('encriptId', (course.data.authorId).toString() as string) + + dispatch(fetchAuthor(authorFormData)) + } }) return ( @@ -129,8 +135,8 @@ export default function Course() {
Ваш викладач -
- +
+