import React, { useContext, useEffect, useState } from 'react'; import getLocations, { Context as LocationsContext, Filters } 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, 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: React.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 [ page, setPage ] = useState(1); const [ filters, setFilters ] = useState(null); const [ sort, setSortState ] = useState(null); const [ sortDirection, setSortDirection ] = useState(false); const setSort = (newSort: string | null) => { if (sort === newSort) { setSortDirection(!sortDirection); } else { setSortState(newSort); setSortDirection(false); } }; return ( ); };