ui(admin): add "working" React admin ui
This commit is contained in:
parent
d1c7494933
commit
5402584508
199 changed files with 13387 additions and 151 deletions
|
@ -0,0 +1,74 @@
|
|||
import type { Actions } from 'easy-peasy';
|
||||
import { useStoreActions } from 'easy-peasy';
|
||||
import { useState } from 'react';
|
||||
import tw from 'twin.macro';
|
||||
|
||||
import deleteLocation from '@/api/admin/locations/deleteLocation';
|
||||
import Button from '@/components/elements/Button';
|
||||
import ConfirmationModal from '@/components/elements/ConfirmationModal';
|
||||
import type { ApplicationStore } from '@/state';
|
||||
|
||||
interface Props {
|
||||
locationId: number;
|
||||
onDeleted: () => void;
|
||||
}
|
||||
|
||||
export default ({ locationId, onDeleted }: Props) => {
|
||||
const [visible, setVisible] = useState(false);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const { clearFlashes, clearAndAddHttpError } = useStoreActions(
|
||||
(actions: Actions<ApplicationStore>) => actions.flashes,
|
||||
);
|
||||
|
||||
const onDelete = () => {
|
||||
setLoading(true);
|
||||
clearFlashes('location');
|
||||
|
||||
deleteLocation(locationId)
|
||||
.then(() => {
|
||||
setLoading(false);
|
||||
onDeleted();
|
||||
})
|
||||
.catch(error => {
|
||||
console.error(error);
|
||||
clearAndAddHttpError({ key: 'location', error });
|
||||
|
||||
setLoading(false);
|
||||
setVisible(false);
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<ConfirmationModal
|
||||
visible={visible}
|
||||
title={'Delete location?'}
|
||||
buttonText={'Yes, delete location'}
|
||||
onConfirmed={onDelete}
|
||||
showSpinnerOverlay={loading}
|
||||
onModalDismissed={() => setVisible(false)}
|
||||
>
|
||||
Are you sure you want to delete this location? You may only delete a location if no nodes are assigned
|
||||
to it.
|
||||
</ConfirmationModal>
|
||||
|
||||
<Button type={'button'} size={'xsmall'} color={'red'} onClick={() => setVisible(true)}>
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
fill="none"
|
||||
viewBox="0 0 24 24"
|
||||
stroke="currentColor"
|
||||
css={tw`h-5 w-5`}
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={2}
|
||||
d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"
|
||||
/>
|
||||
</svg>
|
||||
</Button>
|
||||
</>
|
||||
);
|
||||
};
|
|
@ -0,0 +1,180 @@
|
|||
import type { Action, Actions } from 'easy-peasy';
|
||||
import { action, createContextStore, useStoreActions } from 'easy-peasy';
|
||||
import type { FormikHelpers } from 'formik';
|
||||
import { Form, Formik } from 'formik';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useNavigate, useParams } from 'react-router-dom';
|
||||
import tw from 'twin.macro';
|
||||
import { object, string } from 'yup';
|
||||
|
||||
import type { Location } from '@/api/admin/locations/getLocations';
|
||||
import getLocation from '@/api/admin/locations/getLocation';
|
||||
import updateLocation from '@/api/admin/locations/updateLocation';
|
||||
import AdminBox from '@/components/admin/AdminBox';
|
||||
import AdminContentBlock from '@/components/admin/AdminContentBlock';
|
||||
import LocationDeleteButton from '@/components/admin/locations/LocationDeleteButton';
|
||||
import Button from '@/components/elements/Button';
|
||||
import Field from '@/components/elements/Field';
|
||||
import Spinner from '@/components/elements/Spinner';
|
||||
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
||||
import FlashMessageRender from '@/components/FlashMessageRender';
|
||||
import type { ApplicationStore } from '@/state';
|
||||
|
||||
interface ctx {
|
||||
location: Location | undefined;
|
||||
setLocation: Action<ctx, Location | undefined>;
|
||||
}
|
||||
|
||||
export const Context = createContextStore<ctx>({
|
||||
location: undefined,
|
||||
|
||||
setLocation: action((state, payload) => {
|
||||
state.location = payload;
|
||||
}),
|
||||
});
|
||||
|
||||
interface Values {
|
||||
short: string;
|
||||
long: string;
|
||||
}
|
||||
|
||||
const EditInformationContainer = () => {
|
||||
const navigate = useNavigate();
|
||||
|
||||
const { clearFlashes, clearAndAddHttpError } = useStoreActions(
|
||||
(actions: Actions<ApplicationStore>) => actions.flashes,
|
||||
);
|
||||
|
||||
const location = Context.useStoreState(state => state.location);
|
||||
const setLocation = Context.useStoreActions(actions => actions.setLocation);
|
||||
|
||||
if (location === undefined) {
|
||||
return <></>;
|
||||
}
|
||||
|
||||
const submit = ({ short, long }: Values, { setSubmitting }: FormikHelpers<Values>) => {
|
||||
clearFlashes('location');
|
||||
|
||||
updateLocation(location.id, short, long)
|
||||
.then(() => setLocation({ ...location, short, long }))
|
||||
.catch(error => {
|
||||
console.error(error);
|
||||
clearAndAddHttpError({ key: 'location', error });
|
||||
})
|
||||
.then(() => setSubmitting(false));
|
||||
};
|
||||
|
||||
return (
|
||||
<Formik
|
||||
onSubmit={submit}
|
||||
initialValues={{
|
||||
short: location.short,
|
||||
long: location.long || '',
|
||||
}}
|
||||
validationSchema={object().shape({
|
||||
short: string().required().min(1),
|
||||
long: string().max(255, ''),
|
||||
})}
|
||||
>
|
||||
{({ isSubmitting, isValid }) => (
|
||||
<>
|
||||
<AdminBox title={'Edit Location'} css={tw`relative`}>
|
||||
<SpinnerOverlay visible={isSubmitting} />
|
||||
|
||||
<Form css={tw`mb-0`}>
|
||||
<div>
|
||||
<Field id={'short'} name={'short'} label={'Short Name'} type={'text'} />
|
||||
</div>
|
||||
|
||||
<div css={tw`mt-6`}>
|
||||
<Field id={'long'} name={'long'} label={'Long Name'} type={'text'} />
|
||||
</div>
|
||||
|
||||
<div css={tw`w-full flex flex-row items-center mt-6`}>
|
||||
<div css={tw`flex`}>
|
||||
<LocationDeleteButton
|
||||
locationId={location.id}
|
||||
onDeleted={() => navigate('/admin/locations')}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div css={tw`flex ml-auto`}>
|
||||
<Button type={'submit'} disabled={isSubmitting || !isValid}>
|
||||
Save Changes
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</Form>
|
||||
</AdminBox>
|
||||
</>
|
||||
)}
|
||||
</Formik>
|
||||
);
|
||||
};
|
||||
|
||||
const LocationEditContainer = () => {
|
||||
const params = useParams<'id'>();
|
||||
|
||||
const { clearFlashes, clearAndAddHttpError } = useStoreActions(
|
||||
(actions: Actions<ApplicationStore>) => actions.flashes,
|
||||
);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
const location = Context.useStoreState(state => state.location);
|
||||
const setLocation = Context.useStoreActions(actions => actions.setLocation);
|
||||
|
||||
useEffect(() => {
|
||||
clearFlashes('location');
|
||||
|
||||
getLocation(Number(params.id))
|
||||
.then(location => setLocation(location))
|
||||
.catch(error => {
|
||||
console.error(error);
|
||||
clearAndAddHttpError({ key: 'location', error });
|
||||
})
|
||||
.then(() => setLoading(false));
|
||||
}, []);
|
||||
|
||||
if (loading || location === undefined) {
|
||||
return (
|
||||
<AdminContentBlock>
|
||||
<FlashMessageRender byKey={'location'} css={tw`mb-4`} />
|
||||
|
||||
<div css={tw`w-full flex flex-col items-center justify-center`} style={{ height: '24rem' }}>
|
||||
<Spinner size={'base'} />
|
||||
</div>
|
||||
</AdminContentBlock>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<AdminContentBlock title={'Location - ' + location.short}>
|
||||
<div css={tw`w-full flex flex-row items-center mb-8`}>
|
||||
<div css={tw`flex flex-col flex-shrink`} style={{ minWidth: '0' }}>
|
||||
<h2 css={tw`text-2xl text-neutral-50 font-header font-medium`}>{location.short}</h2>
|
||||
{(location.long || '').length < 1 ? (
|
||||
<p css={tw`text-base text-neutral-400`}>
|
||||
<span css={tw`italic`}>No long name</span>
|
||||
</p>
|
||||
) : (
|
||||
<p css={tw`text-base text-neutral-400 whitespace-nowrap overflow-ellipsis overflow-hidden`}>
|
||||
{location.long}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<FlashMessageRender byKey={'location'} css={tw`mb-4`} />
|
||||
|
||||
<EditInformationContainer />
|
||||
</AdminContentBlock>
|
||||
);
|
||||
};
|
||||
|
||||
export default () => {
|
||||
return (
|
||||
<Context.Provider>
|
||||
<LocationEditContainer />
|
||||
</Context.Provider>
|
||||
);
|
||||
};
|
|
@ -0,0 +1,186 @@
|
|||
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 (
|
||||
<AdminCheckbox
|
||||
name={id.toString()}
|
||||
checked={isChecked}
|
||||
onChange={(e: ChangeEvent<HTMLInputElement>) => {
|
||||
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<HTMLInputElement>) => {
|
||||
setSelectedLocations(e.currentTarget.checked ? locations?.items?.map(location => location.id) || [] : []);
|
||||
};
|
||||
|
||||
const onSearch = (query: string): Promise<void> => {
|
||||
return new Promise(resolve => {
|
||||
if (query.length < 2) {
|
||||
setFilters(null);
|
||||
} else {
|
||||
setFilters({ short: query });
|
||||
}
|
||||
return resolve();
|
||||
});
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
setSelectedLocations([]);
|
||||
}, [page]);
|
||||
|
||||
return (
|
||||
<AdminContentBlock title={'Locations'}>
|
||||
<div css={tw`w-full flex flex-row items-center mb-8`}>
|
||||
<div css={tw`flex flex-col flex-shrink`} style={{ minWidth: '0' }}>
|
||||
<h2 css={tw`text-2xl text-neutral-50 font-header font-medium`}>Locations</h2>
|
||||
<p css={tw`text-base text-neutral-400 whitespace-nowrap overflow-ellipsis overflow-hidden`}>
|
||||
All locations that nodes can be assigned to for easier categorization.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div css={tw`flex ml-auto pl-4`}>
|
||||
<NewLocationButton />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<FlashMessageRender byKey={'locations'} css={tw`mb-4`} />
|
||||
|
||||
<AdminTable>
|
||||
<ContentWrapper
|
||||
checked={selectedLocationsLength === (length === 0 ? -1 : length)}
|
||||
onSelectAllClick={onSelectAllClick}
|
||||
onSearch={onSearch}
|
||||
>
|
||||
<Pagination data={locations} onPageSelect={setPage}>
|
||||
<div css={tw`overflow-x-auto`}>
|
||||
<table css={tw`w-full table-auto`}>
|
||||
<TableHead>
|
||||
<TableHeader
|
||||
name={'ID'}
|
||||
direction={sort === 'id' ? (sortDirection ? 1 : 2) : null}
|
||||
onClick={() => setSort('id')}
|
||||
/>
|
||||
<TableHeader
|
||||
name={'Short Name'}
|
||||
direction={sort === 'short' ? (sortDirection ? 1 : 2) : null}
|
||||
onClick={() => setSort('short')}
|
||||
/>
|
||||
<TableHeader
|
||||
name={'Long Name'}
|
||||
direction={sort === 'long' ? (sortDirection ? 1 : 2) : null}
|
||||
onClick={() => setSort('long')}
|
||||
/>
|
||||
</TableHead>
|
||||
|
||||
<TableBody>
|
||||
{locations !== undefined &&
|
||||
!error &&
|
||||
!isValidating &&
|
||||
length > 0 &&
|
||||
locations.items.map(location => (
|
||||
<TableRow key={location.id}>
|
||||
<td css={tw`pl-6`}>
|
||||
<RowCheckbox id={location.id} />
|
||||
</td>
|
||||
|
||||
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>
|
||||
<CopyOnClick text={location.id.toString()}>
|
||||
<code css={tw`font-mono bg-neutral-900 rounded py-1 px-2`}>
|
||||
{location.id}
|
||||
</code>
|
||||
</CopyOnClick>
|
||||
</td>
|
||||
|
||||
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>
|
||||
<NavLink
|
||||
to={`/admin/locations/${location.id}`}
|
||||
css={tw`text-primary-400 hover:text-primary-300`}
|
||||
>
|
||||
{location.short}
|
||||
</NavLink>
|
||||
</td>
|
||||
|
||||
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>
|
||||
{location.long}
|
||||
</td>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</table>
|
||||
|
||||
{locations === undefined || (error && isValidating) ? (
|
||||
<Loading />
|
||||
) : length < 1 ? (
|
||||
<NoItems />
|
||||
) : null}
|
||||
</div>
|
||||
</Pagination>
|
||||
</ContentWrapper>
|
||||
</AdminTable>
|
||||
</AdminContentBlock>
|
||||
);
|
||||
};
|
||||
|
||||
export default () => {
|
||||
const hooks = useTableHooks<Filters>();
|
||||
|
||||
return (
|
||||
<LocationsContext.Provider value={hooks}>
|
||||
<LocationsContainer />
|
||||
</LocationsContext.Provider>
|
||||
);
|
||||
};
|
|
@ -0,0 +1,112 @@
|
|||
import type { FormikHelpers } from 'formik';
|
||||
import { Form, Formik } from 'formik';
|
||||
import { useState } from 'react';
|
||||
import tw from 'twin.macro';
|
||||
import { object, string } from 'yup';
|
||||
|
||||
import createLocation from '@/api/admin/locations/createLocation';
|
||||
import getLocations from '@/api/admin/locations/getLocations';
|
||||
import Button from '@/components/elements/Button';
|
||||
import Field from '@/components/elements/Field';
|
||||
import Modal from '@/components/elements/Modal';
|
||||
import FlashMessageRender from '@/components/FlashMessageRender';
|
||||
import useFlash from '@/plugins/useFlash';
|
||||
|
||||
interface Values {
|
||||
short: string;
|
||||
long: string;
|
||||
}
|
||||
|
||||
const schema = object().shape({
|
||||
short: string()
|
||||
.required('A location short name must be provided.')
|
||||
.max(32, 'Location short name must not exceed 32 characters.'),
|
||||
long: string().max(255, 'Location long name must not exceed 255 characters.'),
|
||||
});
|
||||
|
||||
export default () => {
|
||||
const [visible, setVisible] = useState(false);
|
||||
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
||||
const { mutate } = getLocations();
|
||||
|
||||
const submit = ({ short, long }: Values, { setSubmitting }: FormikHelpers<Values>) => {
|
||||
clearFlashes('location:create');
|
||||
setSubmitting(true);
|
||||
|
||||
createLocation(short, long)
|
||||
.then(async location => {
|
||||
await mutate(data => ({ ...data!, items: data!.items.concat(location) }), false);
|
||||
setVisible(false);
|
||||
})
|
||||
.catch(error => {
|
||||
clearAndAddHttpError({ key: 'location:create', error });
|
||||
setSubmitting(false);
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<Formik onSubmit={submit} initialValues={{ short: '', long: '' }} validationSchema={schema}>
|
||||
{({ isSubmitting, resetForm }) => (
|
||||
<Modal
|
||||
visible={visible}
|
||||
dismissable={!isSubmitting}
|
||||
showSpinnerOverlay={isSubmitting}
|
||||
onDismissed={() => {
|
||||
resetForm();
|
||||
setVisible(false);
|
||||
}}
|
||||
>
|
||||
<FlashMessageRender byKey={'location:create'} css={tw`mb-6`} />
|
||||
|
||||
<h2 css={tw`mb-6 text-2xl text-neutral-100`}>New Location</h2>
|
||||
|
||||
<Form css={tw`m-0`}>
|
||||
<Field
|
||||
type={'text'}
|
||||
id={'short'}
|
||||
name={'short'}
|
||||
label={'Short'}
|
||||
description={'A short name used to identify this location.'}
|
||||
autoFocus
|
||||
/>
|
||||
|
||||
<div css={tw`mt-6`}>
|
||||
<Field
|
||||
type={'text'}
|
||||
id={'long'}
|
||||
name={'long'}
|
||||
label={'Long'}
|
||||
description={'A long name for this location.'}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div css={tw`flex flex-wrap justify-end mt-6`}>
|
||||
<Button
|
||||
type={'button'}
|
||||
isSecondary
|
||||
css={tw`w-full sm:w-auto sm:mr-2`}
|
||||
onClick={() => setVisible(false)}
|
||||
>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button css={tw`w-full mt-4 sm:w-auto sm:mt-0`} type={'submit'}>
|
||||
Create Location
|
||||
</Button>
|
||||
</div>
|
||||
</Form>
|
||||
</Modal>
|
||||
)}
|
||||
</Formik>
|
||||
|
||||
<Button
|
||||
type={'button'}
|
||||
size={'large'}
|
||||
css={tw`h-10 px-4 py-0 whitespace-nowrap`}
|
||||
onClick={() => setVisible(true)}
|
||||
>
|
||||
New Location
|
||||
</Button>
|
||||
</>
|
||||
);
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue