Merge branch 'develop' into MED-97

This commit is contained in:
2025-09-26 17:01:24 +03:00
86 changed files with 11249 additions and 3151 deletions

View File

@@ -0,0 +1,40 @@
'use client';
import { isBefore, isSameDay } from 'date-fns';
import { uniq } from 'lodash';
import { Calendar } from '@kit/ui/shadcn/calendar';
import { Card } from '@kit/ui/shadcn/card';
import { cn } from '@kit/ui/utils';
import { useBooking } from './booking.provider';
export default function BookingCalendar() {
const { selectedDate, setSelectedDate, isLoadingTimeSlots, timeSlots } =
useBooking();
const availableDates = uniq(timeSlots?.map((timeSlot) => timeSlot.StartTime));
return (
<Card className="mb-4">
<Calendar
mode="single"
selected={selectedDate}
onSelect={setSelectedDate}
disabled={(date) => {
const today = new Date();
today.setHours(0, 0, 0, 0);
return (
isBefore(date, today) ||
!availableDates.some((dateWithBooking) =>
isSameDay(date, dateWithBooking),
)
);
}}
className={cn('rounded-md border', {
'pointer-events-none rounded-md border opacity-50':
isLoadingTimeSlots,
})}
/>
</Card>
);
}

View File

@@ -0,0 +1,49 @@
'use client';
import { StoreProduct } from '@medusajs/types';
import { Trans } from '@kit/ui/trans';
import { EnrichedCartItem } from '../cart/types';
import BookingCalendar from './booking-calendar';
import { BookingProvider } from './booking.provider';
import LocationSelector from './location-selector';
import ServiceSelector from './service-selector';
import TimeSlots from './time-slots';
const BookingContainer = ({
category,
cartItem,
onComplete,
}: {
category: { products: StoreProduct[]; countryCode: string };
cartItem?: EnrichedCartItem;
onComplete?: () => void;
}) => {
const products = cartItem?.product ? [cartItem.product] : category.products;
if (!cartItem || !products?.length) {
<p>
<Trans i18nKey="booking:noProducts" />
</p>;
}
return (
<BookingProvider category={{ products }} service={cartItem?.product}>
<div className="xs:flex-row flex max-h-full flex-col gap-6">
<div className="flex flex-col">
<ServiceSelector products={products} />
<BookingCalendar />
<LocationSelector />
</div>
<TimeSlots
countryCode={category.countryCode}
cartItem={cartItem}
onComplete={onComplete}
/>
</div>
</BookingProvider>
);
};
export default BookingContainer;

View File

@@ -0,0 +1,77 @@
import { createContext } from 'react';
import { StoreProduct } from '@medusajs/types';
import { noop } from 'lodash';
import { Tables } from '@kit/supabase/database';
export type Location = Tables<
{ schema: 'medreport' },
'connected_online_locations'
>;
export type TimeSlotResponse = {
timeSlots: TimeSlot[];
locations: Location[];
};
export type TimeSlot = {
ClinicID: number;
LocationID: number;
UserID: number;
SyncUserID: number;
ServiceID: number;
HKServiceID: number;
StartTime: Date;
EndTime: Date;
PayorCode: string;
serviceProvider?: ServiceProvider;
syncedService?: SyncedService;
} & { location?: Location };
export type ServiceProvider = {
name: string;
id: number;
jobTitleEn: string | null;
jobTitleEt: string | null;
jobTitleRu: string | null;
clinicId: number;
};
export type SyncedService = Tables<
{ schema: 'medreport' },
'connected_online_services'
> & {
providerClinic: ProviderClinic;
};
export type ProviderClinic = Tables<
{ schema: 'medreport' },
'connected_online_providers'
> & { locations: Location[] };
const BookingContext = createContext<{
timeSlots: TimeSlot[] | null;
selectedService: StoreProduct | null;
locations: Location[] | null;
selectedLocationId: number | null;
selectedDate?: Date;
isLoadingTimeSlots?: boolean;
setSelectedService: (selectedService: StoreProduct | null) => void;
setSelectedLocationId: (selectedLocationId: number | null) => void;
updateTimeSlots: (serviceIds: number[]) => Promise<void>;
setSelectedDate: (selectedDate?: Date) => void;
}>({
timeSlots: null,
selectedService: null,
locations: null,
selectedLocationId: null,
selectedDate: new Date(),
isLoadingTimeSlots: false,
setSelectedService: (_) => _,
setSelectedLocationId: (_) => _,
updateTimeSlots: async (_) => noop(),
setSelectedDate: (_) => _,
});
export { BookingContext };

