import React, { useEffect, useState, useRef, useMemo } from 'react' import { useParams, Link } from 'react-router-dom' import dayjs from 'dayjs' import QRCode from 'qrcode' import { sha256 } from 'js-sha256' import { getConfigValue, getNavigationsValue } from '@brojs/cli' import { Box, Breadcrumb, BreadcrumbItem, BreadcrumbLink, Container, VStack, Heading, Stack, } from '@chakra-ui/react' import { api } from '../__data__/api/api' import { User } from '../__data__/model' import { UserCard } from '../components/user-card' import { QRCanvas, StudentList, BreadcrumbsWrapper, } from './style' export function getGravatarURL(email, user) { if (!email) return void 0 const address = String(email).trim().toLowerCase() const hash = sha256(address) // Grab the actual image URL return `https://www.gravatar.com/avatar/${hash}?d=robohash` } const LessonDetail = () => { const { lessonId, courseId } = useParams() const canvRef = useRef(null) const { isFetching, data: accessCode, isSuccess, refetch, } = api.useCreateAccessCodeQuery( { lessonId }, { pollingInterval: Number(getConfigValue('journal.polling-interval')) || 3000, skipPollingIfUnfocused: true, }, ) const AllStudents = api.useCourseAllStudentsQuery(courseId) const [manualAdd, manualAddRqst] = api.useManualAddStudentMutation() const userUrl = useMemo( () => `${location.origin}/journal/u/${lessonId}/${accessCode?.body?._id}`, [accessCode, lessonId], ) useEffect(() => { if (manualAddRqst.isSuccess) { refetch() } }, [manualAddRqst.isSuccess]) useEffect(() => { if (!isFetching && isSuccess) { QRCode.toCanvas( canvRef.current, userUrl, { width: 600 }, function (error) { if (error) console.error(error) console.log('success!') }, ) } }, [isFetching, isSuccess]) const studentsArr = useMemo(() => { let allStudents: (User & { present?: boolean })[] = [ ...(AllStudents.data?.body || []), ].map((st) => ({ ...st, present: false })) let presentStudents: (User & { present?: boolean })[] = [ ...(accessCode?.body.lesson.students || []), ] while (allStudents.length && presentStudents.length) { const student = presentStudents.pop() const present = allStudents.find((st) => st.sub === student.sub) if (present) { present.present = true } else { allStudents.push({ ...student, present: true }) } } return allStudents.sort((a, b) => (a.present ? -1 : 1)) }, [accessCode?.body, AllStudents.data]) return ( <> Журнал Курс Лекция Тема занятия: {accessCode?.body?.lesson?.name} {dayjs(accessCode?.body?.lesson?.date).format('DD MMMM YYYYг.')}{' '} Отмечено - {accessCode?.body?.lesson?.students?.length}{' '} {AllStudents.isSuccess ? `/ ${AllStudents?.data?.body?.length}` : ''}{' '} человек {studentsArr.map((student) => ( manualAdd({ lessonId, user })} /> ))} ) } export default LessonDetail