All checks were successful
ci/woodpecker/push/workflow Pipeline was successful
141 lines
5.4 KiB
TypeScript
141 lines
5.4 KiB
TypeScript
import { useState } from "react";
|
|
import { Modal, Button, Form, Alert } from "react-bootstrap";
|
|
import { generateMockData, DayIndex } from "../../../../types";
|
|
|
|
type Props = {
|
|
isOpen: boolean;
|
|
onClose: () => void;
|
|
currentDayIndex?: number;
|
|
};
|
|
|
|
const DAY_NAMES = ['Pondělí', 'Úterý', 'Středa', 'Čtvrtek', 'Pátek'];
|
|
|
|
/** Modální dialog pro generování mock dat (pouze DEV). */
|
|
export default function GenerateMockDataModal({ isOpen, onClose, currentDayIndex }: Readonly<Props>) {
|
|
const [dayIndex, setDayIndex] = useState<number | undefined>(currentDayIndex);
|
|
const [count, setCount] = useState<string>('');
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [success, setSuccess] = useState(false);
|
|
|
|
const handleGenerate = async () => {
|
|
setError(null);
|
|
setLoading(true);
|
|
|
|
try {
|
|
const body: any = {};
|
|
if (dayIndex !== undefined) {
|
|
body.dayIndex = dayIndex as DayIndex;
|
|
}
|
|
if (count && count.trim() !== '') {
|
|
const countNum = parseInt(count, 10);
|
|
if (isNaN(countNum) || countNum < 1 || countNum > 100) {
|
|
setError('Počet musí být číslo mezi 1 a 100');
|
|
setLoading(false);
|
|
return;
|
|
}
|
|
body.count = countNum;
|
|
}
|
|
|
|
const response = await generateMockData({ body });
|
|
if (response.error) {
|
|
setError((response.error as any).error || 'Nastala chyba při generování dat');
|
|
} else {
|
|
setSuccess(true);
|
|
setTimeout(() => {
|
|
onClose();
|
|
setSuccess(false);
|
|
setCount('');
|
|
}, 1500);
|
|
}
|
|
} catch (e: any) {
|
|
setError(e.message || 'Nastala chyba při generování dat');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const handleClose = () => {
|
|
setError(null);
|
|
setSuccess(false);
|
|
setCount('');
|
|
onClose();
|
|
};
|
|
|
|
return (
|
|
<Modal show={isOpen} onHide={handleClose}>
|
|
<Modal.Header closeButton>
|
|
<Modal.Title><h2>Generovat mock data</h2></Modal.Title>
|
|
</Modal.Header>
|
|
<Modal.Body>
|
|
{success ? (
|
|
<Alert variant="success">
|
|
Mock data byla úspěšně vygenerována!
|
|
</Alert>
|
|
) : (
|
|
<>
|
|
<Alert variant="warning">
|
|
<strong>DEV režim</strong> - Tato funkce je dostupná pouze ve vývojovém prostředí.
|
|
</Alert>
|
|
|
|
{error && (
|
|
<Alert variant="danger" onClose={() => setError(null)} dismissible>
|
|
{error}
|
|
</Alert>
|
|
)}
|
|
|
|
<Form.Group className="mb-3">
|
|
<Form.Label>Den</Form.Label>
|
|
<Form.Select
|
|
value={dayIndex ?? ''}
|
|
onChange={e => setDayIndex(e.target.value === '' ? undefined : parseInt(e.target.value, 10))}
|
|
>
|
|
<option value="">Aktuální den</option>
|
|
{DAY_NAMES.map((name, index) => (
|
|
<option key={index} value={index}>{name}</option>
|
|
))}
|
|
</Form.Select>
|
|
<Form.Text className="text-muted">
|
|
Pokud není vybráno, použije se aktuální den.
|
|
</Form.Text>
|
|
</Form.Group>
|
|
|
|
<Form.Group className="mb-3">
|
|
<Form.Label>Počet záznamů</Form.Label>
|
|
<Form.Control
|
|
type="number"
|
|
placeholder="Náhodný (5-20)"
|
|
value={count}
|
|
onChange={e => setCount(e.target.value)}
|
|
min={1}
|
|
max={100}
|
|
onKeyDown={e => e.stopPropagation()}
|
|
/>
|
|
<Form.Text className="text-muted">
|
|
Pokud není zadáno, vybere se náhodný počet 5-20.
|
|
</Form.Text>
|
|
</Form.Group>
|
|
</>
|
|
)}
|
|
</Modal.Body>
|
|
<Modal.Footer>
|
|
{!success && (
|
|
<>
|
|
<Button variant="secondary" onClick={handleClose} disabled={loading}>
|
|
Storno
|
|
</Button>
|
|
<Button variant="primary" onClick={handleGenerate} disabled={loading}>
|
|
{loading ? 'Generuji...' : 'Generovat'}
|
|
</Button>
|
|
</>
|
|
)}
|
|
{success && (
|
|
<Button variant="secondary" onClick={handleClose}>
|
|
Zavřít
|
|
</Button>
|
|
)}
|
|
</Modal.Footer>
|
|
</Modal>
|
|
);
|
|
}
|