Vlad Chikalkin ccfc65ca9b
Fix/bugs features pt 2 (#58)
* refactor(profile): comment out change role feature

* refactor(orders): update OrderServices and ServiceSelect components to utilize ServiceCard, and enhance service fields with duration in GraphQL types

* refactor(schedule): implement forbidden order states to disable editing slots with active orders

* fix(deploy): update SSH configuration to use dynamic port from secrets for improved flexibility

* refactor(api/orders): simplify order creation logic by removing unnecessary validations and improving error handling

* refactor(contact-row): replace role display logic with useIsMaster hook for improved clarity

* refactor(profile/orders-list): update header text from "Общие записи" to "Недавние записи" for better clarity
gql: GetOrders add sort slot.date:desc

* refactor(profile/orders-list): enhance OrderCard component by adding avatarSource prop based on user role

* feat(order-form): implement date selection with event highlighting and monthly view for available time slots

* refactor(i18n/config): update timeZone from 'Europe/Amsterdam' to 'Europe/Moscow'

* refactor(order-form/datetime-select): enhance date selection logic to include slot availability check

* refactor(datetime-format): integrate dayjs timezone support with default Moscow timezone for date and time formatting

* fix(contact-row): replace useIsMaster hook with isCustomerMaster utility for role display logic

* refactor(service-card): replace formatTime with getMinutes for duration display

* refactor(order-datetime): update date and time handling to use datetime_start and datetime_end for improved consistency

* refactor(profile): streamline profile and slot pages by integrating session user retrieval and updating booking logic with BookButton component

* fix(navigation): append query parameter to bottom-nav links and enhance back navigation logic in success page
2025-07-18 17:11:43 +03:00

151 lines
4.0 KiB
TypeScript

'use client';
import { useAvailableTimeSlotsQuery } from '@/hooks/api/slots';
import { useOrderStore } from '@/stores/order';
import { Button } from '@repo/ui/components/ui/button';
import { Calendar } from '@repo/ui/components/ui/calendar';
import { formatTime } from '@repo/utils/datetime-format';
import dayjs from 'dayjs';
import { useState } from 'react';
export function DateSelect() {
const selectedDate = useOrderStore((store) => store.date);
const setDate = useOrderStore((store) => store.setDate);
const setTime = useOrderStore((store) => store.setTime);
const setSlot = useOrderStore((store) => store.setSlotId);
const masterId = useOrderStore((store) => store.masterId);
const serviceId = useOrderStore((store) => store.serviceId);
const [selectedMonthDate, setSelectedMonthDate] = useState(new Date());
const { data: { slots } = {} } = useAvailableTimeSlotsQuery(
{
filters: {
datetime_start: {
gte: dayjs(selectedMonthDate).startOf('month').startOf('day').toISOString(),
lte: dayjs(selectedMonthDate).endOf('month').endOf('day').toISOString(),
},
master: {
documentId: {
eq: masterId,
},
},
},
},
{
service: {
documentId: {
eq: serviceId,
},
},
},
);
return (
<Calendar
className="bg-background"
disabled={(date) => {
return (
dayjs().isAfter(dayjs(date), 'day') ||
!slots?.some((slot) => dayjs(slot?.datetime_start).isSame(date, 'day'))
);
}}
mode="single"
onMonthChange={(date) => setSelectedMonthDate(date)}
onSelect={(date) => {
if (date) setDate(date);
setTime(null);
setSlot(null);
}}
selected={selectedDate}
/>
);
}
export function DateTimeSelect() {
return (
<div className="space-y-4">
<DateSelect />
<TimeSelect />
</div>
);
}
export function TimeSelect() {
const masterId = useOrderStore((store) => store.masterId);
const date = useOrderStore((store) => store.date);
const serviceId = useOrderStore((store) => store.serviceId);
const { data: { times } = {}, isLoading } = useAvailableTimeSlotsQuery(
{
filters: {
datetime_start: {
gte: dayjs(date).startOf('day').toISOString(),
lte: dayjs(date).endOf('day').toISOString(),
},
master: {
documentId: {
eq: masterId,
},
},
},
},
{
service: {
documentId: {
eq: serviceId,
},
},
},
);
if (isLoading || !times) return null;
const getHour = (isoString: string) => dayjs(isoString).hour();
const morning = times.filter(({ time }) => getHour(time) < 12);
const afternoon = times?.filter(({ time }) => {
const hour = getHour(time);
return hour >= 12 && hour < 18;
});
const evening = times?.filter(({ time }) => getHour(time) >= 18);
return (
<div className="space-y-2">
<TimeSlotsButtons times={morning} title="Утро" />
<TimeSlotsButtons times={afternoon} title="День" />
<TimeSlotsButtons times={evening} title="Вечер" />
</div>
);
}
function TimeSlotsButtons({
times,
title,
}: Readonly<{ times: Array<{ slotId: string; time: string }>; title: string }>) {
const setTime = useOrderStore((store) => store.setTime);
const setSlot = useOrderStore((store) => store.setSlotId);
if (!times.length) return null;
return (
<div className="space-y-2">
<h2 className="text-lg font-semibold">{title}</h2>
<div className="grid grid-cols-3 gap-2">
{times.map(({ slotId, time }) => (
<Button
className="mb-2"
key={time.toString()}
onClick={() => {
setTime(time);
setSlot(slotId);
}}
variant="outline"
>
{formatTime(time).user()}
</Button>
))}
</div>
</div>
);
}