View File

@@ -0,0 +1,80 @@
import React, { useContext, useEffect, useState } from 'react';
import { StoreProduct } from '@medusajs/types';
import { getAvailableTimeSlotsForDisplay } from '~/lib/services/connected-online.service';
import { BookingContext, Location, TimeSlot } from './booking.context';
export function useBooking() {
const context = useContext(BookingContext);
if (!context) {
throw new Error('useBooking must be used within a BookingProvider.');
}
return context;
}
export const BookingProvider: React.FC<{
children: React.ReactElement;
category: { products: StoreProduct[] };
service?: StoreProduct;
}> = ({ children, category, service }) => {
const [selectedService, setSelectedService] = useState<StoreProduct | null>(
(service ?? category?.products?.[0]) || null,
);
const [selectedLocationId, setSelectedLocationId] = useState<number | null>(
null,
);
const [selectedDate, setSelectedDate] = useState<Date>();
const [timeSlots, setTimeSlots] = useState<TimeSlot[] | null>(null);
const [locations, setLocations] = useState<Location[] | null>(null);
const [isLoadingTimeSlots, setIsLoadingTimeSlots] = useState(false);
useEffect(() => {
const metadataServiceIds = selectedService?.metadata?.serviceIds as string;
if (metadataServiceIds) {
const json = JSON.parse(metadataServiceIds);
if (Array.isArray(json)) {
updateTimeSlots(json);
}
}
}, [selectedService, selectedLocationId]);
const updateTimeSlots = async (serviceIds: number[]) => {
setIsLoadingTimeSlots(true);
try {
console.log('serviceIds', serviceIds, selectedLocationId);
const response = await getAvailableTimeSlotsForDisplay(
serviceIds,
selectedLocationId,
);
setTimeSlots(response.timeSlots);
setLocations(response.locations);
} catch (error) {
setTimeSlots(null);
} finally {
setIsLoadingTimeSlots(false);
}
};
return (
<BookingContext.Provider
value={{
timeSlots,
locations,
selectedService,
selectedLocationId,
setSelectedLocationId,
selectedDate,
isLoadingTimeSlots,
setSelectedService,
updateTimeSlots,
setSelectedDate,
}}
>
{children}
</BookingContext.Provider>
);
};

View File

@@ -0,0 +1,55 @@
import { Label } from '@medusajs/ui';
import { useTranslation } from 'react-i18next';
import { RadioGroup, RadioGroupItem } from '@kit/ui/radio-group';
import { Card } from '@kit/ui/shadcn/card';
import { Trans } from '@kit/ui/trans';
import { useBooking } from './booking.provider';
const LocationSelector = () => {
const { t } = useTranslation();
const { selectedLocationId, setSelectedLocationId, locations } = useBooking();
const onLocationSelect = (locationId: number | string | null) => {
if (locationId === 'all') return setSelectedLocationId(null);
setSelectedLocationId(Number(locationId));
};
return (
<Card className="mb-4 p-4">
<h5 className="text-semibold mb-2">
<Trans i18nKey="booking:locations" />
</h5>
<div className="flex flex-col">
<RadioGroup
className="mb-2 flex flex-col"
onValueChange={(val) => onLocationSelect(val)}
>
<div className="flex items-center gap-2">
<RadioGroupItem
value={'all'}
id={'all'}
checked={selectedLocationId === null}
/>
<Label htmlFor={'all'}>{t('booking:showAllLocations')}</Label>
</div>
{locations?.map((location) => (
<div key={location.sync_id} className="flex items-center gap-2">
<RadioGroupItem
value={location.sync_id.toString()}
id={location.sync_id.toString()}
checked={selectedLocationId === location.sync_id}
/>
<Label htmlFor={location.sync_id.toString()}>
{location.name}
</Label>
</div>
))}
</RadioGroup>
</div>
</Card>
);
};
export default LocationSelector;

View File

