@@ -0,0 +1,183 @@
import { useCallback , useEffect , useState } from 'react'
import { useFormat , useTranslation } from '@xenia/i18n'
import { ReasonPicker , reasonText } from '@xenia/ui'
import AppShell from '../components/AppShell'
import client from '../api/client'
import { apiError } from '../api/errors'
import db from '../db/posdb'
// Table bookings on the phone (plan step 4.8), the counter's flow (terminal BookingPanel):
// requests made online are confirmed or declined (the guest sees the answer and the reason);
// today's confirmed bookings are seated at a free table (their food's order moves there) or
// cancelled / marked as a no-show. For whoever handles online orders or manages reservations.
const DECLINE _REASONS = [ 'full' , 'closed' , 'party' ]
const OPEN = [ 'open' , 'partially_paid' , 'paid' ]
export default function BookingsPage ( ) {
const { t } = useTranslation ( 'waiter' )
const [ data , setData ] = useState ( null )
const [ error , setError ] = useState ( '' )
const [ busy , setBusy ] = useState ( null )
const [ declining , setDeclining ] = useState ( null )
const [ free , setFree ] = useState ( [ ] )
const load = useCallback ( async ( ) => {
try {
const r = await client . get ( '/api/connect/bookings' )
setData ( r . data )
setError ( '' )
} catch ( err ) { setError ( apiError ( err , t ( 'bookings.loadFailed' ) ) ) }
try {
const [ tables , orders ] = await Promise . all ( [ db . pos _tables . toArray ( ) , db . pos _orders . toArray ( ) ] )
const busyTables = new Set ( orders . filter ( o => OPEN . includes ( o . status ) ) . map ( o => o . table _id ) )
setFree ( tables . filter ( tb => tb . is _active !== false && ! busyTables . has ( tb . id ) ) )
} catch { setFree ( [ ] ) }
} , [ t ] )
useEffect ( ( ) => {
load ( )
const onLive = e => { if ( [ 'reservation_updated' , 'order_updated' , 'online_reservation' ] . includes ( e . detail ? . type ) ) load ( ) }
window . addEventListener ( 'live-event' , onLive )
window . addEventListener ( 'live-reconnected' , load )
return ( ) => { window . removeEventListener ( 'live-event' , onLive ) ; window . removeEventListener ( 'live-reconnected' , load ) }
} , [ load ] )
async function run ( booking , path , body ) {
setBusy ( booking . id )
setError ( '' )
try {
await client . post ( ` /api/connect/bookings/ ${ booking . id } / ${ path } ` , body ? ? { } )
setDeclining ( null )
} catch ( err ) { setError ( apiError ( err , t ( 'bookings.failed' ) ) ) } finally {
setBusy ( null )
load ( )
}
}
const requested = data ? . requested ? ? [ ]
const upcoming = data ? . upcoming ? ? [ ]
return (
< AppShell title = { t ( 'bookings.title' ) } >
< div style = { { padding : '12px 12px 96px' , display : 'flex' , flexDirection : 'column' , gap : 10 } } data - testid = "bookings" >
{ error && < p className = "error-msg" role = "alert" > { error } < / p > }
{ ! data && ! error && < p style = { { textAlign : 'center' , color : 'var(--muted)' , padding : 24 } } > { t ( 'common.loading' ) } < / p > }
{ data && (
< >
< h2 style = { sectionTitle } > { t ( 'bookings.requests' , { count : requested . length } ) } < / h2 >
{ requested . length === 0 && < p style = { empty } > { t ( 'bookings.noRequests' ) } < / p > }
{ requested . map ( b => (
< BookingCard key = { b . id } booking = { b } >
< div style = { { display : 'flex' , gap : 8 } } >
< button className = "btn btn--accent" style = { { flex : 1 } } disabled = { busy === b . id } onClick = { ( ) => run ( b , 'confirm' ) } data - testid = "booking-confirm" >
{ t ( 'bookings.confirm' ) }
< / button >
< button className = "btn btn--secondary" style = { { flex : 1 } } disabled = { busy === b . id } onClick = { ( ) => setDeclining ( b ) } data - testid = "booking-decline" >
{ t ( 'bookings.decline' ) }
< / button >
< / div >
< / BookingCard >
) ) }
< h2 style = { { ... sectionTitle , marginTop : 12 } } > { t ( 'bookings.upcoming' , { count : upcoming . length } ) } < / h2 >
{ upcoming . length === 0 && < p style = { empty } > { t ( 'bookings.noUpcoming' ) } < / p > }
{ upcoming . map ( b => (
< BookingCard key = { b . id } booking = { b } >
< SeatControls booking = { b } free = { free } busy = { busy === b . id } onSeat = { tableId => run ( b , 'arrive' , { table _id : tableId } ) }
onCancel = { status => run ( b , 'cancel' , { status } ) } / >
< / BookingCard >
) ) }
< / >
) }
< / div >
{ declining && < DeclineSheet booking = { declining } busy = { busy === declining . id } onClose = { ( ) => setDeclining ( null ) } onDecline = { reason => run ( declining , 'decline' , { reason } ) } / > }
< / AppShell >
)
}
const sectionTitle = { fontSize : 12 , fontWeight : 700 , color : 'var(--muted)' , textTransform : 'uppercase' , letterSpacing : 0.6 , margin : '4px 2px 0' }
const empty = { color : 'var(--muted)' , fontSize : 14 , padding : '6px 2px' }
function BookingCard ( { booking : b , children } ) {
const { t } = useTranslation ( 'waiter' )
const { formatTime , formatDate , formatDateTime , formatMoney } = useFormat ( )
const today = formatDate ( b . reserved _for ) === formatDate ( new Date ( ) )
return (
< div data - testid = "booking-card" data - booking = { b . id } data - status = { b . status }
style = { { background : 'var(--bg2)' , border : '1px solid var(--border)' , borderRadius : 14 , padding : 14 , display : 'flex' , flexDirection : 'column' , gap : 8 } } >
< div style = { { display : 'flex' , alignItems : 'baseline' , gap : 8 } } >
< span style = { { fontSize : 17 , fontWeight : 800 , color : 'var(--text)' , flex : 1 } } >
{ today ? t ( 'bookings.today' , { time : formatTime ( b . reserved _for ) } ) : formatDateTime ( b . reserved _for , 'datetimeShort' ) }
< / span >
{ b . ref && < span style = { { fontSize : 12 , color : 'var(--muted)' } } > { b . ref } < / span > }
< / div >
< div style = { { fontSize : 15 , fontWeight : 700 , color : 'var(--text)' } } >
{ b . guest _name } · { t ( 'bookings.people' , { count : b . party _size } ) }
{ b . table _label && < span style = { { color : 'var(--muted)' , fontWeight : 500 } } > · { b . table _label } < / span > }
< / div >
{ b . phone && < a href = { ` tel: ${ b . phone } ` } style = { { fontSize : 14 , color : 'var(--accent)' } } > { b . phone } < / a > }
{ b . note && < div style = { { fontSize : 13 , color : 'var(--x-warning-ink)' , background : 'var(--x-warning-soft)' , borderRadius : 8 , padding : '6px 10px' } } > { b . note } < / div > }
{ b . lines ? . length > 0 && (
< div style = { { fontSize : 13 , color : 'var(--text2)' , borderTop : '1px solid var(--border)' , paddingTop : 6 } } >
< div style = { { fontWeight : 700 , marginBottom : 2 } } >
{ b . order _id ? t ( 'bookings.foodSent' ) : b . fire _at ? t ( 'bookings.foodAt' , { time : formatTime ( b . fire _at ) } ) : t ( 'bookings.foodOnConfirm' ) }
< / div >
{ b . lines . map ( ( l , i ) => < div key = { i } > { l . quantity } × { l . name } < / div > ) }
< div style = { { fontWeight : 700 , marginTop : 2 } } > { formatMoney ( b . total ) } < / div >
< / div >
) }
{ children }
< / div >
)
}
function SeatControls ( { booking , free , busy , onSeat , onCancel } ) {
const { t } = useTranslation ( 'waiter' )
const [ table , setTable ] = useState ( '' )
const options = free . map ( tb => ( { id : tb . id , name : tb . label || ` T ${ tb . number } ` , seats : tb . seat _count } ) )
return (
< div style = { { display : 'flex' , flexDirection : 'column' , gap : 8 } } >
< select value = { table } onChange = { e => setTable ( e . target . value ) } aria - label = { t ( 'bookings.table' ) } data - testid = "booking-table"
style = { { height : 44 , borderRadius : 10 , border : '1px solid var(--border)' , background : 'var(--bg3)' , color : 'var(--text)' , padding : '0 10px' , fontSize : 15 } } >
< option value = "" > { t ( 'bookings.noTable' ) } < / option >
{ options . map ( o => (
< option key = { o . id } value = { o . id } >
{ o . name } { o . seats ? ` · ${ t ( 'seats.table' , { count : o . seats } )} ` : '' }
{ o . seats && booking . party _size > o . seats ? ' ⚠' : '' }
< / option >
) ) }
< / select >
< button className = "btn btn--accent" disabled = { busy } onClick = { ( ) => onSeat ( table ? Number ( table ) : null ) } data - testid = "booking-arrive" >
{ t ( 'bookings.arrive' ) }
< / button >
< div style = { { display : 'flex' , gap : 8 } } >
< button className = "btn btn--secondary" style = { { flex : 1 } } disabled = { busy } onClick = { ( ) => onCancel ( 'cancelled' ) } data - testid = "booking-cancel" > { t ( 'bookings.cancel' ) } < / button >
< button className = "btn btn--secondary" style = { { flex : 1 } } disabled = { busy } onClick = { ( ) => onCancel ( 'no_show' ) } data - testid = "booking-noshow" > { t ( 'bookings.noShow' ) } < / button >
< / div >
< / div >
)
}
function DeclineSheet ( { booking , busy , onClose , onDecline } ) {
const { t } = useTranslation ( 'waiter' )
const [ choice , setChoice ] = useState ( null )
const [ other , setOther ] = useState ( '' )
const options = DECLINE _REASONS . map ( r => ( { value : r , label : t ( ` bookings.reasons. ${ r } ` ) } ) )
const reason = reasonText ( choice , options , other )
return (
< div className = "modal-overlay" onClick = { busy ? undefined : onClose } >
< div className = "modal-sheet" onClick = { e => e . stopPropagation ( ) } data - testid = "decline-sheet" >
< div className = "modal-handle" / >
< h2 className = "modal-title" > { t ( 'bookings.declineTitle' , { name : booking . guest _name } ) } < / h2 >
< p style = { { textAlign : 'center' , color : 'var(--muted)' , fontSize : 13 , margin : '0 0 14px' } } > { t ( 'bookings.declineBody' ) } < / p >
< ReasonPicker options = { options } value = { choice } onChange = { setChoice } other = { other } onOther = { setOther }
otherLabel = { t ( 'bookings.reasons.other' ) } ariaLabel = { t ( 'bookings.reason' ) } testIdPrefix = "decline" / >
< div style = { { display : 'flex' , gap : 10 , marginTop : 16 } } >
< button className = "btn btn--secondary" style = { { flex : 1 } } onClick = { onClose } disabled = { busy } > { t ( 'common.back' ) } < / button >
< button className = "btn btn--danger" style = { { flex : 1 } } disabled = { ! reason || busy } onClick = { ( ) => onDecline ( reason ) } data - testid = "decline-confirm" >
{ t ( 'bookings.decline' ) }
< / button >
< / div >
< / div >
< / div >
)
}