import React, { useCallback, useEffect, useState } from 'react'; import { Button, Form, Input, notification } from 'antd'; import dayjs, { Dayjs } from 'dayjs'; import { i18nText } from '../../../i18nKeys'; import { Tag } from '../../../types/tags'; import { Slot } from '../../../types/experts'; import { RoomEdit, RoomEditDTO } from '../../../types/rooms'; import { getRoomById, updateRoom } from '../../../actions/rooms'; import { Loader } from '../../view/Loader'; import { CustomInput } from '../../view/CustomInput'; import { CustomSelect } from '../../view/CustomSelect'; import { CustomSwitch } from '../../view/CustomSwitch'; import { CustomMultiSelect } from '../../view/CustomMultiSelect'; import { CustomDatePicker } from '../../view/CustomDatePicker'; type EditRoomFormProps = { roomId: number, locale: string, jwt: string, mode: 'create' | 'edit'; afterSubmit?: () => void; } type RoomFormState = { title?: string; description?: string; date?: Dayjs; maxCount?: number; startAt?: string; supervisor?: boolean; tags?: number[]; }; export const EditRoomForm = ({ roomId, locale, jwt, mode, afterSubmit }: EditRoomFormProps) => { const [form] = Form.useForm(); const [editingRoom, setEditingRoom] = useState(); const dateValue = Form.useWatch('date', form); const [loading, setLoading] = useState(false); const [fetchLoading, setFetchLoading] = useState(false); useEffect(() => { setFetchLoading(true); getRoomById(locale, jwt, roomId) .then((data) => { setEditingRoom(data); const { item } = data || {}; if (mode === 'edit' && item) { form.setFieldsValue({ title: item.title, description: item.description, date: item?.scheduledStartAtUtc ? dayjs(item.scheduledStartAtUtc) : undefined, maxCount: item.maxClients, startAt: item?.scheduledStartAtUtc, supervisor: item.isNeedSupervisor, tags: item.tagIds || undefined }) } }) .finally(() => { setFetchLoading(false); }) }, []); const getAvailableSlots = useCallback((): string[] => { const dateList = new Set(); if (editingRoom?.availableSlots) { editingRoom.availableSlots.forEach(({ startTime }) => { const [date] = startTime.split('T'); dateList.add(dayjs(date).format('YYYY-MM-DD')); }); return Array.from(dateList); } return []; }, [editingRoom?.availableSlots]); const getTimeOptions = (slots?: Slot[], curDate?: Dayjs) => { const date = curDate ? curDate.format('YYYY-MM-DD') : ''; if (slots && slots?.length && date) { return slots.filter(({ startTime }) => dayjs(startTime).format('YYYY-MM-DD') === date) .map(({ startTime, endTime }) => ({ value: startTime, label: `${dayjs(startTime).format('HH:mm')} - ${dayjs(endTime).format('HH:mm')}` })); } return []; } const getTagsOptions = (tags?: Tag[]) => { if (tags) { return tags.map(({ id, name }) => ({ value: id, label: {name} })) || []; } return []; } const onSubmit = () => { setLoading(true); const { title, description, startAt, maxCount, tags, supervisor } = form.getFieldsValue(); const result: RoomEdit = { ...editingRoom, id: roomId, title, scheduledStartAtUtc: startAt, maxClients: maxCount, isNeedSupervisor: supervisor, tagIds: tags || [] }; if (description) { result.description = description; } updateRoom(locale, jwt, result) .then(() => { afterSubmit && afterSubmit(); }) .catch((err) => { notification.error({ message: 'Error', description: err?.response?.data?.errMessage }); }) .finally(() => { setLoading(false) }); } const disabledDate = (current: Dayjs) => current && !getAvailableSlots().includes(current.format('YYYY-MM-DD')); return (
({ value: i+1, label: i+1 }))} />
); };