Skip to content

Commit

Permalink
feat(profile): implement 'my courses' page in user profile
Browse files Browse the repository at this point in the history
  • Loading branch information
danilych committed Feb 11, 2024
1 parent 65c1795 commit 767a264
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 6 deletions.
25 changes: 25 additions & 0 deletions app/redux/slices/courses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ export const fetchBoughtCourse = createAsyncThunk(
}
)

export const fetchAuthorCourse = createAsyncThunk(
'course/fetchAuthorCourse',
async (params: any) => {
const { data } = await instance.post('/api/user/GetAuthorsCourses', params)

return data
}
)

const initialState = {
courses: {
items: [],
Expand Down Expand Up @@ -94,6 +103,22 @@ const coursesSlice = createSlice({
builder.addCase(fetchBoughtCourse.rejected, (state, action) => {
state.courses.status = 'error'

// @ts-ignore
state.courses.items = null
})
builder.addCase(fetchAuthorCourse.pending, (state, action) => {
state.courses.status = 'loading'

// @ts-ignore
state.courses.items = null
})
builder.addCase(fetchAuthorCourse.fulfilled, (state, action) => {
state.courses.status = 'loaded'
state.courses.items = action.payload.data
})
builder.addCase(fetchAuthorCourse.rejected, (state, action) => {
state.courses.status = 'error'

// @ts-ignore
state.courses.items = null
})
Expand Down
3 changes: 2 additions & 1 deletion app/routes/my-account.courses.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function Courses() {

useEffect(() => {

Check warning on line 37 in app/routes/my-account.courses.tsx

View workflow job for this annotation

GitHub Actions / pipeline (21.x)

React Hook useEffect contains a call to 'setIsCoursesLoading'. Without a list of dependencies, this can lead to an infinite chain of updates. To fix this, pass [courses.status] as a second argument to the useEffect Hook
if (courses.status === 'loaded') setIsCoursesLoading(false)

if (courses.status === 'loading') setIsCoursesLoading(true)
})

Expand All @@ -54,6 +54,7 @@ export default function Courses() {
</div>
) : (
<div className="mt-[42px] w-[1500px] mx-auto grid grid-cols-3 gap-2">

{/* @ts-ignore */}
{courses.items.map((course: any) => (
<CourseCard key={course.id} id={course.id} picturePath={course.picturePath} name={course.name} price={course.price} rating={course.rating} />
Expand Down
60 changes: 55 additions & 5 deletions app/routes/my-account.my-courses.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,78 @@
import { ThunkDispatch } from '@reduxjs/toolkit'
import { type V2_MetaFunction } from '@remix-run/node'
// import { useNavigate } from '@remix-run/react'
import { useEffect } from 'react'
import { useSelector } from 'react-redux'
import { Link } from '@remix-run/react'
import { AddCourseCard } from 'assets/images'
import { Spinner } from 'flowbite-react'
import { useEffect, useState } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { CourseCard } from '~/entities'
import { selectIsAuth } from '~/redux/slices/auth'
import { fetchAuthorCourse } from '~/redux/slices/courses'
import { AccountMenu } from '~/widgets'

export const meta: V2_MetaFunction = () => {
return [{ title: 'Mentohub | My Account' }]
}

export default function MyCourses() {
// const navigate = useNavigate()

const isAuth = useSelector(selectIsAuth)

const [isCoursesLoading, setIsCoursesLoading] = useState(true)

const dispatch = useDispatch<ThunkDispatch<any, any, any>>()

// @ts-ignore
const { courses } = useSelector(state => state.courses)

useEffect(() => {
if (!isAuth) {
// navigate('/')
}

const userId = window.localStorage.getItem('userId')
let requestData = new FormData()
requestData.append('authorID', userId as string)

dispatch(fetchAuthorCourse(requestData))
}, [])

useEffect(() => {
if (courses.status === 'loaded') setIsCoursesLoading(false)

if (courses.status === 'loading') setIsCoursesLoading(true)
})

return (
<div className="bg-white min-h-screen pb-[92px] mt-[68px]">
<AccountMenu page="my-courses" />

{isCoursesLoading ? (
<div className="w-full h-[570px]">
<Spinner
className="absolute top-1/2 left-1/2 mt-[-50px] ml-[-50px]"
aria-label="Default status example"
size="lg"
/>
</div>
) : (
<div className="mt-[42px] w-[1500px] mx-auto grid grid-cols-3 gap-2">
<Link to="/create-course">
<img className="h-[570px] w-[484px]" src={AddCourseCard} alt="" />
</Link>

{/* @ts-ignore */}
{courses.items.map((course: any) => (
<CourseCard
key={course.id}
id={course.id}
picturePath={course.picturePath}
name={course.name}
price={course.price}
rating={course.rating}
/>
))}
</div>
)}
</div>
)
}
Binary file added assets/images/add-course-card.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions assets/images/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export { default as Advantages3 } from './advantages-3.png'
export { default as JoinUsImage } from './join-us.png'
export { default as AddImageCover } from './add-image-cover.png'
export { default as EmptyAvatar } from './empty-avatar.png'
export { default as AddCourseCard } from './add-course-card.png'

0 comments on commit 767a264

Please sign in to comment.