diff --git a/src/index.tsx b/src/index.tsx index f8f8087..023fe23 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,3 +1,4 @@ +/* eslint-disable react/display-name */ import React from 'react'; import ReactDOM from 'react-dom/client'; diff --git a/src/pages/lesson-list/components/item.tsx b/src/pages/lesson-list/components/item.tsx new file mode 100644 index 0000000..9674d7c --- /dev/null +++ b/src/pages/lesson-list/components/item.tsx @@ -0,0 +1,144 @@ +import React, { useEffect, useRef, useState } from 'react' +import dayjs from 'dayjs' +import { Link } from 'react-router-dom' +import { getNavigationsValue, getFeatures } from '@brojs/cli' +import { + Button, + Tr, + Td, + Menu, + MenuButton, + MenuItem, + MenuList, + useToast, +} from '@chakra-ui/react' +import { EditIcon } from '@chakra-ui/icons' + +import { qrCode } from '../../../assets' + +import { LessonForm } from './lessons-form' +import { api } from '../../../__data__/api/api' + +const features = getFeatures('journal') +const groupByDate = features?.['group.by.date'] + +type ItemProps = { + id: string + date: string + name: string + isTeacher: boolean + courseId: string + setlessonToDelete(): void + students: unknown[] +} + +export const Item: React.FC = ({ + id, + date, + name, + isTeacher, + courseId, + setlessonToDelete, + students, +}) => { + const [edit, setEdit] = useState(false) + const toastRef = useRef(null) + const toast = useToast() + const [updateLesson, updateLessonRqst] = api.useUpdateLessonMutation() + const createdLessonRef = useRef(null) + + const onSubmit = (lessonData) => { + toastRef.current = toast({ + title: 'Отправляем', + status: 'loading', + duration: 9000, + }) + createdLessonRef.current = lessonData + if (navigator.onLine) { + updateLesson(lessonData) + } else { + toast.update(toastRef.current, { + title: 'Отсутствует интернет', + status: 'error', + duration: 3000 + }) + } + } + + useEffect(() => { + if (updateLessonRqst.isSuccess) { + const toastProps = { + title: 'Лекция Обновлена', + description: `Лекция ${createdLessonRef.current?.name} успешно обновлена`, + status: 'success' as const, + duration: 9000, + isClosable: true, + } + if (toastRef.current) toast.update(toastRef.current, toastProps) + else toast(toastProps) + setEdit(false) + } + }, [updateLessonRqst.isSuccess]) + + if (edit && isTeacher) { + return ( + + + { + setEdit(false) + }} + lesson={{ id, name, date }} + title={'Редактирование лекции'} + nameButton={'Сохранить'} + /> + + + ) + } + + return ( + + {isTeacher && ( + + + + + + )} + + {dayjs(date).format(groupByDate ? 'HH:mm' : 'HH:mm DD.MM.YY')} + + {name} + {isTeacher && ( + + {!edit && ( + + + + + + { + setEdit(true) + }} + > + Edit + + Delete + + + )} + {edit && } + + )} + {students.length} + + ) +} diff --git a/src/pages/lesson-list/components/lesson-items.tsx b/src/pages/lesson-list/components/lesson-items.tsx new file mode 100644 index 0000000..b78decc --- /dev/null +++ b/src/pages/lesson-list/components/lesson-items.tsx @@ -0,0 +1,45 @@ +import React from 'react' +import dayjs from 'dayjs' +import { + Tr, + Td, +} from '@chakra-ui/react' + +import { Lesson } from '../../../__data__/model' + +import { Item } from './item' + +type LessonItemProps = { + date: string + lessons: Lesson[] + isTeacher: boolean + courseId: string + setlessonToDelete(lesson: Lesson): void +} + +export const LessonItems: React.FC = ({ + date, + lessons, + isTeacher, + courseId, + setlessonToDelete, +}) => ( + <> + {date && ( + + + {dayjs(date).format('DD MMMM YYYY')} + + + )} + {lessons.map((lesson) => ( + setlessonToDelete(lesson)} + courseId={courseId} + isTeacher={isTeacher} + /> + ))} + +) diff --git a/src/pages/lesson-list/components/lessons-form.tsx b/src/pages/lesson-list/components/lessons-form.tsx index 7c0cf88..b1ead84 100644 --- a/src/pages/lesson-list/components/lessons-form.tsx +++ b/src/pages/lesson-list/components/lessons-form.tsx @@ -1,19 +1,19 @@ import React from 'react' import { useForm, Controller } from 'react-hook-form' import { - Box, - Card, - CardBody, - CardHeader, - Heading, - Button, - CloseButton, - VStack, - FormControl, - FormLabel, - FormHelperText, - FormErrorMessage, - Input, + Box, + Card, + CardBody, + CardHeader, + Heading, + Button, + CloseButton, + VStack, + FormControl, + FormLabel, + FormHelperText, + FormErrorMessage, + Input, } from '@chakra-ui/react' import { AddIcon } from '@chakra-ui/icons' @@ -22,116 +22,119 @@ import { Lesson } from '../../../__data__/model' import { ErrorSpan } from '../style' interface NewLessonForm { - name: string; - date: string; + name: string + date: string } interface LessonFormProps { - lesson?: Partial - isLoading: boolean - onCancel: () => void - onSubmit: (lesson: Lesson) => void - error?: string - title: string - nameButton: string + lesson?: Partial + isLoading: boolean + onCancel: () => void + onSubmit: (lesson: Lesson) => void + error?: string + title: string + nameButton: string } export const LessonForm = ({ - lesson, - isLoading, - onCancel, - onSubmit, - error, - title, - nameButton, + lesson, + isLoading, + onCancel, + onSubmit, + error, + title, + nameButton, }: LessonFormProps) => { - const { - control, - handleSubmit, - reset, - formState: { errors }, - } = useForm({ - defaultValues: (lesson && { ...lesson, date: dateToCalendarFormat(lesson.date) }) || { - name: '', - date: dateToCalendarFormat(), - }, - }) - - return ( - - + const { + control, + handleSubmit, + reset, + formState: { errors }, + } = useForm({ + defaultValues: (lesson && { + ...lesson, + date: dateToCalendarFormat(lesson.date), + }) || { + name: '', + date: dateToCalendarFormat(), + }, + }) + + return ( + + - {title} + {title} { + ml="auto" + onClick={() => { reset() onCancel() - }} + }} /> - - + +
- - ( - - Дата - + ( + + Дата + + {errors.date ? ( + {errors.date?.message} + ) : ( + Укажите дату и время лекции + )} + + )} /> - {errors.date ? ( - {errors.date?.message} - ) : ( - Укажите дату и время лекции - )} - - )} - /> - - ( - - Название новой лекции: - ( + + Название новой лекции: + + {errors.name && ( + {errors.name.message} + )} + + )} /> - {errors.name && ( - {errors.name.message} - )} - - )} - /> - - - - - - {error && {error}} + + + + + + {error && {error}}
-
-
- ) + + + ) } diff --git a/src/pages/lesson-list/lesson-list.tsx b/src/pages/lesson-list/lesson-list.tsx index 2a7c1f7..d5ddd3d 100644 --- a/src/pages/lesson-list/lesson-list.tsx +++ b/src/pages/lesson-list/lesson-list.tsx @@ -1,9 +1,4 @@ -import React, { - useEffect, - useMemo, - useRef, - useState, -} from 'react' +import React, { useEffect, useMemo, useRef, useState } from 'react' import dayjs from 'dayjs' import { Link, useParams } from 'react-router-dom' import { getNavigationsValue, getFeatures } from '@brojs/cli' @@ -22,12 +17,7 @@ import { Tr, Th, Tbody, - Td, - Menu, - MenuButton, - MenuItem, Text, - MenuList, AlertDialog, AlertDialogBody, AlertDialogContent, @@ -35,18 +25,18 @@ import { AlertDialogHeader, AlertDialogOverlay, } from '@chakra-ui/react' -import { AddIcon, EditIcon } from '@chakra-ui/icons' +import { AddIcon } from '@chakra-ui/icons' import { useAppSelector } from '../../__data__/store' import { api } from '../../__data__/api/api' import { isTeacher } from '../../utils/user' -import { qrCode } from '../../assets' import { Lesson } from '../../__data__/model' import { XlSpinner } from '../../components/xl-spinner' import { LessonForm } from './components/lessons-form' -import { BreadcrumbsWrapper } from './style' import { Bar } from './components/bar' +import { LessonItems } from './components/lesson-items' +import { BreadcrumbsWrapper } from './style' const features = getFeatures('journal') @@ -67,7 +57,10 @@ const LessonList = () => { const toastRef = useRef(null) const createdLessonRef = useRef(null) const [editLesson, setEditLesson] = useState(null) - const sorted = useMemo(() => [...(data?.body || [])]?.sort((a, b) => a.date > b.date ? 1 : -1), [data, data?.body]) + const sorted = useMemo( + () => [...(data?.body || [])]?.sort((a, b) => (a.date > b.date ? 1 : -1)), + [data, data?.body], + ) const lessonCalc = useMemo(() => { if (!isSuccess) { @@ -95,7 +88,7 @@ const LessonList = () => { } } - return lessonsData.sort((a, b) => a.date < b.date? 1 : -1) + return lessonsData.sort((a, b) => (a.date < b.date ? 1 : -1)) }, [groupByDate, isSuccess, sorted]) const onSubmit = (lessonData) => { @@ -153,8 +146,8 @@ const LessonList = () => { if (crLQuery.isSuccess) { const toastProps = { title: 'Лекция создана', - description: `Лекция ${createdLessonRef.current.name} успешно создана`, - status: 'success' as 'success', + description: `Лекция ${createdLessonRef.current?.name} успешно создана`, + status: 'success' as const, duration: 9000, isClosable: true, } @@ -168,8 +161,8 @@ const LessonList = () => { if (updateLessonRqst.isSuccess) { const toastProps = { title: 'Лекция Обновлена', - description: `Лекция ${createdLessonRef.current.name} успешно обновлена`, - status: 'success' as 'success', + description: `Лекция ${createdLessonRef.current?.name} успешно обновлена`, + status: 'success' as const, duration: 9000, isClosable: true, } @@ -180,7 +173,7 @@ const LessonList = () => { }, [updateLessonRqst.isSuccess]) if (isLoading) { - return ; + return } return ( @@ -213,7 +206,7 @@ const LessonList = () => { colorScheme="red" loadingText="" isLoading={deletingRqst.isLoading} - onClick={() => deleteLesson(lessonToDelete._id)} + onClick={() => deleteLesson(lessonToDelete.id)} ml={3} > Delete @@ -240,7 +233,7 @@ const LessonList = () => { {showForm ? ( { @@ -265,7 +258,7 @@ const LessonList = () => { )} )} - {barFeature && sorted?.length && ( + {barFeature && sorted?.length > 1 && ( ({ @@ -285,7 +278,7 @@ const LessonList = () => { )} - Дата + {groupByDate ? 'Время' : 'Дата'} Название {isTeacher(user) && action} @@ -294,56 +287,14 @@ const LessonList = () => { {lessonCalc?.map(({ data: lessons, date }) => ( - - {date && {dayjs(date).format('DD MMMM YYYY')}} - {lessons.map((lesson) => ( - - {isTeacher(user) && ( - - - - - - )} - - {dayjs(lesson.date).format(groupByDate ? 'HH:mm' : 'HH:mm DD.MM.YY')} - - {lesson.name} - {isTeacher(user) && ( - - - - - - - { - setShowForm(true) - setEditLesson(lesson) - }} - > - Edit - - setlessonToDelete(lesson)} - > - Delete - - - - - )} - {lesson.students.length} - - ))} - + ))}