import React from 'react' import type { SlotSnippetInput } from '@packages/shared' import { Formik, FieldArray } from 'formik' import DateTimeInput from './DateTimeInput' import { p as P, pInput as PInput } from 'web-components' import { trpc } from '@/lib/trpc-client' import ProductsSelector from './ProductsSelector' // Snippet form row — single source in @packages/shared (complete payload) type VendorSnippet = SlotSnippetInput interface SlotFormProps { onSlotAdded?: () => void initialDeliveryTime?: Date | null initialFreezeTime?: Date | null initialIsActive?: boolean slotId?: number initialProductIds?: number[] initialGroupIds?: number[] } export default function SlotForm({ onSlotAdded, initialDeliveryTime, initialFreezeTime, initialIsActive = true, slotId, initialProductIds = [], initialGroupIds = [], }: SlotFormProps) { const { data: slotData } = trpc.admin.slots.getSlotById.useQuery( { id: slotId! }, { enabled: !!slotId } ) const vendorSnippetsFromSlot = (slotData?.slot?.vendorSnippets || []).map((snippet: any) => ({ name: snippet.name || '', groupIds: snippet.groupIds || [], skuIds: snippet.skuIds || [], validTill: snippet.validTill || null, })) as VendorSnippet[] const initialValues = { deliveryTime: initialDeliveryTime || (slotData?.slot?.deliveryTime ? new Date(slotData.slot.deliveryTime) : null), freezeTime: initialFreezeTime || (slotData?.slot?.freezeTime ? new Date(slotData.slot.freezeTime) : null), selectedGroupIds: initialGroupIds.length > 0 ? initialGroupIds : (slotData?.slot?.groupIds || []), selectedSkuIds: initialProductIds.length > 0 ? initialProductIds : (slotData?.slot?.products?.map((p: any) => p.id) || []), vendorSnippetList: vendorSnippetsFromSlot, } const { mutate: createSlot, isPending: isCreating } = trpc.admin.slots.createSlot.useMutation() const { mutate: updateSlot, isPending: isUpdating } = trpc.admin.slots.updateSlot.useMutation() const isEditMode = !!slotId const isPending = isCreating || isUpdating const { data: groupsData } = trpc.admin.product.getGroups.useQuery() const handleFormSubmit = (values: typeof initialValues) => { if (!values.deliveryTime || !values.freezeTime) { window.alert('Error: Please fill all fields') return } if (values.freezeTime > values.deliveryTime) { window.alert('Error: Freeze time must be before or equal to delivery time') return } const slotInputData = { deliveryTime: values.deliveryTime.toISOString(), freezeTime: values.freezeTime.toISOString(), isActive: initialIsActive, groupIds: values.selectedGroupIds, skuIds: values.selectedSkuIds, vendorSnippets: values.vendorSnippetList.map((snippet: VendorSnippet) => ({ name: snippet.name, skuIds: snippet.skuIds, validTill: snippet.validTill ?? undefined, })), } if (isEditMode && slotId) { updateSlot( { id: slotId, ...slotInputData }, { onSuccess: () => { window.alert('Success: Slot updated successfully!') onSlotAdded?.() }, onError: (error: any) => { console.log({msg: JSON.stringify(error.message)}) window.alert(`Error: ${error.message || 'Failed to update slot'}`) }, } ) } else { createSlot( slotInputData, { onSuccess: () => { window.alert('Success: Slot created successfully!') onSlotAdded?.() }, onError: (error: any) => { window.alert(`Error: ${error.message || 'Failed to create slot'}`) }, } ) } } return ( {({ handleSubmit, values, setFieldValue }) => { const mappedGroups = (groupsData?.groups || []).map(group => ({ ...group, products: group.products.map(product => ({ ...product, id: product.id, })), })) return (

{isEditMode ? 'Edit Slot' : 'Create New Slot'}

Delivery Date & Time

setFieldValue('deliveryTime', value)} />

Freeze Date & Time

setFieldValue('freezeTime', value)} />
setFieldValue('selectedSkuIds', newSkuIds)} groups={mappedGroups} selectedGroupIds={values.selectedGroupIds} onGroupChange={(newGroupIds) => setFieldValue('selectedGroupIds', newGroupIds)} label="Select Products" placeholder="Select products for this slot" />
{/* Vendor Snippets */} {({ push, remove }) => (

Vendor Snippets

{values.vendorSnippetList.map((snippet: VendorSnippet, index: number) => (
setFieldValue(`vendorSnippetList.${index}.name`, e.target.value)} />
setFieldValue(`vendorSnippetList.${index}.skuIds`, newSkuIds)} groups={mappedGroups.filter(group => values.selectedGroupIds.includes(group.id) ).map(group => ({ ...group, products: group.products.filter((prod: any) => values.selectedSkuIds.includes(prod.id)) }))} selectedGroupIds={snippet.groupIds || []} onGroupChange={(newGroupIds) => setFieldValue(`vendorSnippetList.${index}.groupIds`, newGroupIds)} label="Select Products" placeholder="Select products for snippet" isDisabled={(sku) => !values.selectedSkuIds.includes(sku.id)} />
))}
)}
)}}
) }