import React, { useContext, useEffect, useState } from 'react'; import getLocations, { Context as LocationsContext } from '@/api/admin/locations/getLocations'; import FlashMessageRender from '@/components/FlashMessageRender'; import useFlash from '@/plugins/useFlash'; import { AdminContext } from '@/state/admin'; import { NavLink, useRouteMatch } from 'react-router-dom'; import tw from 'twin.macro'; import AdminContentBlock from '@/components/admin/AdminContentBlock'; import AdminCheckbox from '@/components/admin/AdminCheckbox'; import AdminTable, { TableBody, TableHead, TableHeader, TableRow, Pagination, Loading, NoItems, ContentWrapper } from '@/components/admin/AdminTable'; import NewLocationButton from '@/components/admin/locations/NewLocationButton'; import CopyOnClick from '@/components/elements/CopyOnClick'; 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 match = useRouteMatch(); const { page, setPage } = useContext(LocationsContext); const { clearFlashes, clearAndAddHttpError } = useFlash(); const { data: locations, error, isValidating } = getLocations(); useEffect(() => { if (!error) { clearFlashes('locations'); return; } clearAndAddHttpError({ error, key: 'locations' }); }, [ 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: React.ChangeEvent) => { setSelectedLocations(e.currentTarget.checked ? (locations?.items?.map(location => location.id) || []) : []); }; useEffect(() => { setSelectedLocations([]); }, [ page ]); return (

Locations

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

{ locations === undefined || (error && isValidating) ? : length < 1 ? :
{ locations.items.map(location => ( )) }
{location.id} {location.short} {location.long}
}
); }; export default () => { const [ page, setPage ] = useState(1); return ( ); };