diff --git a/app/data/navigation.ts b/app/data/navigation.ts new file mode 100644 index 0000000..9ccd5c5 --- /dev/null +++ b/app/data/navigation.ts @@ -0,0 +1,30 @@ +export const categories = [ + { + "key": 1, + "value": "Web development" + }, + { + "key": 2, + "value": "DevOps" + }, + { + "key": 3, + "value": "Back-end development" + }, + { + "key": 4, + "value": "Game development" + }, + { + "key": 5, + "value": "UX/UI" + }, + { + "key": 6, + "value": "Fitness" + }, + { + "key": 7, + "value": "Marketing" + } +] \ No newline at end of file diff --git a/app/redux/slices/courses.ts b/app/redux/slices/courses.ts index e8ed95c..94da11e 100644 --- a/app/redux/slices/courses.ts +++ b/app/redux/slices/courses.ts @@ -46,6 +46,15 @@ export const fetchAuthorCourse = createAsyncThunk( } ) +export const searchCourses = createAsyncThunk( + 'course/searchCourses', + async (params: any) => { + const { data } = await instance.post('/Course/Table', params) + + return data + } +) + const initialState = { courses: { items: [], @@ -119,6 +128,22 @@ const coursesSlice = createSlice({ builder.addCase(fetchAuthorCourse.rejected, (state, action) => { state.courses.status = 'error' + // @ts-ignore + state.courses.items = null + }) + builder.addCase(searchCourses.pending, (state, action) => { + state.courses.status = 'loading' + + // @ts-ignore + state.courses.items = null + }) + builder.addCase(searchCourses.fulfilled, (state, action) => { + state.courses.status = 'loaded' + state.courses.items = action.payload.data.courses + }) + builder.addCase(searchCourses.rejected, (state, action) => { + state.courses.status = 'error' + // @ts-ignore state.courses.items = null }) diff --git a/app/routes/courses.$course.tsx b/app/routes/courses.$course.tsx index aa786ec..9e31848 100644 --- a/app/routes/courses.$course.tsx +++ b/app/routes/courses.$course.tsx @@ -36,7 +36,7 @@ export default function Course() { if (course.data != null) { let authorFormData = new FormData() - authorFormData.append('encriptId', (course.data.authorId).toString() as string) + authorFormData.append('encriptId', course.data.authorEncryptedId as string) dispatch(fetchAuthor(authorFormData)) } diff --git a/app/routes/courses._index.tsx b/app/routes/courses._index.tsx new file mode 100644 index 0000000..f8ef9f4 --- /dev/null +++ b/app/routes/courses._index.tsx @@ -0,0 +1,78 @@ +import type { ThunkDispatch } from '@reduxjs/toolkit' +import { useEffect, useState } from 'react' +import { useDispatch, useSelector } from 'react-redux' +import { searchCourses } from '~/redux/slices/courses' +import { Header3 } from '~/shared' +import { Spinner } from 'flowbite-react' +import { CourseCard } from '~/entities' + +export default function Courses() { + const dispatch = useDispatch>() + + const [categoryValue, setCategoryValue] = useState('Loading') + + // @ts-ignore + const { courses } = useSelector(state => state.courses) + + const [isCoursesLoading, setIsCoursesLoading] = useState(true) + + useEffect(() => { + const value = window.localStorage.getItem('categoryValue') + const key = window.localStorage.getItem('CategoryID') + + setCategoryValue(value!) + + const requestValue = { + CategoryID: key, + SearchText: '', + LanguageID: '-1', + Rate: '-1', + Level: '-1', + PriceFrom: '0', + PriceTo: '500', + CurrentPage: '0', + } + + console.log(courses.items) + + dispatch(searchCourses(requestValue)) + }, []) + + useEffect(() => { + if (courses.status === 'loaded') setIsCoursesLoading(false) + + if (courses.status === 'loading') setIsCoursesLoading(true) + }) + + return ( +
+ {isCoursesLoading ? ( +
+ +
+ ) : ( +
+ Pезультати за запитом “{categoryValue}” + +
+ {/* @ts-ignore */} + {courses.items.map((course: any) => ( + + ))} +
+
+ )} +
+ ) +} diff --git a/app/widgets/navigation.tsx b/app/widgets/navigation.tsx index 6d7df2d..7f85403 100644 --- a/app/widgets/navigation.tsx +++ b/app/widgets/navigation.tsx @@ -6,13 +6,19 @@ import { useSelector } from 'react-redux' import { selectIsAuth } from '~/redux/slices/auth' import { Dropdown } from 'flowbite-react' import { useEffect } from 'react' +import { categories } from '~/data/navigation' export default function Navigation() { const isAuth = useSelector(selectIsAuth) - useEffect(() => { - - }, []) + useEffect(() => {}, []) + + async function storeCategory(id: any, value: any) { + if (typeof window !== 'undefined') { + window.localStorage.setItem('CategoryID', id) + window.localStorage.setItem('categoryValue', value) + } + } console.log('isAuth: ', isAuth) return ( @@ -32,8 +38,15 @@ export default function Navigation() { } inline > - Українська - Англійська + {categories.map((category, index) => ( + storeCategory(category.key, category.value)} + href="/courses" + > + {category.value} + + ))}