@@ -0,0 +1,85 @@
import { useState } from 'react';
import { StoreProduct } from '@medusajs/types';
import { ChevronDown } from 'lucide-react';
import { Card } from '@kit/ui/shadcn/card';
import { Label } from '@kit/ui/shadcn/label';
import {
Popover,
PopoverContent,
PopoverTrigger,
} from '@kit/ui/shadcn/popover';
import { RadioGroup, RadioGroupItem } from '@kit/ui/shadcn/radio-group';
import { Trans } from '@kit/ui/trans';
import { useBooking } from './booking.provider';
const ServiceSelector = ({ products }: { products: StoreProduct[] }) => {
const { selectedService, setSelectedService } = useBooking();
const [collapsed, setCollapsed] = useState(false);
const [firstFourProducts] = useState<StoreProduct[]>(products?.slice(0, 4));
const onServiceSelect = async (productId: StoreProduct['id']) => {
const product = products.find((p) => p.id === productId);
setSelectedService(product ?? null);
setCollapsed(false);
};
return (
<Card className="mb-4 p-4">
<h5 className="text-semibold mb-2">
<Trans i18nKey="booking:services" />
</h5>
<Popover open={collapsed} onOpenChange={setCollapsed}>
<div className="flex flex-col">
<RadioGroup
defaultValue={selectedService?.id || ''}
className="mb-2 flex flex-col"
onValueChange={onServiceSelect}
>
{firstFourProducts?.map((product) => (
<div key={product.id} className="flex items-center gap-2">
<RadioGroupItem
value={product.id}
id={product.id}
checked={selectedService?.id === product.id}
/>
<Label htmlFor={product.id}>{product.title}</Label>
</div>
))}
</RadioGroup>
{products.length > 4 && (
<PopoverTrigger asChild>
<div
onClick={() => setCollapsed((_) => !_)}
className="flex cursor-pointer items-center justify-between border-t py-1"
>
<span>
<Trans i18nKey="booking:showAll" />
</span>
<ChevronDown />
</div>
</PopoverTrigger>
)}
</div>
<PopoverContent sideOffset={10}>
<RadioGroup onValueChange={onServiceSelect}>
{products?.map((product) => (
<div key={product.id + '-2'} className="flex items-center gap-2">
<RadioGroupItem
value={product.id}
id={product.id + '-2'}
checked={selectedService?.id === product.id}
/>
<Label htmlFor={product.id + '-2'}>{product.title}</Label>
</div>
))}
</RadioGroup>
</PopoverContent>
</Popover>
</Card>
);
};
export default ServiceSelector;

View File

@@ -0,0 +1,319 @@
import { useMemo, useState } from 'react';
import { useRouter } from 'next/navigation';
import { formatCurrency } from '@/packages/shared/src/utils';
import { addHours, isAfter, isSameDay } from 'date-fns';
import { orderBy } from 'lodash';
import { useTranslation } from 'react-i18next';
import { pathsConfig } from '@kit/shared/config';
import { formatDateAndTime } from '@kit/shared/utils';
import { Button } from '@kit/ui/shadcn/button';
import { Card } from '@kit/ui/shadcn/card';
import { toast } from '@kit/ui/sonner';
import { Trans } from '@kit/ui/trans';
import { cn } from '@kit/ui/utils';
import { updateReservationTime } from '~/lib/services/reservation.service';
import { createInitialReservationAction } from '../../_lib/server/actions';
import { EnrichedCartItem } from '../cart/types';
import { ServiceProvider, TimeSlot } from './booking.context';
import { useBooking } from './booking.provider';
const getServiceProviderTitle = (
currentLocale: string,
serviceProvider?: ServiceProvider,
) => {
if (!serviceProvider) return null;
if (currentLocale === 'en') return serviceProvider.jobTitleEn;
if (currentLocale === 'ru') return serviceProvider.jobTitleRu;
return serviceProvider.jobTitleEt;
};
const PAGE_SIZE = 7;
const TimeSlots = ({
countryCode,
cartItem,
onComplete,
}: {
countryCode: string;
cartItem?: EnrichedCartItem;
onComplete?: () => void;
}) => {
const [currentPage, setCurrentPage] = useState(1);
const {
t,
i18n: { language: currentLocale },
} = useTranslation();
const booking = useBooking();
const router = useRouter();
const selectedDate = booking.selectedDate ?? new Date();
const filteredBookings = useMemo(
() =>
orderBy(
booking?.timeSlots?.filter(({ StartTime }) => {
const firstAvailableTimeToSelect = isSameDay(selectedDate, new Date())
? addHours(new Date(), 0.5)
: selectedDate;
return isAfter(StartTime, firstAvailableTimeToSelect);
}) ?? [],
'StartTime',
'asc',
),
[booking.timeSlots, selectedDate],
);
const totalPages = Math.ceil(filteredBookings.length / PAGE_SIZE);
const paginatedBookings = useMemo(() => {
const startIndex = (currentPage - 1) * PAGE_SIZE;
const endIndex = startIndex + PAGE_SIZE;
return filteredBookings.slice(startIndex, endIndex);
}, [filteredBookings, currentPage, PAGE_SIZE]);
const handlePageChange = (page: number) => {
setCurrentPage(page);
};
const generatePageNumbers = () => {
const pages = [];
const maxVisiblePages = 5;
if (totalPages <= maxVisiblePages) {
for (let i = 1; i <= totalPages; i++) {
pages.push(i);
}
} else {
if (currentPage <= 3) {
for (let i = 1; i <= 4; i++) {
pages.push(i);
}
pages.push('...');
pages.push(totalPages);
} else if (currentPage >= totalPages - 2) {
pages.push(1);
pages.push('...');
for (let i = totalPages - 3; i <= totalPages; i++) {
pages.push(i);
}
} else {
pages.push(1);
pages.push('...');
for (let i = currentPage - 1; i <= currentPage + 1; i++) {
pages.push(i);
}
pages.push('...');
pages.push(totalPages);
}
}
return pages;
};
if (!booking?.timeSlots?.length) {
return null;
}
const handleBookTime = async (timeSlot: TimeSlot, comments?: string) => {
const selectedService = booking.selectedService;
const selectedVariant = selectedService?.variants?.[0];
const syncedService = timeSlot.syncedService;
if (!syncedService || !selectedVariant) {
return toast.error(t('booking:serviceNotFound'));
}
const bookTimePromise = createInitialReservationAction(
selectedVariant,
countryCode,
Number(syncedService.id),
syncedService?.clinic_id,
timeSlot.UserID,
timeSlot.SyncUserID,
timeSlot.StartTime,
booking.selectedLocationId ? booking.selectedLocationId : null,
comments,
).then(() => {
if (onComplete) {
onComplete();
}
router.push(pathsConfig.app.cart);
});
toast.promise(() => bookTimePromise, {
success: <Trans i18nKey={'booking:bookTimeSuccess'} />,
error: <Trans i18nKey={'booking:bookTimeError'} />,
loading: <Trans i18nKey={'booking:bookTimeLoading'} />,
});
};
const handleChangeTime = async (
timeSlot: TimeSlot,
reservationId: number,
cartId: string,
) => {
const syncedService = timeSlot.syncedService;
if (!syncedService) {
return toast.error(t('booking:serviceNotFound'));
}
const bookTimePromise = updateReservationTime({
reservationId,
newStartTime: timeSlot.StartTime,
newServiceId: Number(syncedService.id),
newAppointmentUserId: timeSlot.UserID,
newSyncUserId: timeSlot.SyncUserID,
newLocationId: booking.selectedLocationId
? booking.selectedLocationId
: null,
cartId,
});
toast.promise(() => bookTimePromise, {
success: <Trans i18nKey={'booking:bookTimeSuccess'} />,
error: <Trans i18nKey={'booking:bookTimeError'} />,
loading: <Trans i18nKey={'booking:bookTimeLoading'} />,
});
if (onComplete) {
onComplete();
}
};
const handleTimeSelect = async (timeSlot: TimeSlot) => {
if (cartItem?.reservation.id) {
return handleChangeTime(
timeSlot,
cartItem.reservation.id,
cartItem.cart_id,
);
}
return handleBookTime(timeSlot);
};
return (
<div className="flex w-full flex-col gap-4">
<div className="flex h-full w-full flex-col gap-2 overflow-auto">
{paginatedBookings.map((timeSlot, index) => {
const isEHIF = timeSlot.HKServiceID > 0;
const serviceProviderTitle = getServiceProviderTitle(
currentLocale,
timeSlot.serviceProvider,
);
const price =
booking.selectedService?.variants?.[0]?.calculated_price
?.calculated_amount ?? cartItem?.unit_price;
return (
<Card
className="xs:flex xs:justify-between grid w-full justify-center-safe gap-3 p-4"
key={index}
>
<div>
<span>{formatDateAndTime(timeSlot.StartTime.toString())}</span>
<div className="flex">
<h5
className={cn(
(serviceProviderTitle || isEHIF) &&
"after:mx-2 after:content-['·']",
)}
>
{timeSlot.serviceProvider?.name}
</h5>
{serviceProviderTitle && (
<span
className={cn(isEHIF && "after:mx-2 after:content-['·']")}
>
{serviceProviderTitle}
</span>
)}
{isEHIF && <span>{t('booking:ehifBooking')}</span>}
</div>
<div className="flex text-xs">{timeSlot.location?.address}</div>
</div>
<div className="flex-end not-last:xs:justify-center flex items-center justify-between gap-2">
<span className="text-sm font-semibold">
{formatCurrency({
currencyCode: 'EUR',
locale: 'et-EE',
value: price ?? '',
})}
</span>
<Button onClick={() => handleTimeSelect(timeSlot)} size="sm">
<Trans i18nKey="common:book" />
</Button>
</div>
</Card>
);
})}
{!paginatedBookings.length && (
<div className="wrap text-muted-foreground flex size-full content-center-safe justify-center-safe">
<p>{t('booking:noResults')}</p>
</div>
)}
</div>
{totalPages > 1 && (
<div className="flex items-center justify-between">
<div className="text-muted-foreground text-sm">
{t('common:pageOfPages', {
page: currentPage,
total: totalPages,
})}
</div>
<div className="flex items-center gap-1">
<Button
variant="outline"
size="sm"
onClick={() => handlePageChange(currentPage - 1)}
disabled={currentPage === 1}
>
<Trans i18nKey="common:previous" defaultValue="Previous" />
</Button>
{generatePageNumbers().map((page, index) => (
<Button
key={index}
variant={page === currentPage ? 'default' : 'outline'}
size="sm"
onClick={() =>
typeof page === 'number' && handlePageChange(page)
}
disabled={page === '...'}
className={cn(
'min-w-[2rem]',
page === '...' && 'cursor-default hover:bg-transparent',
)}
>
{page}
</Button>
))}
<Button
variant="outline"
size="sm"
onClick={() => handlePageChange(currentPage + 1)}
disabled={currentPage === totalPages}
>
<Trans i18nKey="common:next" defaultValue="Next" />
</Button>
</div>
</div>
)}
</div>
);
};
export default TimeSlots;

