CI / Generate TypeScript types (push) Successful in 11s
CI / Server unit tests (push) Successful in 27s
CI / Build server (push) Successful in 29s
CI / Build client (push) Successful in 42s
CI / Playwright E2E tests (push) Successful in 1m43s
CI / Build and push Docker image (push) Successful in 46s
CI / Notify (push) Successful in 2s
176 lines
7.8 KiB
TypeScript
176 lines
7.8 KiB
TypeScript
import { useState } from "react";
|
|
import { Modal, Button, Form, Alert } from "react-bootstrap";
|
|
import { generateMockOrders } from "../../../../types";
|
|
|
|
type Props = {
|
|
isOpen: boolean;
|
|
onClose: () => void;
|
|
/** Příznak, zda je pro dnešní den založena alespoň jedna skupina objednávky. */
|
|
hasGroups: boolean;
|
|
/** Příznak, zda je nadefinován alespoň jeden obchod (bez něj nelze generovat). */
|
|
hasStores: boolean;
|
|
};
|
|
|
|
const DEFAULT_COUNT = '3';
|
|
const DEFAULT_MIN_AMOUNT = '100';
|
|
const DEFAULT_MAX_AMOUNT = '1000';
|
|
|
|
/** Vybere český tvar slova podle počtu (1 osoba / 2 osoby / 5 osob). */
|
|
function plural(count: number, one: string, few: string, many: string): string {
|
|
if (count === 1) return one;
|
|
if (count >= 2 && count <= 4) return few;
|
|
return many;
|
|
}
|
|
|
|
/** Modální dialog pro generování mock dat sekce Objednání (pouze DEV). */
|
|
export default function GenerateMockOrdersModal({ isOpen, onClose, hasGroups, hasStores }: Readonly<Props>) {
|
|
const [count, setCount] = useState<string>(DEFAULT_COUNT);
|
|
const [minAmount, setMinAmount] = useState<string>(DEFAULT_MIN_AMOUNT);
|
|
const [maxAmount, setMaxAmount] = useState<string>(DEFAULT_MAX_AMOUNT);
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [success, setSuccess] = useState<string | null>(null);
|
|
|
|
const handleGenerate = async () => {
|
|
setError(null);
|
|
|
|
const countNum = parseInt(count, 10);
|
|
if (isNaN(countNum) || countNum < 1 || countNum > 50) {
|
|
setError('Počet osob musí být číslo mezi 1 a 50');
|
|
return;
|
|
}
|
|
const minNum = parseInt(minAmount, 10);
|
|
const maxNum = parseInt(maxAmount, 10);
|
|
if (isNaN(minNum) || isNaN(maxNum) || minNum < 0 || maxNum < minNum) {
|
|
setError('Zadejte platný rozsah částek (od ≤ do)');
|
|
return;
|
|
}
|
|
|
|
setLoading(true);
|
|
try {
|
|
const response = await generateMockOrders({
|
|
body: { count: countNum, minAmount: minNum, maxAmount: maxNum },
|
|
});
|
|
if (response.error) {
|
|
setError((response.error as any).error || 'Nastala chyba při generování dat');
|
|
} else {
|
|
const groups = response.data?.groups ?? [];
|
|
const total = response.data?.count ?? 0;
|
|
const created = groups.find(g => g.created);
|
|
setSuccess(
|
|
`Doplněno: ${total} ${plural(total, 'osoba', 'osoby', 'osob')}`
|
|
+ ` / ${groups.length} ${plural(groups.length, 'skupina', 'skupiny', 'skupin')}`
|
|
+ (created ? ` (skupina „${created.name}“ byla nově založena).` : '.')
|
|
);
|
|
setTimeout(() => {
|
|
onClose();
|
|
setSuccess(null);
|
|
}, 2000);
|
|
}
|
|
} catch (e: any) {
|
|
setError(e.message || 'Nastala chyba při generování dat');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const handleClose = () => {
|
|
setError(null);
|
|
setSuccess(null);
|
|
onClose();
|
|
};
|
|
|
|
return (
|
|
<Modal show={isOpen} onHide={handleClose}>
|
|
<Modal.Header closeButton>
|
|
<Modal.Title><h2>Generovat mock objednávky</h2></Modal.Title>
|
|
</Modal.Header>
|
|
<Modal.Body>
|
|
{success ? (
|
|
<Alert variant="success">{success}</Alert>
|
|
) : (
|
|
<>
|
|
<Alert variant="warning">
|
|
<strong>DEV režim</strong> - Tato funkce je dostupná pouze ve vývojovém prostředí.
|
|
Data se generují vždy jen pro aktuální den.
|
|
</Alert>
|
|
|
|
{error && (
|
|
<Alert variant="danger" onClose={() => setError(null)} dismissible>
|
|
{error}
|
|
</Alert>
|
|
)}
|
|
|
|
{hasStores ? (
|
|
<>
|
|
<Form.Group className="mb-3">
|
|
<Form.Label>Počet osob</Form.Label>
|
|
<Form.Control
|
|
type="number"
|
|
value={count}
|
|
onChange={e => setCount(e.target.value)}
|
|
min={1}
|
|
max={50}
|
|
onKeyDown={e => e.stopPropagation()}
|
|
/>
|
|
<Form.Text className="text-muted">
|
|
{hasGroups
|
|
? 'Zadaný počet osob se doplní do každé existující skupiny.'
|
|
: 'Není založena žádná skupina — jedna se založí z náhodně vybraného obchodu a doplní se do ní zadaný počet osob.'}
|
|
</Form.Text>
|
|
</Form.Group>
|
|
|
|
<Form.Group className="mb-3">
|
|
<Form.Label>Rozsah částek (Kč)</Form.Label>
|
|
<div className="d-flex gap-2 align-items-center">
|
|
<Form.Control
|
|
type="number"
|
|
value={minAmount}
|
|
onChange={e => setMinAmount(e.target.value)}
|
|
min={0}
|
|
onKeyDown={e => e.stopPropagation()}
|
|
/>
|
|
<span>—</span>
|
|
<Form.Control
|
|
type="number"
|
|
value={maxAmount}
|
|
onChange={e => setMaxAmount(e.target.value)}
|
|
min={0}
|
|
onKeyDown={e => e.stopPropagation()}
|
|
/>
|
|
</div>
|
|
<Form.Text className="text-muted">
|
|
Každé osobě se nastaví náhodná částka z tohoto rozmezí a do poznámky náhodné jídlo.
|
|
</Form.Text>
|
|
</Form.Group>
|
|
</>
|
|
) : (
|
|
<p className="text-muted mb-0">
|
|
Není nadefinován žádný obchod — mock objednávky nelze vygenerovat.
|
|
Nejprve přidejte obchod ve správě obchodů.
|
|
</p>
|
|
)}
|
|
</>
|
|
)}
|
|
</Modal.Body>
|
|
<Modal.Footer>
|
|
{!success && (
|
|
<>
|
|
<Button variant="secondary" onClick={handleClose} disabled={loading}>
|
|
Storno
|
|
</Button>
|
|
<Button variant="primary" onClick={handleGenerate} disabled={loading || !hasStores}>
|
|
{loading ? 'Generuji...' : 'Generovat'}
|
|
</Button>
|
|
</>
|
|
)}
|
|
{success && (
|
|
<Button variant="secondary" onClick={handleClose}>
|
|
Zavřít
|
|
</Button>
|
|
)}
|
|
</Modal.Footer>
|
|
</Modal>
|
|
);
|
|
}
|