import { useCallback, useEffect, useState } from 'react' import { api } from '../api.js' import { atLeast, useAuth } from '../auth.js' import { useT } from '../i18n.js' import { ErrorNote } from '../components/ui.jsx' export default function Settings() { const t = useT() const { user } = useAuth() const admin = atLeast(user, 'admin') const [cats, setCats] = useState([]) const [sites, setSites] = useState([]) const [depts, setDepts] = useState([]) const [deptId, setDeptId] = useState(user.department_id || '') const [copySource, setCopySource] = useState('defaults') const [notice, setNotice] = useState(null) const [error, setError] = useState(null) const [newCat, setNewCat] = useState('') const [newSite, setNewSite] = useState('') useEffect(() => { api.get('/api/departments').then((ds) => { setDepts(ds) setDeptId((cur) => cur || ds[0]?.id || '') }) }, []) const load = useCallback(() => { if (deptId) api.get(`/api/categories?department_id=${deptId}`).then(setCats) api.get('/api/sites').then(setSites) }, [deptId]) useEffect(() => { load() }, [load]) const editableDepts = admin ? depts : depts.filter((d) => d.id === user.department_id) const run = async (fn) => { try { await fn() setError(null) } catch (e) { setError(e) } load() } const putCat = (c, patch) => run(() => api.put(`/api/categories/${c.id}`, patch)) const putSite = (s, patch) => run(() => api.put(`/api/sites/${s.id}`, patch)) return (
| {t('order')} | {t('name_en')} | {t('name_pt')} | {t('section')} | {t('active')} | |
|---|---|---|---|---|---|
| putCat(c, { active: e.target.checked })} aria-label={t('active')} /> |