View File

@@ -0,0 +1,146 @@
'use client';
import { useState } from 'react';
import { formatCurrency, formatDateAndTime } from '@/packages/shared/src/utils';
import { useTranslation } from 'react-i18next';
import { Button } from '@kit/ui/button';
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
} from '@kit/ui/dialog';
import { TableCell, TableRow } from '@kit/ui/table';
import { Trans } from '@kit/ui/trans';
import BookingContainer from '../booking/booking-container';
import CartItemDelete from './cart-item-delete';
import { EnrichedCartItem } from './types';
const EditCartServiceItemModal = ({
item,
onComplete,
}: {
item: EnrichedCartItem | null;
onComplete: () => void;
}) => {
if (!item) return null;
return (
<Dialog defaultOpen>
<DialogContent className="xs:max-w-[90vw] flex max-h-screen max-w-full flex-col items-center gap-4 space-y-4 overflow-y-scroll">
<DialogHeader className="items-center text-center">
<DialogTitle>
<Trans i18nKey="cart:editServiceItem.title" />
</DialogTitle>
<DialogDescription>
<Trans i18nKey="cart:editServiceItem.description" />
</DialogDescription>
</DialogHeader>
<div>
{item.product && item.reservation.countryCode ? (
<BookingContainer
category={{
products: [item.product],
countryCode: item.reservation.countryCode,
}}
cartItem={item}
onComplete={onComplete}
/>
) : (
<p>
<Trans i18nKey="booking:noProducts" />
</p>
)}
</div>
</DialogContent>
</Dialog>
);
};
export default function CartServiceItem({
item,
currencyCode,
isUnavailable,
}: {
item: EnrichedCartItem;
currencyCode: string;
isUnavailable?: boolean;
}) {
const [editingItem, setEditingItem] = useState<EnrichedCartItem | null>(null);
const {
i18n: { language },
} = useTranslation();
return (
<>
<TableRow className="w-full" data-testid="product-row">
<TableCell className="w-[100%] px-4 text-left sm:px-6">
<p
className="txt-medium-plus text-ui-fg-base"
data-testid="product-title"
>
{item.product_title}
</p>
</TableCell>
<TableCell className="px-4 sm:px-6">
{formatDateAndTime(item.reservation.startTime.toString())}
</TableCell>
<TableCell className="px-4 sm:px-6">
{item.reservation.location?.address ?? '-'}
</TableCell>
<TableCell className="px-4 sm:px-6">{item.quantity}</TableCell>
<TableCell className="min-w-[80px] px-4 sm:px-6">
{formatCurrency({
value: item.unit_price,
currencyCode,
locale: language,
})}
</TableCell>
<TableCell className="min-w-[80px] px-4 text-right sm:px-6">
{formatCurrency({
value: item.total,
currencyCode,
locale: language,
})}
</TableCell>
<TableCell className="px-4 text-right sm:px-6">
<span className="flex justify-end gap-x-1">
<Button size="sm" onClick={() => setEditingItem(item)}>
<Trans i18nKey="common:change" />
</Button>
</span>
</TableCell>
<TableCell className="px-4 text-right sm:px-6">
<span className="flex w-[60px] justify-end gap-x-1">
<CartItemDelete id={item.id} />
</span>
</TableCell>
</TableRow>
{isUnavailable && (
<TableRow>
<TableCell
colSpan={8}
className="text-destructive px-4 text-left sm:px-6"
>
<Trans i18nKey="booking:timeSlotUnavailable" />
</TableCell>
</TableRow>
)}
<EditCartServiceItemModal
item={editingItem}
onComplete={() => setEditingItem(null)}
/>
</>
);
}

