import { useState } from "react"; import { Modal, Button, Form, ListGroup, Alert } from "react-bootstrap"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faTrashCan } from "@fortawesome/free-regular-svg-icons"; import { faUpRightFromSquare } from "@fortawesome/free-solid-svg-icons"; import { addStore, deleteStore, Store } from "../../../../types"; type Props = { isOpen: boolean; onClose: () => void; stores: Store[]; onStoresChanged: (stores: Store[]) => void; }; export default function StoreAdminModal({ isOpen, onClose, stores, onStoresChanged }: Readonly) { const [newName, setNewName] = useState(''); const [newUrl, setNewUrl] = useState(''); const [heslo, setHeslo] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const handleAdd = async () => { if (!newName.trim()) return; setError(null); setLoading(true); try { const res = await addStore({ body: { name: newName.trim(), url: newUrl.trim() || undefined, heslo } }); if (res.error) { setError((res.error as any).error || 'Nastala chyba'); } else if (res.data) { onStoresChanged(res.data as Store[]); setNewName(''); setNewUrl(''); } } catch (e: any) { setError(e.message || 'Nastala chyba'); } finally { setLoading(false); } }; const handleRemove = async (name: string) => { setError(null); setLoading(true); try { const res = await deleteStore({ body: { name, heslo } }); if (res.error) { setError((res.error as any).error || 'Nastala chyba'); } else if (res.data) { onStoresChanged(res.data as Store[]); } } catch (e: any) { setError(e.message || 'Nastala chyba'); } finally { setLoading(false); } }; return (

Správa obchodů

{error && ( setError(null)} dismissible> {error} )} Admin heslo setHeslo(e.target.value)} onKeyDown={e => e.stopPropagation()} />
Přidat obchod
setNewName(e.target.value)} onKeyDown={e => { e.stopPropagation(); if (e.key === 'Enter') handleAdd(); }} />
setNewUrl(e.target.value)} onKeyDown={e => { e.stopPropagation(); if (e.key === 'Enter') handleAdd(); }} />
Aktuální seznam
{stores.length === 0 ? (

Žádné obchody v seznamu

) : ( {stores.map(s => ( {s.name} {s.url && /^https?:\/\//i.test(s.url) && ( )} handleRemove(s.name)} style={{ cursor: 'pointer' }} /> ))} )}
); }