'use client'; import React, { FC, useEffect, useState } from 'react'; import { Modal, Button, message } from 'antd'; import { CloseOutlined, DeleteOutlined } from '@ant-design/icons'; import dayjs from 'dayjs'; import { i18nText } from '../../i18nKeys'; import { AUTH_TOKEN_KEY } from '../../constants/common'; import { UTC_LIST } from '../../constants/time'; import { MapWorkingTime, ScheduleDTO } from '../../types/schedule'; import { WEEK_DAY, formattedSchedule, getNewTime, getTimeZoneOffset, getTimeString, formattedTimeByOffset, formattedWorkList } from '../../utils/time'; import { useLocalStorage } from '../../hooks/useLocalStorage'; import { setSchedule } from '../../actions/profile'; import { CustomSelect } from '../view/CustomSelect'; import { CustomTimePicker } from '../view/CustomTimePicker'; import { LinkButton } from '../view/LinkButton'; import { OutlinedButton } from '../view/OutlinedButton'; type EditExpertScheduleModalProps = { open: boolean; handleCancel: () => void; locale: string; data?: ScheduleDTO; refresh: () => void; }; const DEFAULT_WORK: MapWorkingTime = { startDay: '' }; export const EditExpertScheduleModal: FC = ({ open, handleCancel, locale, data, refresh, }) => { const defaultTimeZone = dayjs().format('Z'); const [jwt] = useLocalStorage(AUTH_TOKEN_KEY, ''); const [timeZone, setTimeZone] = useState(defaultTimeZone); const [workList, setWorkList] = useState([DEFAULT_WORK]); const [loading, setLoading] = useState(false); useEffect(() => { if (open && data?.workingTimes && data.workingTimes.length > 0) { setWorkList(formattedSchedule(data.workingTimes, timeZone)); } }, [open]); const onSave = () => { const workingTimes = formattedWorkList(workList, timeZone); setLoading(true); setSchedule(locale, jwt, { workingTimes }) .then(() => { handleCancel(); refresh(); }) .catch(() => { message.error('Не удалось сохранить расписание'); }) .finally(() => { setLoading(false); }) }; const addWorkingHours = () => { setWorkList([ ...workList, DEFAULT_WORK ]); }; const deleteWorkingHours = (index: number) => { setWorkList(workList.filter((work, i) => i !== index)); }; const onChangeWeekDay = (val: string, index: number) => { setWorkList(workList.map((work, i) => { if (i === index) { return { ...work, startDay: val } } return work; })); }; const onChangeTime = (time: string, index: number, start?: boolean) => { setWorkList(workList.map((work, i) => { if (i === index) { const timeMin = getNewTime(time); let res; if (start) { res = { startTimeMin: timeMin } } else { res = { endTimeMin: timeMin } } return { ...work, ...res } } return work; })); }; const onChangeTimeZone = (newTimeZone: string) => { const offset = getTimeZoneOffset(timeZone, newTimeZone); setTimeZone(newTimeZone); setWorkList(workList.map((work) => formattedTimeByOffset(work, offset))); } return ( } >
{i18nText('schedule', locale)}
{`${i18nText('yourTimezone', locale)}: ${defaultTimeZone}`}
({ value, label: value }))} onChange={(val) => onChangeTimeZone(val)} />

{i18nText('workTime', locale)}

{workList.length === 1 ? workList.map(({ startDay, startTimeMin, endTimeMin }, index) => (
)) : null} {workList.length > 1 ? workList.map(({ startDay, startTimeMin, endTimeMin }, index) => (
({ value, label: i18nText(value, locale) }))} onChange={(val) => onChangeWeekDay(val, index)} /> onChangeTime(timeString, index, true)} /> onChangeTime(timeString, index)} /> } onClick={() => deleteWorkingHours(index)} />
)) : null}
{i18nText('addWorkingHours', locale)}
); };