mirror of
https://github.com/sergeyyarkov/educt-client.git
synced 2026-10-11 02:09:08 +03:00
feat: rendering courses
This commit is contained in:
parent
5ad94b7b25
commit
2c6534648a
33
src/hooks/queries/course/useFetchCourses.ts
Normal file
33
src/hooks/queries/course/useFetchCourses.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import { ICourse } from '@educt/interfaces';
|
||||
import { CourseServiceInstance } from '@educt/services';
|
||||
import { FetchCoursesParams } from '@educt/types';
|
||||
import useAsync from '@educt/hooks/useAsync';
|
||||
import { useEffect } from 'react';
|
||||
import { useErrorHandler } from 'react-error-boundary';
|
||||
|
||||
const useFetchCourses = (params?: FetchCoursesParams | undefined) => {
|
||||
const handleError = useErrorHandler();
|
||||
|
||||
const fetchCourses = async (params?: FetchCoursesParams | undefined) => {
|
||||
try {
|
||||
const result = await CourseServiceInstance.fetchAll(params);
|
||||
return result.data;
|
||||
} catch (error) {
|
||||
handleError(error);
|
||||
return Promise.reject(error);
|
||||
}
|
||||
};
|
||||
|
||||
const { execute: fetch, ...state } = useAsync<
|
||||
Omit<ICourse, 'teacher' | 'students' | 'lessons'>[],
|
||||
Parameters<typeof fetchCourses>
|
||||
>(fetchCourses);
|
||||
|
||||
useEffect(() => {
|
||||
fetch(params);
|
||||
}, []);
|
||||
|
||||
return { ...state };
|
||||
};
|
||||
|
||||
export { useFetchCourses };
|
||||
@ -14,6 +14,7 @@ import { useUpdateLesson } from './lesson/useUpdateLesson';
|
||||
/**
|
||||
* Course
|
||||
*/
|
||||
import { useFetchCourses } from './course/useFetchCourses';
|
||||
import { useCreateCourse } from './course/useCreateCourse';
|
||||
import { useUpdateCourse } from './course/useUpdateCourse';
|
||||
import { useFetchCourse } from './course/useFetchCourse';
|
||||
@ -40,6 +41,7 @@ import { useUpdateCategory } from './category/useUpdateCategory';
|
||||
export {
|
||||
useLogin,
|
||||
useLogout,
|
||||
useFetchCourses,
|
||||
useCreateCourse,
|
||||
useUpdateCourse,
|
||||
useFetchCourse,
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import React from 'react';
|
||||
import moment from 'moment';
|
||||
import {
|
||||
Box,
|
||||
BoxProps,
|
||||
@ -48,7 +49,7 @@ export const CourseCardCreated: React.FC<{ created: string }> = props => (
|
||||
Created
|
||||
</Text>
|
||||
<Box as='time' verticalAlign='middle' dateTime={props.created}>
|
||||
{props.created}
|
||||
{moment(props.created).format('ll')}
|
||||
</Box>
|
||||
</Box>
|
||||
);
|
||||
@ -86,7 +87,7 @@ export const CourseCardWatch: React.FC<{ id: string }> = props => {
|
||||
|
||||
return (
|
||||
<Box>
|
||||
<Button onClick={() => history.push(`/courses/${props.id}`)} leftIcon={<MdOutlinePlayCircle />} size='sm'>
|
||||
<Button onClick={() => history.push(`/course/${props.id}`)} leftIcon={<MdOutlinePlayCircle />} size='sm'>
|
||||
Watch
|
||||
</Button>
|
||||
</Box>
|
||||
|
||||
@ -20,6 +20,8 @@ import {
|
||||
CourseCardStat,
|
||||
CourseCardList,
|
||||
} from './components';
|
||||
import { observer } from 'mobx-react';
|
||||
import { useFetchCourses } from '@educt/hooks/queries';
|
||||
|
||||
/**
|
||||
* Main page
|
||||
@ -28,8 +30,9 @@ const MainPage: React.FC<IPageProps> = () => {
|
||||
const {
|
||||
userStore: { me },
|
||||
} = useRootStore();
|
||||
const { data: courses } = useFetchCourses({ limit: 3 });
|
||||
|
||||
if (me === null) return <LoadingPage />;
|
||||
if (me === null || courses === null) return <LoadingPage />;
|
||||
|
||||
return (
|
||||
<PageWrapper>
|
||||
@ -39,7 +42,11 @@ const MainPage: React.FC<IPageProps> = () => {
|
||||
<Stat>
|
||||
<StatLabel>Unread messages</StatLabel>
|
||||
<StatContent>
|
||||
<StatNumber>3</StatNumber>
|
||||
<StatNumber>
|
||||
<Link as={ReactRouterLink} to='/messages'>
|
||||
3
|
||||
</Link>
|
||||
</StatNumber>
|
||||
<StatIcon icon={MdOutlineMessage} />
|
||||
</StatContent>
|
||||
</Stat>
|
||||
@ -76,22 +83,24 @@ const MainPage: React.FC<IPageProps> = () => {
|
||||
</Link>
|
||||
</Flex>
|
||||
<CourseCardList>
|
||||
<CourseCard>
|
||||
<CourseCardHeading>
|
||||
<CourseCardTitle>Javascript in a nutshell</CourseCardTitle>
|
||||
<CourseCardCategory>Web Developing</CourseCardCategory>
|
||||
</CourseCardHeading>
|
||||
{courses.map(course => (
|
||||
<CourseCard key={course.id}>
|
||||
<CourseCardHeading borderColor={course.color?.hex}>
|
||||
<CourseCardTitle>{course.title}</CourseCardTitle>
|
||||
<CourseCardCategory>{course.category.title}</CourseCardCategory>
|
||||
</CourseCardHeading>
|
||||
|
||||
<CourseCardContent>
|
||||
<CourseCardCreated created='29 Nov 2022' />
|
||||
<CourseCardLessons count={26} />
|
||||
</CourseCardContent>
|
||||
<CourseCardContent>
|
||||
<CourseCardCreated created={course.created_at} />
|
||||
<CourseCardLessons count={course.lessons_count} />
|
||||
</CourseCardContent>
|
||||
|
||||
<CourseCardFooter>
|
||||
<CourseCardStat students={24} likes={14} />
|
||||
<CourseCardWatch id='123' />
|
||||
</CourseCardFooter>
|
||||
</CourseCard>
|
||||
<CourseCardFooter>
|
||||
<CourseCardStat students={course.students_count} likes={course.likes_count} />
|
||||
<CourseCardWatch id={course.id} />
|
||||
</CourseCardFooter>
|
||||
</CourseCard>
|
||||
))}
|
||||
</CourseCardList>
|
||||
</Box>
|
||||
<Divider />
|
||||
@ -100,4 +109,4 @@ const MainPage: React.FC<IPageProps> = () => {
|
||||
);
|
||||
};
|
||||
|
||||
export default MainPage;
|
||||
export default observer(MainPage);
|
||||
|
||||
@ -24,6 +24,7 @@ class CourseService {
|
||||
params: {
|
||||
status: params?.status,
|
||||
category_id: params?.category_id,
|
||||
limit: params?.limit,
|
||||
},
|
||||
});
|
||||
return result.data;
|
||||
|
||||
8
src/types/index.d.ts
vendored
8
src/types/index.d.ts
vendored
@ -109,15 +109,9 @@ export type CoursesPageContextType = {
|
||||
setCourseStatus: React.Dispatch<React.SetStateAction<CourseStatusEnum | undefined>>;
|
||||
};
|
||||
export type FetchCoursesParams = {
|
||||
/**
|
||||
* Get courses by status
|
||||
*/
|
||||
status?: CourseStatusEnum;
|
||||
|
||||
/**
|
||||
* Get courses with category by id
|
||||
*/
|
||||
category_id?: string;
|
||||
limit?: number;
|
||||
};
|
||||
export type FetchUsersParamsType = {
|
||||
page?: number;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user