import React, { useState } from 'react'; import { observer } from 'mobx-react'; import { Box, Grid, Text } from '@chakra-ui/react'; /** * Types */ import { CourseStatusEnum } from '@educt/enums'; import { CourseItemPropsType } from './CourseItem'; /** * Components */ import DeleteCourseDialog from '@educt/components/Dialogs/DeleteCourseDialog'; import LoadingList from '@educt/components/LoadingList'; /** * Hooks */ import { useEffect } from 'react'; import { useSetCourseStatus } from '@educt/hooks/queries'; import { useRootStore } from '@educt/hooks/useRootStore'; import { useDisclosure } from '@chakra-ui/hooks'; /** * Contexts */ import { ICourse } from '@educt/interfaces'; type CourseListPropsType = { render: React.FC; }; const CourseList: React.FC = ({ render: Item }) => { const { userStore: { me }, courseStore, pageStore: { coursesStore }, } = useRootStore(); const [deleting, setDeleting] = useState | null>(null); const { onOpen: onOpenDeleteDialog, onClose: onCloseDeleteDialog, isOpen: isOpenDeleteDialog } = useDisclosure(); const { setCourseStatus } = useSetCourseStatus(); const { courses, isLoading } = courseStore; const handleDeleteCourse = (course: Pick) => { setDeleting(course); onOpenDeleteDialog(); }; /** * Fetch courses handler */ useEffect(() => { if (me !== null) { courseStore.loadCourses({ category_id: coursesStore.selectedCategory?.id, status: me.isAdmin || me.isTeacher ? coursesStore.showedStatus : CourseStatusEnum.PUBLISHED, }); } }, [courseStore, coursesStore.selectedCategory, coursesStore.showedStatus]); if (courses === null || isLoading) return ; return ( {deleting && } {courses.length !== 0 ? ( {courses.map(course => ( handleDeleteCourse({ id: course.id, title: course.title })} onSetStatus={setCourseStatus} /> ))} ) : ( There are no courses  {!coursesStore.selectedCategory && !coursesStore.showedStatus ? 'in the system yet' : `in this category${coursesStore.showedStatus ? ' or with this status' : ''}`} . )} ); }; export default observer(CourseList);