feat(MED-100): add partner locations list for cart

This commit is contained in:
2025-08-25 11:50:55 +03:00
parent 1d1b10d094
commit 4e6f12a9a0
2 changed files with 155 additions and 16 deletions

View File

@@ -19,17 +19,13 @@ import {
} from '@kit/ui/select';
import { updateCartPartnerLocation } from '../../_lib/server/update-cart-partner-location';
import partnerLocations from './partner-locations.json';
const AnalysisLocationSchema = z.object({
locationId: z.string().min(1),
});
const MOCK_LOCATIONS: { id: string, name: string }[] = [
{ id: "synlab-tallinn-1", name: "SYNLAB - Tallinn" },
{ id: "synlab-tartu-1", name: "SYNLAB - Tartu" },
{ id: "synlab-parnu-1", name: "SYNLAB - Pärnu" },
]
export default function AnalysisLocation({ cart, analysisPackages }: { cart: StoreCart, analysisPackages: StoreCartLineItem[] }) {
export default function AnalysisLocation({ cart, synlabAnalyses }: { cart: StoreCart, synlabAnalyses: StoreCartLineItem[] }) {
const { t } = useTranslation('cart');
const form = useForm<z.infer<typeof AnalysisLocationSchema>>({
@@ -39,12 +35,16 @@ export default function AnalysisLocation({ cart, analysisPackages }: { cart: Sto
resolver: zodResolver(AnalysisLocationSchema),
});
const getLocation = (locationId: string) => partnerLocations.find(({ name }) => name === locationId);
const selectedLocation = getLocation(form.watch('locationId'));
const onSubmit = async ({ locationId }: z.infer<typeof AnalysisLocationSchema>) => {
const promise = updateCartPartnerLocation({
cartId: cart.id,
lineIds: analysisPackages.map(({ id }) => id),
lineIds: synlabAnalyses.map(({ id }) => id),
partnerLocationId: locationId,
partnerLocationName: MOCK_LOCATIONS.find((location) => location.id === locationId)?.name ?? '',
partnerLocationName: getLocation(locationId)?.name ?? '',
});
toast.promise(promise, {
@@ -55,7 +55,7 @@ export default function AnalysisLocation({ cart, analysisPackages }: { cart: Sto
}
return (
<div className="w-full bg-white flex flex-col txt-medium">
<div className="w-full bg-white flex flex-col txt-medium gap-y-2">
<Form {...form}>
<form
onSubmit={form.handleSubmit((data) => onSubmit(data))}
@@ -78,18 +78,35 @@ export default function AnalysisLocation({ cart, analysisPackages }: { cart: Sto
</SelectTrigger>
<SelectContent>
<SelectGroup>
<SelectLabel>{t('cart:locations.locationSelect')}</SelectLabel>
{MOCK_LOCATIONS.map((location) => (
<SelectItem key={location.id} value={location.id}>{location.name}</SelectItem>
{Object.entries(partnerLocations
.reduce((acc, curr) => ({
...acc,
[curr.city]: [...((acc[curr.city] as typeof partnerLocations) ?? []), curr],
}), {} as Record<string, typeof partnerLocations>))
.map(([city, locations]) => (
<SelectGroup key={city}>
<SelectLabel>{city}</SelectLabel>
{locations.map((location) => (
<SelectItem key={location.name} value={location.name}>{location.name}</SelectItem>
))}
</SelectGroup>
))}
</SelectGroup>
</SelectContent>
</Select>
</form>
</Form>
{selectedLocation && (
<div className="flex flex-col gap-y-2 mb-4">
<p className="text-sm">
{selectedLocation.address}
</p>
<p className="text-sm">
{selectedLocation.hours}
</p>
</div>
)}
<p className="text-sm text-muted-foreground">
<Trans i18nKey={'cart:locations.description'} />
</p>