105 lines
3.5 KiB
TypeScript
105 lines
3.5 KiB
TypeScript
import { useState } from "react";
|
|
import { Modal, Button, Alert } from "react-bootstrap";
|
|
import { clearMockData, 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 smazání mock dat (pouze DEV). */
|
|
export default function ClearMockDataModal({ isOpen, onClose, currentDayIndex }: Readonly<Props>) {
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [success, setSuccess] = useState(false);
|
|
|
|
const handleClear = async () => {
|
|
setError(null);
|
|
setLoading(true);
|
|
|
|
try {
|
|
const body: any = {};
|
|
if (currentDayIndex !== undefined) {
|
|
body.dayIndex = currentDayIndex as DayIndex;
|
|
}
|
|
|
|
const response = await clearMockData({ body });
|
|
if (response.error) {
|
|
setError((response.error as any).error || 'Nastala chyba při mazání dat');
|
|
} else {
|
|
setSuccess(true);
|
|
setTimeout(() => {
|
|
onClose();
|
|
setSuccess(false);
|
|
}, 1500);
|
|
}
|
|
} catch (e: any) {
|
|
setError(e.message || 'Nastala chyba při mazání dat');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const handleClose = () => {
|
|
setError(null);
|
|
setSuccess(false);
|
|
onClose();
|
|
};
|
|
|
|
const dayName = currentDayIndex !== undefined ? DAY_NAMES[currentDayIndex] : 'aktuální den';
|
|
|
|
return (
|
|
<Modal show={isOpen} onHide={handleClose}>
|
|
<Modal.Header closeButton>
|
|
<Modal.Title><h2>Smazat data</h2></Modal.Title>
|
|
</Modal.Header>
|
|
<Modal.Body>
|
|
{success ? (
|
|
<Alert variant="success">
|
|
Data byla úspěšně smazá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>
|
|
)}
|
|
|
|
<p>
|
|
Opravdu chcete smazat všechny volby stravování pro <strong>{dayName}</strong>?
|
|
</p>
|
|
<p className="text-muted">
|
|
Tato akce je nevratná.
|
|
</p>
|
|
</>
|
|
)}
|
|
</Modal.Body>
|
|
<Modal.Footer>
|
|
{!success && (
|
|
<>
|
|
<Button variant="secondary" onClick={handleClose} disabled={loading}>
|
|
Ne, zrušit
|
|
</Button>
|
|
<Button variant="danger" onClick={handleClear} disabled={loading}>
|
|
{loading ? 'Mažu...' : 'Ano, smazat'}
|
|
</Button>
|
|
</>
|
|
)}
|
|
{success && (
|
|
<Button variant="secondary" onClick={handleClose}>
|
|
Zavřít
|
|
</Button>
|
|
)}
|
|
</Modal.Footer>
|
|
</Modal>
|
|
);
|
|
}
|