import type { ChangeEvent } from 'react'; import { useContext, useEffect } from 'react'; import { NavLink } from 'react-router-dom'; import tw from 'twin.macro'; import type { Filters } from '@/api/admin/locations/getLocations'; import getLocations, { Context as LocationsContext } from '@/api/admin/locations/getLocations'; import AdminContentBlock from '@/components/admin/AdminContentBlock'; import AdminCheckbox from '@/components/admin/AdminCheckbox'; import AdminTable, { TableBody, TableHead, TableHeader, TableRow, Pagination, Loading, NoItems, ContentWrapper, useTableHooks, } from '@/components/admin/AdminTable'; import NewLocationButton from '@/components/admin/locations/NewLocationButton'; import CopyOnClick from '@/components/elements/CopyOnClick'; import FlashMessageRender from '@/components/FlashMessageRender'; import useFlash from '@/plugins/useFlash'; import { AdminContext } from '@/state/admin'; const RowCheckbox = ({ id }: { id: number }) => { const isChecked = AdminContext.useStoreState(state => state.locations.selectedLocations.indexOf(id) >= 0); const appendSelectedLocation = AdminContext.useStoreActions(actions => actions.locations.appendSelectedLocation); const removeSelectedLocation = AdminContext.useStoreActions(actions => actions.locations.removeSelectedLocation); return ( ) => { if (e.currentTarget.checked) { appendSelectedLocation(id); } else { removeSelectedLocation(id); } }} /> ); }; const LocationsContainer = () => { const { page, setPage, setFilters, sort, setSort, sortDirection } = useContext(LocationsContext); const { clearFlashes, clearAndAddHttpError } = useFlash(); const { data: locations, error, isValidating } = getLocations(); useEffect(() => { if (!error) { clearFlashes('locations'); return; } clearAndAddHttpError({ key: 'locations', error }); }, [error]); const length = locations?.items?.length || 0; const setSelectedLocations = AdminContext.useStoreActions(actions => actions.locations.setSelectedLocations); const selectedLocationsLength = AdminContext.useStoreState(state => state.locations.selectedLocations.length); const onSelectAllClick = (e: ChangeEvent) => { setSelectedLocations(e.currentTarget.checked ? locations?.items?.map(location => location.id) || [] : []); }; const onSearch = (query: string): Promise => { return new Promise(resolve => { if (query.length < 2) { setFilters(null); } else { setFilters({ short: query }); } return resolve(); }); }; useEffect(() => { setSelectedLocations([]); }, [page]); return (

Locations

All locations that nodes can be assigned to for easier categorization.

setSort('id')} /> setSort('short')} /> setSort('long')} /> {locations !== undefined && !error && !isValidating && length > 0 && locations.items.map(location => ( ))}
{location.id} {location.short} {location.long}
{locations === undefined || (error && isValidating) ? ( ) : length < 1 ? ( ) : null}
); }; export default () => { const hooks = useTableHooks(); return ( ); };