Oddělení přenačtení menu do vlastní komponenty
All checks were successful
ci/woodpecker/push/workflow Pipeline was successful
All checks were successful
ci/woodpecker/push/workflow Pipeline was successful
This commit is contained in:
105
client/src/components/modals/RefreshMenuModal.tsx
Normal file
105
client/src/components/modals/RefreshMenuModal.tsx
Normal file
@@ -0,0 +1,105 @@
|
||||
import { useRef, useState } from "react";
|
||||
import { Modal, Button, Alert } from "react-bootstrap";
|
||||
|
||||
type Props = {
|
||||
isOpen: boolean;
|
||||
onClose: () => void;
|
||||
};
|
||||
|
||||
/** Modální dialog pro přenačtení menu z restaurací. */
|
||||
export default function RefreshMenuModal({ isOpen, onClose }: Readonly<Props>) {
|
||||
const refreshPassRef = useRef<HTMLInputElement>(null);
|
||||
const refreshTypeRef = useRef<HTMLSelectElement>(null);
|
||||
const [refreshLoading, setRefreshLoading] = useState(false);
|
||||
const [refreshMessage, setRefreshMessage] = useState<{ type: 'success' | 'error', text: string } | null>(null);
|
||||
|
||||
const handleRefresh = async () => {
|
||||
const password = refreshPassRef.current?.value;
|
||||
const type = refreshTypeRef.current?.value;
|
||||
if (!password || !type) {
|
||||
setRefreshMessage({ type: 'error', text: 'Zadejte heslo a typ refresh.' });
|
||||
return;
|
||||
}
|
||||
|
||||
setRefreshLoading(true);
|
||||
setRefreshMessage(null);
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/food/refresh?type=${type}&heslo=${encodeURIComponent(password)}`);
|
||||
const data = await res.json();
|
||||
if (res.ok) {
|
||||
setRefreshMessage({ type: 'success', text: 'Uspesny fetch' });
|
||||
if (refreshPassRef.current) {
|
||||
// Clean hesla xd
|
||||
refreshPassRef.current.value = '';
|
||||
}
|
||||
} else {
|
||||
setRefreshMessage({ type: 'error', text: data.error || 'Chyba při obnovování jídelníčku.' });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error refreshing menu:', error);
|
||||
setRefreshMessage({ type: 'error', text: 'Chyba při obnovování jídelníčku.' });
|
||||
} finally {
|
||||
setRefreshLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleClose = () => {
|
||||
setRefreshMessage(null);
|
||||
onClose();
|
||||
};
|
||||
|
||||
return (
|
||||
<Modal show={isOpen} onHide={handleClose} size="lg">
|
||||
<Modal.Header closeButton>
|
||||
<Modal.Title><h2>Přenačtení menu</h2></Modal.Title>
|
||||
</Modal.Header>
|
||||
<Modal.Body>
|
||||
<p>Ruční refresh dat z restaurací.</p>
|
||||
|
||||
{refreshMessage && (
|
||||
<Alert variant={refreshMessage.type === 'success' ? 'success' : 'danger'}>
|
||||
{refreshMessage.text}
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
<div className="mb-3">
|
||||
Heslo: <input
|
||||
ref={refreshPassRef}
|
||||
type="password"
|
||||
placeholder="Zadejte heslo"
|
||||
className="form-control d-inline-block"
|
||||
style={{ width: 'auto', marginLeft: '10px' }}
|
||||
onKeyDown={e => e.stopPropagation()}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="mb-3">
|
||||
Typ refreshe: <select
|
||||
ref={refreshTypeRef}
|
||||
className="form-select d-inline-block"
|
||||
style={{ width: 'auto', marginLeft: '10px' }}
|
||||
defaultValue="week"
|
||||
>
|
||||
<option value="week">Týden</option>
|
||||
<option value="day">Den</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<Button
|
||||
variant="info"
|
||||
onClick={handleRefresh}
|
||||
disabled={refreshLoading}
|
||||
className="mb-3"
|
||||
>
|
||||
{refreshLoading ? 'Refreshing...' : 'Refresh'}
|
||||
</Button>
|
||||
</Modal.Body>
|
||||
<Modal.Footer>
|
||||
<Button variant="secondary" onClick={handleClose}>
|
||||
Zavřít
|
||||
</Button>
|
||||
</Modal.Footer>
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user