fix: drawer open/close updates UI immediately without refresh
- Return null instead of throwing on 404 for drawer current query - Sync drawer session ID to null when drawer closes - Await query invalidation before closing dialog - Fix unused approvedBy lint error Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -121,7 +121,13 @@ export function transactionOptions(id: string | null) {
|
||||
export function currentDrawerOptions(locationId: string | null) {
|
||||
return queryOptions({
|
||||
queryKey: posKeys.drawer(locationId ?? ''),
|
||||
queryFn: () => api.get<DrawerSession>('/v1/drawer/current', { locationId }),
|
||||
queryFn: async (): Promise<DrawerSession | null> => {
|
||||
try {
|
||||
return await api.get<DrawerSession>('/v1/drawer/current', { locationId })
|
||||
} catch {
|
||||
return null // 404 = no open drawer
|
||||
}
|
||||
},
|
||||
enabled: !!locationId,
|
||||
retry: false,
|
||||
})
|
||||
|
||||
@@ -43,9 +43,9 @@ export function POSDrawerDialog({ open, onOpenChange, drawer }: POSDrawerDialogP
|
||||
locationId: locationId ?? undefined,
|
||||
openingBalance: parseFloat(openingBalance) || 0,
|
||||
}),
|
||||
onSuccess: (session) => {
|
||||
onSuccess: async (session) => {
|
||||
setDrawerSession(session.id)
|
||||
queryClient.invalidateQueries({ queryKey: posKeys.drawer(locationId ?? '') })
|
||||
await queryClient.invalidateQueries({ queryKey: posKeys.drawer(locationId ?? '') })
|
||||
toast.success('Drawer opened')
|
||||
onOpenChange(false)
|
||||
},
|
||||
@@ -58,9 +58,8 @@ export function POSDrawerDialog({ open, onOpenChange, drawer }: POSDrawerDialogP
|
||||
closingBalance: parseFloat(closingBalance) || 0,
|
||||
notes: notes || undefined,
|
||||
}),
|
||||
onSuccess: (session) => {
|
||||
onSuccess: async (session) => {
|
||||
setDrawerSession(null)
|
||||
queryClient.invalidateQueries({ queryKey: posKeys.drawer(locationId ?? '') })
|
||||
const overShort = parseFloat(session.overShort ?? '0')
|
||||
if (Math.abs(overShort) < 0.01) {
|
||||
toast.success('Drawer closed - balanced')
|
||||
@@ -68,6 +67,7 @@ export function POSDrawerDialog({ open, onOpenChange, drawer }: POSDrawerDialogP
|
||||
toast.warning(`Drawer closed - ${overShort > 0 ? 'over' : 'short'} $${Math.abs(overShort).toFixed(2)}`)
|
||||
}
|
||||
onOpenChange(false)
|
||||
await queryClient.invalidateQueries({ queryKey: posKeys.drawer(locationId ?? '') })
|
||||
},
|
||||
onError: (err) => toast.error(err.message),
|
||||
})
|
||||
|
||||
@@ -44,6 +44,8 @@ export function POSRegister() {
|
||||
useEffect(() => {
|
||||
if (drawer?.id && drawer.status === 'open') {
|
||||
setDrawerSession(drawer.id)
|
||||
} else {
|
||||
setDrawerSession(null)
|
||||
}
|
||||
}, [drawer, setDrawerSession])
|
||||
|
||||
|
||||
@@ -104,7 +104,7 @@ export const DrawerService = {
|
||||
return session ?? null
|
||||
},
|
||||
|
||||
async addAdjustment(db: PostgresJsDatabase<any>, sessionId: string, input: DrawerAdjustmentInput, createdBy: string, approvedBy?: string) {
|
||||
async addAdjustment(db: PostgresJsDatabase<any>, sessionId: string, input: DrawerAdjustmentInput, createdBy: string, _approvedBy?: string) {
|
||||
const session = await this.getById(db, sessionId)
|
||||
if (!session) throw new NotFoundError('Drawer session')
|
||||
if (session.status === 'closed') throw new ConflictError('Cannot adjust a closed drawer')
|
||||
|
||||
Reference in New Issue
Block a user