freshyo/apps/admin-ui/app/(drawer)/dashboard/slots/add.tsx
2026-10-07 00:27:11 +05:30

59 lines
2 KiB
TypeScript

import React from 'react';
import { View, Text } from 'react-native';
import { AppContainer } from 'common-ui';
import SlotForm from '@/components/SlotForm';
import { useRouter, useLocalSearchParams } from 'expo-router';
import { trpc } from '@/src/trpc-client';
export default function AddSlot() {
const router = useRouter();
const { baseslot, dts, fts } = useLocalSearchParams();
const baseSlotId = baseslot ? parseInt(baseslot as string) : null;
// e2e hook: SlotForm accepts initial times but nothing passed them, so the
// screen always opened with null times and e2e had to fight the native
// picker (which cannot set time-of-day). Deep links can carry the exact
// delivery/freeze instants as ISO strings: ...?dts=<ISO>&fts=<ISO>.
// URL-encoding collapses colons in ISO strings to %3A, so normalize.
const parseInstant = (raw?: string | string[]): Date | null => {
const value = Array.isArray(raw) ? raw[0] : raw
if (!value) return null
const date = new Date(value.replace(/%3A/gi, ':'))
return isNaN(date.getTime()) ? null : date
}
const initialDeliveryTime = parseInstant(dts)
const initialFreezeTime = parseInstant(fts)
const { refetch } = trpc.admin.slots.getAll.useQuery();
const { data: baseSlotData, isLoading } = trpc.admin.slots.getSlotById.useQuery(
{ id: baseSlotId! },
{ enabled: !!baseSlotId }
);
const handleSlotAdded = () => {
refetch();
router.back();
};
if (isLoading && baseSlotId) {
return (
<AppContainer>
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Loading base slot...</Text>
</View>
</AppContainer>
);
}
return (
<AppContainer>
<SlotForm
onSlotAdded={handleSlotAdded}
initialDeliveryTime={initialDeliveryTime}
initialFreezeTime={initialFreezeTime}
initialProductIds={baseSlotData?.slot?.products?.map((p: any) => p.id) || []}
initialGroupIds={baseSlotData?.slot?.groupIds || []}
/>
</AppContainer>
);
}