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 { faPlus, faUpRightFromSquare, faXmark } from "@fortawesome/free-solid-svg-icons"; import { addStore, deleteStore, Store } from "../../../../types"; import { getStoreUrls, storeUrlLabel } from "../../utils/storeUrls"; 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(''); // Jeden podnik může být dostupný přes více dovozových služeb — proto seznam URL const [newUrls, setNewUrls] = useState(['']); const [heslo, setHeslo] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const setUrlAt = (index: number, value: string) => setNewUrls(prev => prev.map((u, i) => (i === index ? value : u))); const addUrlRow = () => setNewUrls(prev => [...prev, '']); const removeUrlRow = (index: number) => setNewUrls(prev => (prev.length === 1 ? [''] : prev.filter((_, i) => i !== index))); const handleAdd = async () => { if (!newName.trim()) return; setError(null); setLoading(true); try { const urls = newUrls.map(u => u.trim()).filter(Boolean); const res = await addStore({ body: { name: newName.trim(), urls: urls.length > 0 ? urls : undefined, heslo } }); if (res.error) { setError((res.error as any).error || 'Nastala chyba'); } else if (res.data) { onStoresChanged(res.data as Store[]); setNewName(''); setNewUrls(['']); } } 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(); }} /> {newUrls.map((url, index) => (
setUrlAt(index, 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 => { const urls = getStoreUrls(s); return (
{s.name}
{urls.length > 0 && ( {urls.map(url => ( {storeUrlLabel(url)} ))} )}
handleRemove(s.name)} style={{ cursor: 'pointer' }} />
); })}
)}
); }