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, } from '@chakra-ui/react' import { AddIcon } from '@chakra-ui/icons' import { useTranslation } from 'react-i18next' import { dateToCalendarFormat } from '../../../utils/time' import { Lesson } from '../../../__data__/model' import { ErrorSpan } from '../style' interface NewLessonForm { name: string date: string } interface LessonFormProps { 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, }: LessonFormProps) => { const { t } = useTranslation() const getNearestTimeSlot = () => { const now = new Date(); const minutes = now.getMinutes(); if (minutes < 30) { // Округляем до начала текущего часа now.setMinutes(0, 0, 0); } else { // Округляем до начала следующего часа now.setHours(now.getHours() + 1); now.setMinutes(0, 0, 0); } return dateToCalendarFormat(now.toISOString()); }; const { control, handleSubmit, reset, formState: { errors }, } = useForm({ defaultValues: (lesson && { ...lesson, date: dateToCalendarFormat(lesson.date), }) || { name: '', date: getNearestTimeSlot(), }, }) return ( {title} { reset() onCancel() }} />
( {t('journal.pl.lesson.form.date')} {errors.date ? ( {errors.date?.message} ) : ( {t('journal.pl.lesson.form.dateTime')} )} )} /> ( {t('journal.pl.lesson.form.title')} {errors.name && ( {errors.name.message} )} )} /> {error && {error}}
) }