View File

@@ -0,0 +1,72 @@
import { StoreCart } from '@medusajs/types';
import {
Table,
TableBody,
TableHead,
TableHeader,
TableRow,
} from '@kit/ui/table';
import { Trans } from '@kit/ui/trans';
import CartServiceItem from './cart-service-item';
import { EnrichedCartItem } from './types';
export default function CartServiceItems({
cart,
items,
productColumnLabelKey,
unavailableLineItemIds,
}: {
cart: StoreCart;
items: EnrichedCartItem[];
productColumnLabelKey: string;
unavailableLineItemIds?: string[];
}) {
if (!items || items.length === 0) {
return null;
}
return (
<Table className="border-separate rounded-lg border">
<TableHeader className="text-ui-fg-subtle txt-medium-plus">
<TableRow>
<TableHead className="px-4 sm:px-6">
<Trans i18nKey={productColumnLabelKey} />
</TableHead>
<TableHead className="px-4 sm:px-6">
<Trans i18nKey="cart:table.time" />
</TableHead>
<TableHead className="px-4 sm:px-6">
<Trans i18nKey="cart:table.location" />
</TableHead>
<TableHead className="px-4 sm:px-6">
<Trans i18nKey="cart:table.quantity" />
</TableHead>
<TableHead className="min-w-[100px] px-4 sm:px-6">
<Trans i18nKey="cart:table.price" />
</TableHead>
<TableHead className="min-w-[100px] px-4 text-right sm:px-6">
<Trans i18nKey="cart:table.total" />
</TableHead>
<TableHead className="px-4 sm:px-6"></TableHead>
<TableHead className="px-4 sm:px-6"></TableHead>
</TableRow>
</TableHeader>
<TableBody>
{items
.sort((a, b) =>
(a.created_at ?? '') > (b.created_at ?? '') ? -1 : 1,
)
.map((item) => (
<CartServiceItem
key={item.id}
item={item}
currencyCode={cart.currency_code}
isUnavailable={unavailableLineItemIds?.includes(item.id)}
/>
))}
</TableBody>
</Table>
);
}

View File

