diff --git a/ijl.config.js b/ijl.config.js index 86ab287..348b52a 100644 --- a/ijl.config.js +++ b/ijl.config.js @@ -1,27 +1,32 @@ const pkg = require('./package') module.exports = { - apiPath: 'stubs/api', - webpackConfig: { - output: { - publicPath: `/static/${pkg.name}/${process.env.VERSION || pkg.version}/` - } + apiPath: 'stubs/api', + webpackConfig: { + output: { + publicPath: `/static/${pkg.name}/${process.env.VERSION || pkg.version}/`, }, - navigations: { - 'journal.main': '/journal.pl' + }, + navigations: { + 'journal.main': '/journal.pl', + }, + features: { + journal: { + // add your features here in the format [featureName]: { value: string } + 'lesson.bar': { + on: true, + value: '', + key: 'lesson.bar', + }, + 'group.by.date': { + on: true, + value: '', + key: 'group.by.date', + }, }, - features: { - 'journal': { - // add your features here in the format [featureName]: { value: string } - "lesson.bar": { - "on": true, - "value": "", - "key": "lesson.bar" - } - }, - }, - config: { - "journal.back.url": "/api", - "journal.polling-interval": "10000" - } + }, + config: { + 'journal.back.url': '/api', + 'journal.polling-interval': '10000', + }, } diff --git a/src/pages/lesson-list.tsx b/src/pages/lesson-list.tsx index 4ce21d1..7a0a853 100644 --- a/src/pages/lesson-list.tsx +++ b/src/pages/lesson-list.tsx @@ -1,4 +1,9 @@ -import React, { useEffect, useRef, useState } from 'react' +import React, { + useEffect, + useMemo, + useRef, + useState, +} from 'react' import { ResponsiveBar } from '@nivo/bar' import dayjs from 'dayjs' import { Link, useParams } from 'react-router-dom' @@ -52,13 +57,13 @@ import { api } from '../__data__/api/api' import { isTeacher } from '../utils/user' import { qrCode } from '../assets' import { Lesson } from '../__data__/model' -import pkg from '../../package.json' import { ErrorSpan, BreadcrumbsWrapper } from './style' const features = getFeatures('journal') const barFeature = features?.['lesson.bar'] +const groupByDate = features?.['group.by.date'] interface NewLessonForm { name: string @@ -178,7 +183,7 @@ const LessonForm = ({ const LessonList = () => { const { courseId } = useParams() const user = useAppSelector((s) => s.user) - const { data, isLoading, error } = api.useLessonListQuery(courseId) + const { data, isLoading, error, isSuccess } = api.useLessonListQuery(courseId) const [createLesson, crLQuery] = api.useCreateLessonMutation() const [deleteLesson, deletingRqst] = api.useDeleteLessonMutation() const [updateLesson, updateLessonRqst] = api.useUpdateLessonMutation() @@ -189,6 +194,36 @@ const LessonList = () => { const toastRef = useRef(null) const createdLessonRef = useRef(null) const [editLesson, setEditLesson] = useState(null) + const lessonCalc = useMemo(() => { + if (!isSuccess) { + return [] + } + + const sorted = [...data?.body].sort((a, b) => a.date > b.date ? 1 : -1) + + if (!groupByDate) { + return [{ date: '', data: sorted }] + } + + const lessonsData = [] + for (let i = 0; i < sorted.length; i++) { + const element = sorted[i] + const find = lessonsData.find( + (item) => dayjs(element.date).diff(dayjs(item.date), 'day') === 0, + ) + + if (find) { + find.data.push(element) + } else { + lessonsData.push({ + date: element.date, + data: [element], + }) + } + } + + return lessonsData + }, [groupByDate, isSuccess]) const onSubmit = (lessonData) => { toastRef.current = toast({ @@ -397,50 +432,57 @@ const LessonList = () => { - {data?.body?.map((lesson) => ( - - {isTeacher(user) && ( - - - - - - )} - - {dayjs(lesson.date).format('H:mm DD.MM.YY')} - - {lesson.name} - {isTeacher(user) && ( - - - - - - - { - setShowForm(true) - setEditLesson(lesson) - }} + {lessonCalc?.map(({ data: lessons, date }) => ( + + {date && {dayjs(date).format('DD MMMM YYYY')}} + {lessons.map((lesson) => ( + + {isTeacher(user) && ( + + - Edit - - setlessonToDelete(lesson)}> - Delete - - - - - )} - {lesson.students.length} - + + + + )} + + {dayjs(lesson.date).format('H:mm DD.MM.YY')} + + {lesson.name} + {isTeacher(user) && ( + + + + + + + { + setShowForm(true) + setEditLesson(lesson) + }} + > + Edit + + setlessonToDelete(lesson)} + > + Delete + + + + + )} + {lesson.students.length} + + ))} + ))} @@ -452,7 +494,7 @@ const LessonList = () => { export default LessonList -const Bar = ({ data /* see data tab */ }) => ( +const Bar = ({ data }) => (