import { compact, findIndex, isEmpty, isNil, uniqBy } from 'lodash'; import React, { ReactNode, useEffect, useState } from 'react'; import { SummaryCheckbox, TravelersFormValues } from '../../booking-wizard/types'; import { buildClassName } from '../utils/class-util'; import Icon from '../components/icon'; import Spinner from '../../search-results/components/spinner/spinner'; export interface SummaryNotification { id: number; title?: string | null; description?: string | null; hasToBeConfirmed?: boolean; isConfirmed?: boolean; } export interface SummaryVoucherState { code?: string; isValidated?: boolean; isValid?: boolean; } export interface SharedSummaryProps { translations: any; travelerFormValues: TravelersFormValues; checkboxes?: SummaryCheckbox[] | null; notifications?: SummaryNotification[] | null; remarks?: string; voucher?: SummaryVoucherState; voucherCodes?: string[]; enableVoucher?: boolean; allowOption?: boolean; isOffer?: boolean; customValidateText?: string; isSubmitting?: boolean; skipPayment?: boolean; userValidated?: boolean; renderOptions: () => ReactNode; renderPreviousButton: () => ReactNode; renderLoader?: () => ReactNode; onSubmit: React.FormEventHandler; onRemarksChange?: (text: string) => void; onCheckboxesChange?: (checkboxes: SummaryCheckbox[]) => void; onNotificationsChange?: (notifications: SummaryNotification[]) => void; onVoucherChange?: (voucher: SummaryVoucherState) => void; onValidateVoucher?: () => void; onAddVoucher?: React.MouseEventHandler; onRemoveVoucher?: (code: string) => void; onUserValidatedChange?: (isValid: boolean) => void; } const formatBirthDate = (birthDate?: string | null) => { if (!birthDate) return ''; return birthDate.split('T')[0].split('-').reverse().join('/'); }; const SharedSummary: React.FC = ({ translations, travelerFormValues, checkboxes, notifications, remarks = '', voucher = {}, voucherCodes = [], enableVoucher = false, allowOption = false, isOffer = false, customValidateText, isSubmitting = false, skipPayment = false, userValidated = true, renderOptions, renderPreviousButton, renderLoader, onSubmit, onRemarksChange, onCheckboxesChange, onNotificationsChange, onVoucherChange, onValidateVoucher, onAddVoucher, onRemoveVoucher, onUserValidatedChange }) => { const [localCheckboxes, setLocalCheckboxes] = useState(checkboxes); useEffect(() => { setLocalCheckboxes(checkboxes); }, [checkboxes]); useEffect(() => { const checkboxesValidated = !isNil(localCheckboxes) ? localCheckboxes.every((checkbox) => checkbox.isSelected) : true; const notificationsValidated = !isNil(notifications) ? notifications.filter((notification) => notification.hasToBeConfirmed).every((notification) => notification.isConfirmed) : true; onUserValidatedChange?.(checkboxesValidated && notificationsValidated); }, [localCheckboxes, notifications, onUserValidatedChange]); const handleNotificationChange = (id: number, checked: boolean) => { const updatedNotifications = (notifications ?? []).map((notification) => notification.id === id ? { ...notification, isConfirmed: checked } : notification ); onNotificationsChange?.(updatedNotifications); }; const handleCheckboxChange = (id: string) => { if (isNil(localCheckboxes)) return; const newCheckboxes = [...localCheckboxes]; const index = findIndex(localCheckboxes, (checkbox) => checkbox.id === id); if (index < 0) return; newCheckboxes[index] = { ...newCheckboxes[index], isSelected: !newCheckboxes[index].isSelected }; setLocalCheckboxes(newCheckboxes); onCheckboxesChange?.(newCheckboxes); }; return ( <> {isSubmitting && } {!isSubmitting && (
{translations.SUMMARY.PERSONAL_DETAILS}
{travelerFormValues.rooms.map((room, roomIndex) => (
{travelerFormValues.rooms.length > 1 ? `${translations.SHARED.ROOM} ${roomIndex + 1}` : translations.ROOM_OPTIONS_FORM.TRAVELER_GROUP}

{`${room.adults.length + room.children.length} ${ room.adults.length + room.children.length === 1 ? translations.SUMMARY.TRAVELER : translations.SUMMARY.TRAVELERS }: ${compact([ room.adults.length, room.adults.length === 1 && ` ${translations.SUMMARY.ADULT}`, room.adults.length > 1 && ` ${translations.SUMMARY.ADULTS}`, room.adults?.length && room.children?.length && ', ', room.children.length, room.children.length === 1 && ` ${translations.SUMMARY.CHILD}`, room.children.length > 1 && ` ${translations.SUMMARY.CHILDREN}` ]).join('')}`}

{[...room.adults, ...room.children].map((traveler) => { const isMainBooker = traveler.id === travelerFormValues.mainBookerId; return (
  • {traveler.firstName} {traveler.lastName} {' '} {isMainBooker && ({translations.SUMMARY.MAIN_BOOKER})}
  • {formatBirthDate(traveler.birthDate)}
  • {isMainBooker && ( <> {travelerFormValues.street && (
  • {`${travelerFormValues.street} ${compact([ travelerFormValues.houseNumber, travelerFormValues.box ]).join(' ')}, ${travelerFormValues.zipCode} ${travelerFormValues.place}`}
  • )} {travelerFormValues.phone &&
  • {travelerFormValues.phone}
  • } {travelerFormValues.email &&
  • {travelerFormValues.email}
  • } )}
); })}
))}
{translations.SUMMARY.OPTIONS}
    {renderOptions()}
{enableVoucher && (
{translations.SUMMARY.VOUCHERS}
onVoucherChange?.({ code: event.target.value })} />
{voucher.isValid && voucher.isValidated && (
{translations.SUMMARY.VOUCHER_VALID}
)} {!voucher.isValid && voucher.isValidated &&
{translations.SUMMARY.VOUCHER_INVALID}
}
    {!isEmpty(voucherCodes) && voucherCodes.map((code, index) => (
  • {code}{' '}
  • ))}
)} {!isEmpty(notifications) && (
{translations.SUMMARY.NOTIFICATIONS_TITLE}
{uniqBy( (notifications ?? []).filter((notification) => !notification.hasToBeConfirmed), 'id' ).map((notification) => ( {notification.title} {notification.description} ))} {uniqBy( (notifications ?? []).filter((notification) => notification.hasToBeConfirmed), 'id' ).map((notification) => (
))}
)}
{translations.SUMMARY.REMARKS}