- CRM -> Customers: search ignoring case and Greek accents (and part of the phone), visits / spent / last visit, the card (contact, notes, open tab, every visit with what was on a tab), new/edit/remove - Inventory -> Throwaways: log (product, quantity with a comma, reason, note) with its cost, today's list (delete today only), a period's totals by reason and product, every entry; Inventory -> Stock: an honest 'not available yet' - Notes: notes (pinned first, edit in place) and to-dos (urgent first, done folded away, who did it kept) - backend: crm.manage / inventory.manage / notes.use (the customer search also with the dashboard key, for orders); spending counted by services/sales.py (refunds off, tab lines in); null clears a field; names/notes not blank, priorities checked; waste ?date_from=&date_to= venue dates; the Greek reason labels left the API; live customers.changed / waste.changed / notes.changed - the overview's notes card needs notes.use and refreshes live - tests: test_customers_waste_notes.py (4), crm.test.mjs (3), e2e manager.mjs (customers, throwaways, notes, stock, Greek) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
96 lines
5.9 KiB
React
96 lines
5.9 KiB
React
// CRM → Customers (plan step 6.13b; replaces the v1 CustomersPage): the venue's regular
|
|
// customers. Search by name, nickname, phone, e-mail or notes (case and Greek accents ignored);
|
|
// each with visits, what they spent (counted as in the reports: refunds off, tab lines in) and
|
|
// their last visit. A tap opens their card: contact, notes (allergies, likes), their open tab,
|
|
// every visit. New, edit, remove (a removed customer stays on old orders). Live: `customers.changed`.
|
|
import { useDeferredValue, useState } from 'react'
|
|
import { useMutation } from '@tanstack/react-query'
|
|
import toast from 'react-hot-toast'
|
|
import { Pencil, Plus, Search, Trash2, Users } from 'lucide-react'
|
|
import { errorMessage } from '@xenia/api'
|
|
import { useFormat, useTranslation } from '@xenia/i18n'
|
|
import { Button, Dialog, EmptyState, IconButton, Input, Spinner } from '@xenia/ui'
|
|
import client from '../../api/client'
|
|
import { useLiveQuery } from '../../live/useLiveQuery'
|
|
import { Avatar } from '../overview/parts'
|
|
import { Panel, Table } from '../reports/kit/parts'
|
|
import CustomerCard from './CustomerCard'
|
|
import CustomerDialog from './CustomerDialog'
|
|
|
|
export default function CustomersPage() {
|
|
const { t } = useTranslation('manager')
|
|
const fmt = useFormat()
|
|
const [q, setQ] = useState('')
|
|
const search = useDeferredValue(q.trim())
|
|
const [dialog, setDialog] = useState(null) // { kind: 'new' | 'edit' | 'card' | 'remove', customer }
|
|
const { data, isPending, refetch } = useLiveQuery({
|
|
queryKey: ['customers', search], placeholderData: prev => prev,
|
|
queryFn: () => client.get('/api/customers/', { params: search ? { search } : {} }).then(r => r.data),
|
|
events: ['customers.changed', 'order_closed', 'order_paid'],
|
|
})
|
|
const rows = data ?? []
|
|
const close = () => setDialog(null)
|
|
const saved = () => { close(); refetch() }
|
|
const remove = useMutation({
|
|
mutationFn: c => client.delete(`/api/customers/${c.id}`),
|
|
onSuccess: (_r, c) => { toast.success(t('customers.removed', { name: c.name })); saved() },
|
|
onError: err => toast.error(errorMessage(err, t)),
|
|
})
|
|
const stop = fn => ev => { ev.stopPropagation(); fn() }
|
|
|
|
const columns = [
|
|
{ key: 'name', label: t('customers.name'), render: c => (
|
|
<span className="flex items-center gap-2">
|
|
<Avatar name={c.name} size="sm" />
|
|
<span className="grid">
|
|
<span className="font-semibold text-ink">{c.name}{c.nickname && <span className="font-normal text-ink-2"> «{c.nickname}»</span>}</span>
|
|
{c.notes && <span className="text-caption text-ink-2 line-clamp-1">{c.notes}</span>}
|
|
</span>
|
|
</span>) },
|
|
{ key: 'phone', label: t('customers.phone'), render: c => (c.phone ? <a href={`tel:${c.phone.replace(/\s/g, '')}`} onClick={ev => ev.stopPropagation()} className="text-accent-ink hover:underline">{c.phone}</a> : '') },
|
|
{ key: 'visit_count', label: t('customers.visits'), align: 'right' },
|
|
{ key: 'total_spent', label: t('customers.spent'), align: 'right', tip: t('customers.spentTip'), render: c => (c.total_spent ? fmt.formatMoney(c.total_spent) : '') },
|
|
{ key: 'last_visit', label: t('customers.lastVisit'), sort: c => c.last_visit ?? '', render: c => (c.last_visit ? fmt.formatDate(c.last_visit) : '') },
|
|
{ key: 'actions', label: '', sort: false, render: c => (
|
|
<span className="flex justify-end gap-0.5">
|
|
<IconButton size="sm" variant="ghost" label={t('customers.editOf', { name: c.name })} onClick={stop(() => setDialog({ kind: 'edit', customer: c }))}><Pencil size={15} /></IconButton>
|
|
<IconButton size="sm" variant="ghost" label={t('customers.removeOf', { name: c.name })} onClick={stop(() => setDialog({ kind: 'remove', customer: c }))}><Trash2 size={15} /></IconButton>
|
|
</span>) },
|
|
]
|
|
|
|
return (
|
|
<div className="h-full overflow-y-auto p-4 lg:p-6" data-testid="customers-page">
|
|
<div className="max-w-6xl mx-auto grid gap-4">
|
|
<header className="flex flex-wrap items-center gap-3">
|
|
<div className="flex-1 min-w-0">
|
|
<h1 className="text-title font-semibold text-ink">{t('customers.title')}</h1>
|
|
<p className="text-label text-ink-2">{t('customers.subtitle')}</p>
|
|
</div>
|
|
<Button icon={<Plus size={16} />} onClick={() => setDialog({ kind: 'new' })} data-testid="customer-new">{t('customers.new')}</Button>
|
|
</header>
|
|
<div className="relative">
|
|
<Search size={16} className="absolute left-3 top-1/2 -translate-y-1/2 text-ink-3" aria-hidden="true" />
|
|
<Input value={q} onChange={e => setQ(e.target.value)} placeholder={t('customers.search')} aria-label={t('customers.search')} className="pl-9" data-testid="customer-search" />
|
|
</div>
|
|
{isPending ? <div className="py-10 flex justify-center"><Spinner /></div>
|
|
: !rows.length && !search ? <EmptyState icon={<Users size={28} />} title={t('customers.none')} description={t('customers.noneHint')} />
|
|
: (
|
|
<Panel padded={false}>
|
|
<Table columns={columns} rows={rows} onRow={c => setDialog({ kind: 'card', customer: c })} rowTestId="customer-row"
|
|
empty={t('customers.noMatch')} initialSort={{ key: 'name', dir: 'asc' }} />
|
|
</Panel>
|
|
)}
|
|
</div>
|
|
{(dialog?.kind === 'new' || dialog?.kind === 'edit') && <CustomerDialog customer={dialog.customer} onClose={close} onSaved={saved} />}
|
|
{dialog?.kind === 'card' && <CustomerCard customer={dialog.customer} onClose={close} onEdit={() => setDialog({ kind: 'edit', customer: dialog.customer })} />}
|
|
{dialog?.kind === 'remove' && (
|
|
<Dialog open size="sm" onClose={close} title={t('customers.removeTitle', { name: dialog.customer.name })} description={t('customers.removeBody')}
|
|
footer={<>
|
|
<Button variant="secondary" onClick={close}>{t('customers.back')}</Button>
|
|
<Button variant="danger" loading={remove.isPending} onClick={() => remove.mutate(dialog.customer)}>{t('customers.removeYes')}</Button>
|
|
</>} />
|
|
)}
|
|
</div>
|
|
)
|
|
}
|