2021-01-08 22:12:43 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import tw from 'twin.macro';
|
2021-01-09 17:32:34 +00:00
|
|
|
import { useHistory, useRouteMatch } from 'react-router-dom';
|
2021-01-08 22:12:43 +00:00
|
|
|
import { action, Action, Actions, createContextStore, useStoreActions } from 'easy-peasy';
|
|
|
|
import { Location } from '@/api/admin/locations/getLocations';
|
|
|
|
import getLocation from '@/api/admin/locations/getLocation';
|
|
|
|
import AdminContentBlock from '@/components/admin/AdminContentBlock';
|
|
|
|
import Spinner from '@/components/elements/Spinner';
|
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
|
|
|
import { ApplicationStore } from '@/state';
|
2021-01-09 16:27:58 +00:00
|
|
|
import { object, string } from 'yup';
|
|
|
|
import AdminBox from '@/components/admin/AdminBox';
|
|
|
|
import Button from '@/components/elements/Button';
|
|
|
|
import Field from '@/components/elements/Field';
|
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
|
|
|
import { Form, Formik, FormikHelpers } from 'formik';
|
|
|
|
import updateLocation from '@/api/admin/locations/updateLocation';
|
2021-01-09 17:42:13 +00:00
|
|
|
import LocationDeleteButton from '@/components/admin/locations/LocationDeleteButton';
|
2021-01-08 22:12:43 +00:00
|
|
|
|
|
|
|
interface ctx {
|
|
|
|
location: Location | undefined;
|
|
|
|
setLocation: Action<ctx, Location | undefined>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const Context = createContextStore<ctx>({
|
|
|
|
location: undefined,
|
|
|
|
|
|
|
|
setLocation: action((state, payload) => {
|
|
|
|
state.location = payload;
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
2021-01-09 16:27:58 +00:00
|
|
|
interface Values {
|
|
|
|
short: string;
|
|
|
|
long: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const EditInformationContainer = () => {
|
2021-01-09 17:32:34 +00:00
|
|
|
const history = useHistory();
|
|
|
|
|
2021-01-09 16:27:58 +00:00
|
|
|
const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
2021-01-09 17:32:34 +00:00
|
|
|
|
2021-01-09 16:27:58 +00:00
|
|
|
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 }) => (
|
|
|
|
<React.Fragment>
|
|
|
|
<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>
|
|
|
|
|
2021-01-09 17:32:34 +00:00
|
|
|
<div css={tw`w-full flex flex-row items-center mt-6`}>
|
|
|
|
<div css={tw`flex`}>
|
|
|
|
<LocationDeleteButton
|
|
|
|
locationId={location.id}
|
|
|
|
onDeleted={() => history.push('/admin/locations')}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div css={tw`flex ml-auto`}>
|
|
|
|
<Button type={'submit'} disabled={isSubmitting || !isValid}>
|
|
|
|
Save
|
|
|
|
</Button>
|
|
|
|
</div>
|
2021-01-09 16:27:58 +00:00
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</AdminBox>
|
|
|
|
</React.Fragment>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</Formik>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-01-08 22:12:43 +00:00
|
|
|
const LocationEditContainer = () => {
|
|
|
|
const match = useRouteMatch<{ id?: string }>();
|
|
|
|
|
|
|
|
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(match.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`}>
|
2021-01-10 18:34:14 +00:00
|
|
|
<div css={tw`flex flex-col flex-shrink`} style={{ minWidth: '0' }}>
|
2021-01-08 22:12:43 +00:00
|
|
|
<h2 css={tw`text-2xl text-neutral-50 font-header font-medium`}>{location.short}</h2>
|
2021-01-09 16:27:58 +00:00
|
|
|
{
|
|
|
|
(location.long || '').length < 1 ?
|
|
|
|
<p css={tw`text-base text-neutral-400`}>
|
|
|
|
<span css={tw`italic`}>No long name</span>
|
|
|
|
</p>
|
|
|
|
:
|
2021-01-10 18:34:14 +00:00
|
|
|
<p css={tw`text-base text-neutral-400 whitespace-nowrap overflow-ellipsis overflow-hidden`}>{location.long}</p>
|
2021-01-09 16:27:58 +00:00
|
|
|
}
|
2021-01-08 22:12:43 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<FlashMessageRender byKey={'location'} css={tw`mb-4`}/>
|
2021-01-09 16:27:58 +00:00
|
|
|
|
|
|
|
<EditInformationContainer/>
|
2021-01-08 22:12:43 +00:00
|
|
|
</AdminContentBlock>
|
|
|
|
);
|
|
|
|
};
|
2021-01-06 22:50:21 +00:00
|
|
|
|
|
|
|
export default () => {
|
|
|
|
return (
|
2021-01-08 22:12:43 +00:00
|
|
|
<Context.Provider>
|
|
|
|
<LocationEditContainer/>
|
|
|
|
</Context.Provider>
|
2021-01-06 22:50:21 +00:00
|
|
|
);
|
|
|
|
};
|