'use client'; import React, { MouseEvent, useCallback, useEffect, useState } from 'react'; import { Empty, Space } from 'antd'; import dayjs from 'dayjs'; import 'dayjs/locale/ru'; import 'dayjs/locale/en'; import 'dayjs/locale/de'; import 'dayjs/locale/it'; import 'dayjs/locale/fr'; import 'dayjs/locale/es'; import { RoomsType } from '../../../types/rooms'; import { getRecentRooms, getUpcomingRooms } from '../../../actions/rooms'; import { Loader } from '../../view/Loader'; import { useLocalStorage } from '../../../hooks/useLocalStorage'; import { AUTH_TOKEN_KEY } from '../../../constants/common'; import { usePathname, useRouter } from '../../../navigation'; import { i18nText } from '../../../i18nKeys'; import { CreateRoom } from './CreateRoom'; type RoomsTabsProps = { locale: string; activeTab: RoomsType; }; export const RoomsTabs = ({ locale, activeTab }: RoomsTabsProps) => { const [sort, setSort] = useState(); const [rooms, setRooms] = useState(); const [loading, setLoading] = useState(true); const [errorData, setErrorData] = useState(); const [jwt] = useLocalStorage(AUTH_TOKEN_KEY, ''); const router = useRouter(); const pathname = usePathname(); const fetchData = () => { setErrorData(undefined); setLoading(true); Promise.all([ getUpcomingRooms(locale, jwt), getRecentRooms(locale, jwt) ]) .then(([upcoming, recent]) => { setRooms({ [RoomsType.UPCOMING]: upcoming || [], [RoomsType.RECENT]: recent || [] }); }) .catch((err) => { setErrorData(err); }) .finally(() => { setLoading(false); }); }; useEffect(() => { fetchData(); }, []); const onChangeSort = useCallback((value: string) => { setSort(value); }, [sort]); const onClickSession = (event: MouseEvent, id: number) => { event.stopPropagation(); event.preventDefault(); router.push(`${pathname}/${id}`); }; const getChildren = (list?: any[]) => ( <> {/*
*/}
{list && list?.length > 0 ? list?.map(({ id, scheduledStartAtUtc, scheduledEndAtUtc, title, coach, clients, supervisor, maxClients }) => { const startDate = dayjs(scheduledStartAtUtc).locale(locale); const endDate = dayjs(scheduledEndAtUtc).locale(locale); const today = dayjs().format('YYYY-MM-DD') === startDate.format('YYYY-MM-DD'); return (
) => onClickSession(e, id)}>
{`${coach?.name} ${coach?.surname || ''}`}
{title}
{today ? `${i18nText('today', locale)} ${startDate.format('HH:mm')} - ${endDate.format('HH:mm')}` : `${startDate.format('D MMMM')} ${startDate.format('HH:mm')} - ${endDate.format('HH:mm')}`}
{supervisor && ( <>
{i18nText('room.supervisor', locale)}
{`${supervisor?.name} ${supervisor?.surname || ''}`}
)}
{i18nText('room.members', locale)}
{`${clients.length}/${maxClients}`}
) }) : ( )}
); const tabs = [ { key: RoomsType.UPCOMING, label: ( <> {i18nText('room.upcoming', locale)} {rooms?.upcoming && rooms?.upcoming?.length > 0 ? ({rooms?.upcoming.length}) : null} ), children: getChildren(rooms?.upcoming) }, { key: RoomsType.RECENT, label: i18nText('room.recent', locale), children: getChildren(rooms?.recent) }, { key: RoomsType.NEW, label: i18nText('room.newRoom', locale), children: } ]; return (
{tabs.map(({ key, label }) => ( router.push(`/account/rooms/${key}`)} > {label} ))}
{tabs.filter(({ key }) => key === activeTab)[0].children}
); };