@@ -13,10 +13,12 @@ import { Trans } from '@kit/ui/trans';
import AnalysisLocation from './analysis-location';
import CartItems from './cart-items';
import CartServiceItems from './cart-service-items';
import DiscountCode from './discount-code';
import { initiatePayment } from '../../_lib/server/cart-actions';
import { useRouter } from 'next/navigation';
import { AccountBalanceSummary } from '@kit/accounts/services/account-balance.service';
import { EnrichedCartItem } from './types';
const IS_DISCOUNT_SHOWN = true as boolean;
@@ -30,7 +32,7 @@ export default function Cart({
accountId: string;
cart: StoreCart | null;
synlabAnalyses: StoreCartLineItem[];
ttoServiceItems: StoreCartLineItem[];
ttoServiceItems: EnrichedCartItem[];
balanceSummary: AccountBalanceSummary | null;
}) {
const {
@@ -39,6 +41,9 @@ export default function Cart({
const [isInitiatingSession, setIsInitiatingSession] = useState(false);
const router = useRouter();
const [unavailableLineItemIds, setUnavailableLineItemIds] =
useState<string[]>();
const items = cart?.items ?? [];
const hasCartItems = cart && Array.isArray(items) && items.length > 0;
@@ -66,12 +71,15 @@ export default function Cart({
setIsInitiatingSession(true);
try {
const { url, isFullyPaidByBenefits, orderId } = await initiatePayment({
const { url, isFullyPaidByBenefits, orderId, unavailableLineItemIds } = await initiatePayment({
accountId,
balanceSummary: balanceSummary!,
cart: cart!,
language,
});
if (unavailableLineItemIds) {
setUnavailableLineItemIds(unavailableLineItemIds);
}
if (url) {
window.location.href = url;
} else if (isFullyPaidByBenefits) {
@@ -99,10 +107,11 @@ export default function Cart({
items={synlabAnalyses}
productColumnLabelKey="cart:items.synlabAnalyses.productColumnLabel"
/>
<CartItems
<CartServiceItems
cart={cart}
items={ttoServiceItems}
productColumnLabelKey="cart:items.ttoServices.productColumnLabel"
unavailableLineItemIds={unavailableLineItemIds}
/>
</div>
{hasCartItems && (
@@ -202,6 +211,10 @@ export default function Cart({
cart={{ ...cart }}
synlabAnalyses={synlabAnalyses}
/>
<AnalysisLocation
cart={{ ...cart }}
synlabAnalyses={synlabAnalyses}
/>
</CardContent>
</Card>
)}

View File

@@ -1,3 +1,6 @@
import { StoreCartLineItem } from "@medusajs/types";
import { Reservation } from "~/lib/types/reservation";
export interface MontonioOrderToken {
uuid: string;
accessKey: string;
@@ -10,6 +13,12 @@ export interface MontonioOrderToken {
| 'PENDING'
| 'EXPIRED'
| 'REFUNDED';
| 'PAID'
| 'FAILED'
| 'CANCELLED'
| 'PENDING'
| 'EXPIRED'
| 'REFUNDED';
paymentMethod: string;
grandTotal: number;
currency: string;
@@ -20,3 +29,10 @@ export interface MontonioOrderToken {
iat: number;
exp: number;
}
export enum CartItemType {
analysisOrders = 'analysisOrders',
ttoServices = 'ttoServices',
}
export type EnrichedCartItem = StoreCartLineItem & { reservation: Reservation };

View File

@@ -103,7 +103,6 @@ export default function OrderAnalysesCards({
{title}
{description && (
<>
{' '}
<InfoTooltip
content={
<div className="flex flex-col gap-2">

View File

@@ -2,16 +2,18 @@ import { formatDate } from 'date-fns';
import { Trans } from '@kit/ui/trans';
import type { AnalysisOrder } from '~/lib/types/analysis-order';
export default function OrderDetails({ order }: { order: AnalysisOrder }) {
export default function OrderDetails({
order,
}: {
order: { id: string; created_at: string | Date };
}) {
return (
<div className="flex flex-col gap-y-2">
<div>
<span className="font-bold">
<Trans i18nKey="cart:orderConfirmed.orderNumber" />:{' '}
</span>
<span className="break-all">{order.medusa_order_id}</span>
<span className="break-all">{order.id}</span>
</div>
<div>

View File

@@ -5,51 +5,77 @@ import { Eye } from 'lucide-react';
import { Trans } from '@kit/ui/makerkit/trans';
import type { AnalysisOrder } from '~/lib/types/analysis-order';
import type { AnalysisOrder } from '~/lib/types/order';
import OrderItemsTable from './order-items-table';
export default function OrderBlock({
analysisOrder,
medusaOrderStatus,
itemsAnalysisPackage,
itemsTtoService,
itemsOther,
medusaOrderId,
}: {
analysisOrder: AnalysisOrder;
analysisOrder?: AnalysisOrder;
medusaOrderStatus: string;
itemsAnalysisPackage: StoreOrderLineItem[];
itemsTtoService: StoreOrderLineItem[];
itemsOther: StoreOrderLineItem[];
medusaOrderId: string;
}) {
return (
<div className="flex flex-col gap-4">
<h4>
<Trans
i18nKey="analysis-results:orderTitle"
values={{ orderNumber: analysisOrder.medusa_order_id }}
values={{ orderNumber: medusaOrderId }}
/>
{` (${analysisOrder.id})`}
</h4>
<div className="flex gap-2">
<h5>
<Trans i18nKey={`orders:status.${analysisOrder.status}`} />
</h5>
<Link
href={`/home/order/${analysisOrder.id}`}
className="text-small-regular flex items-center justify-between"
>
<button className="text-ui-fg-subtle hover:text-ui-fg-base flex cursor-pointer gap-x-1">
<Eye />
</button>
</Link>
</div>
{analysisOrder && (
<div className="flex gap-2">
<h5>
<Trans i18nKey={`orders:status.${analysisOrder.status}`} />
</h5>
<Link
href={`/home/order/${analysisOrder.id}`}
className="text-small-regular flex items-center justify-between"
>
<button className="text-ui-fg-subtle hover:text-ui-fg-base flex cursor-pointer gap-x-1">
<Eye />
</button>
</Link>
</div>
)}
<div className="flex flex-col gap-4">
<OrderItemsTable
items={itemsAnalysisPackage}
title="orders:table.analysisPackage"
analysisOrder={analysisOrder}
/>
{analysisOrder && (
<OrderItemsTable
items={itemsAnalysisPackage}
title="orders:table.analysisPackage"
order={{
medusaOrderId: analysisOrder.medusa_order_id,
id: analysisOrder.id,
status: analysisOrder.status,
}}
/>
)}
{itemsTtoService && (
<OrderItemsTable
items={itemsTtoService}
title="orders:table.ttoService"
type="ttoService"
order={{
status: medusaOrderStatus.toUpperCase(),
medusaOrderId,
}}
/>
)}
<OrderItemsTable
items={itemsOther}
title="orders:table.otherOrders"
analysisOrder={analysisOrder}
order={{
status: analysisOrder?.status,
}}
/>
</div>
</div>

View File

@@ -4,7 +4,6 @@ import { useRouter } from 'next/navigation';
import { StoreOrderLineItem } from '@medusajs/types';
import { formatDate } from 'date-fns';
import { Eye } from 'lucide-react';
import { pathsConfig } from '@kit/shared/config';
import { Button } from '@kit/ui/button';
@@ -18,18 +17,22 @@ import {
} from '@kit/ui/table';
import { Trans } from '@kit/ui/trans';
import type { AnalysisOrder } from '~/lib/types/analysis-order';
import type { Order } from '~/lib/types/order';
import { logAnalysisResultsNavigateAction } from './actions';
export type OrderItemType = 'analysisOrder' | 'ttoService';
export default function OrderItemsTable({
items,
title,
analysisOrder,
order,
type = 'analysisOrder',
}: {
items: StoreOrderLineItem[];
title: string;
analysisOrder: AnalysisOrder;
order: Order;
type?: OrderItemType;
}) {
const router = useRouter();
@@ -37,9 +40,15 @@ export default function OrderItemsTable({
return null;
}
const openAnalysisResults = async () => {
await logAnalysisResultsNavigateAction(analysisOrder.medusa_order_id);
router.push(`${pathsConfig.app.analysisResults}/${analysisOrder.id}`);
const isAnalysisOrder = type === 'analysisOrder';
const openDetailedView = async () => {
if (isAnalysisOrder && order?.medusaOrderId && order?.id) {
await logAnalysisResultsNavigateAction(order.medusaOrderId);
router.push(`${pathsConfig.app.analysisResults}/${order.id}`);
} else {
router.push(`${pathsConfig.app.myOrders}/${order.medusaOrderId}`);
}
};
return (
@@ -55,7 +64,7 @@ export default function OrderItemsTable({
<TableHead className="px-6">
<Trans i18nKey="orders:table.status" />
</TableHead>
<TableHead className="px-6"></TableHead>
{isAnalysisOrder && <TableHead className="px-6"></TableHead>}
</TableRow>
</TableHeader>
<TableBody>
@@ -76,11 +85,13 @@ export default function OrderItemsTable({
</TableCell>
<TableCell className="min-w-[180px] px-6">
<Trans i18nKey={`orders:status.${analysisOrder.status}`} />
<Trans
i18nKey={`orders:status.${type}.${order?.status ?? 'CONFIRMED'}`}
/>
</TableCell>
<TableCell className="px-6 text-right">
<Button size="sm" onClick={openAnalysisResults}>
<Button size="sm" onClick={openDetailedView}>
<Trans i18nKey="analysis-results:view" />
</Button>
</TableCell>

View File

@@ -4,17 +4,20 @@ import React from 'react';
import { redirect } from 'next/navigation';
import { createPath, pathsConfig } from '@/packages/shared/src/config';
import { pathsConfig } from '@/packages/shared/src/config';
import { StoreProduct } from '@medusajs/types';
import { ComponentInstanceIcon } from '@radix-ui/react-icons';
import { cn } from '@kit/ui/shadcn';
import { Card, CardDescription, CardTitle } from '@kit/ui/shadcn/card';
import { Card, CardDescription } from '@kit/ui/shadcn/card';
export interface ServiceCategory {
name: string;
handle: string;
color: string;
description: string;
products: StoreProduct[];
countryCode: string;
}
const ServiceCategories = ({