Skip to content

Commit

Permalink
feat(view-course): fetch author data #wip
Browse files Browse the repository at this point in the history
  • Loading branch information
danilych committed Feb 13, 2024
1 parent 4a3d44a commit eb4d971
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
45 changes: 45 additions & 0 deletions app/redux/slices/author.ts
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions app/redux/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ 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: {
courses: courseReducer,
course: courseItemReducer,
auth: authReducer,
user: userReducer,
author: authorReducer,
},
});

Expand Down
14 changes: 10 additions & 4 deletions app/routes/courses.$course.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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))


}, [])

Check warning on line 30 in app/routes/courses.$course.tsx

View workflow job for this annotation

GitHub Actions / pipeline (21.x)

React Hook useEffect has missing dependencies: 'dispatch' and 'params.course'. Either include them or remove the dependency array

useEffect(() => {

Check warning on line 32 in app/routes/courses.$course.tsx

View workflow job for this annotation

GitHub Actions / pipeline (21.x)

React Hook useEffect contains a call to 'setIsPostLoading'. Without a list of dependencies, this can lead to an infinite chain of updates. To fix this, pass [course.status, course.data, dispatch] as a second argument to the useEffect Hook
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 (
Expand Down Expand Up @@ -129,8 +135,8 @@ export default function Course() {
<div className="mt-[92px] w-[1200px]">
<Header3>Ваш викладач</Header3>

<div className='flex flex-row gap-[42px]'>
<img src="" alt="" />
<div className="flex flex-row gap-[42px]">
<img src="" alt="" />
</div>
</div>
</div>
Expand Down

0 comments on commit eb4d971

Please sign in to comment.