2021-01-08 22:12:43 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import tw from 'twin.macro';
|
2021-05-19 02:53:42 +00:00
|
|
|
import { useRouteMatch } from 'react-router-dom';
|
2021-01-08 22:12:43 +00:00
|
|
|
import { action, Action, Actions, createContextStore, useStoreActions } from 'easy-peasy';
|
|
|
|
import { Mount } from '@/api/admin/mounts/getMounts';
|
|
|
|
import getMount from '@/api/admin/mounts/getMount';
|
|
|
|
import AdminContentBlock from '@/components/admin/AdminContentBlock';
|
|
|
|
import Spinner from '@/components/elements/Spinner';
|
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
|
|
|
import { ApplicationStore } from '@/state';
|
2021-01-08 22:34:55 +00:00
|
|
|
import { boolean, object, string } from 'yup';
|
|
|
|
import updateMount from '@/api/admin/mounts/updateMount';
|
|
|
|
import AdminBox from '@/components/admin/AdminBox';
|
|
|
|
import Button from '@/components/elements/Button';
|
|
|
|
import Field from '@/components/elements/Field';
|
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
2021-01-15 01:13:21 +00:00
|
|
|
import { Field as FormikField, Form, Formik, FormikHelpers } from 'formik';
|
|
|
|
import Label from '@/components/elements/Label';
|
2021-01-08 22:12:43 +00:00
|
|
|
|
|
|
|
interface ctx {
|
|
|
|
mount: Mount | undefined;
|
|
|
|
setMount: Action<ctx, Mount | undefined>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const Context = createContextStore<ctx>({
|
|
|
|
mount: undefined,
|
|
|
|
|
|
|
|
setMount: action((state, payload) => {
|
|
|
|
state.mount = payload;
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
2021-01-08 22:34:55 +00:00
|
|
|
interface Values {
|
|
|
|
name: string;
|
|
|
|
description: string;
|
|
|
|
source: string;
|
|
|
|
target: string;
|
2021-01-15 01:13:21 +00:00
|
|
|
readOnly: string;
|
|
|
|
userMountable: string;
|
2021-01-08 22:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const EditInformationContainer = () => {
|
|
|
|
const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
2021-01-09 17:42:13 +00:00
|
|
|
|
2021-01-08 22:34:55 +00:00
|
|
|
const mount = Context.useStoreState(state => state.mount);
|
|
|
|
const setMount = Context.useStoreActions(actions => actions.setMount);
|
|
|
|
|
|
|
|
if (mount === undefined) {
|
|
|
|
return (
|
|
|
|
<></>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const submit = ({ name, description, source, target, readOnly, userMountable }: Values, { setSubmitting }: FormikHelpers<Values>) => {
|
2021-01-08 22:39:20 +00:00
|
|
|
clearFlashes('mount');
|
2021-01-08 22:34:55 +00:00
|
|
|
|
2021-01-15 01:13:21 +00:00
|
|
|
updateMount(mount.id, name, description, source, target, readOnly === '1', userMountable === '1')
|
|
|
|
.then(() => setMount({ ...mount, name, description, source, target, readOnly: readOnly === '1', userMountable: userMountable === '1' }))
|
2021-01-08 22:34:55 +00:00
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
2021-01-08 22:39:20 +00:00
|
|
|
clearAndAddHttpError({ key: 'mount', error });
|
2021-01-08 22:34:55 +00:00
|
|
|
})
|
|
|
|
.then(() => setSubmitting(false));
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Formik
|
|
|
|
onSubmit={submit}
|
|
|
|
initialValues={{
|
|
|
|
name: mount.name,
|
|
|
|
description: mount.description || '',
|
|
|
|
source: mount.source,
|
|
|
|
target: mount.target,
|
2021-01-15 01:13:21 +00:00
|
|
|
readOnly: mount.readOnly ? '1' : '0',
|
|
|
|
userMountable: mount.userMountable ? '1' : '0',
|
2021-01-08 22:34:55 +00:00
|
|
|
}}
|
|
|
|
validationSchema={object().shape({
|
|
|
|
name: string().required().min(1),
|
|
|
|
description: string().max(255, ''),
|
|
|
|
source: string().max(255, ''),
|
|
|
|
target: string().max(255, ''),
|
|
|
|
readOnly: boolean(),
|
|
|
|
userMountable: boolean(),
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{
|
|
|
|
({ isSubmitting, isValid }) => (
|
|
|
|
<React.Fragment>
|
2021-01-08 22:39:20 +00:00
|
|
|
<AdminBox title={'Edit Mount'} css={tw`relative`}>
|
2021-01-08 22:34:55 +00:00
|
|
|
<SpinnerOverlay visible={isSubmitting}/>
|
|
|
|
|
|
|
|
<Form css={tw`mb-0`}>
|
|
|
|
<div>
|
|
|
|
<Field
|
|
|
|
id={'name'}
|
|
|
|
name={'name'}
|
|
|
|
label={'Name'}
|
|
|
|
type={'text'}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div css={tw`mt-6`}>
|
|
|
|
<Field
|
|
|
|
id={'description'}
|
|
|
|
name={'description'}
|
|
|
|
label={'Description'}
|
|
|
|
type={'text'}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2021-01-10 18:40:30 +00:00
|
|
|
<div css={tw`md:w-full md:flex md:flex-row mt-6`}>
|
|
|
|
<div css={tw`md:w-full md:flex md:flex-col md:mr-4 mt-6 md:mt-0`}>
|
|
|
|
<Field
|
|
|
|
id={'source'}
|
|
|
|
name={'source'}
|
|
|
|
label={'Source'}
|
|
|
|
type={'text'}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div css={tw`md:w-full md:flex md:flex-col md:ml-4 mt-6 md:mt-0`}>
|
|
|
|
<Field
|
|
|
|
id={'target'}
|
|
|
|
name={'target'}
|
|
|
|
label={'Target'}
|
|
|
|
type={'text'}
|
|
|
|
/>
|
|
|
|
</div>
|
2021-01-08 22:34:55 +00:00
|
|
|
</div>
|
|
|
|
|
2021-01-15 01:13:21 +00:00
|
|
|
<div css={tw`md:w-full md:flex md:flex-row mt-6`}>
|
|
|
|
<div css={tw`md:w-full md:flex md:flex-col md:mr-4 mt-6 md:mt-0`}>
|
|
|
|
<Label htmlFor={'readOnly'}>Permissions</Label>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<label css={tw`inline-flex items-center mr-2`}>
|
|
|
|
<FormikField
|
|
|
|
name={'readOnly'}
|
|
|
|
type={'radio'}
|
|
|
|
value={'0'}
|
|
|
|
/>
|
|
|
|
<span css={tw`ml-2`}>Writable</span>
|
|
|
|
</label>
|
|
|
|
|
|
|
|
<label css={tw`inline-flex items-center ml-2`}>
|
|
|
|
<FormikField
|
|
|
|
name={'readOnly'}
|
|
|
|
type={'radio'}
|
|
|
|
value={'1'}
|
|
|
|
/>
|
|
|
|
<span css={tw`ml-2`}>Read Only</span>
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div css={tw`md:w-full md:flex md:flex-col md:ml-4 mt-6 md:mt-0`}>
|
|
|
|
<Label htmlFor={'userMountable'}>User Mountable</Label>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<label css={tw`inline-flex items-center mr-2`}>
|
|
|
|
<FormikField
|
|
|
|
name={'userMountable'}
|
|
|
|
type={'radio'}
|
|
|
|
value={'0'}
|
|
|
|
/>
|
|
|
|
<span css={tw`ml-2`}>Admin Only</span>
|
|
|
|
</label>
|
|
|
|
|
|
|
|
<label css={tw`inline-flex items-center ml-2`}>
|
|
|
|
<FormikField
|
|
|
|
name={'userMountable'}
|
|
|
|
type={'radio'}
|
|
|
|
value={'1'}
|
|
|
|
|
|
|
|
/>
|
|
|
|
<span css={tw`ml-2`}>Users</span>
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2021-01-09 17:42:13 +00:00
|
|
|
<div css={tw`w-full flex flex-row items-center mt-6`}>
|
|
|
|
<div css={tw`flex`}>
|
2021-05-19 02:53:42 +00:00
|
|
|
{/* <MountDeleteButton */}
|
|
|
|
{/* mountId={mount.id} */}
|
|
|
|
{/* onDeleted={() => history.push('/admin/mounts')} */}
|
|
|
|
{/* /> */}
|
2021-01-09 17:42:13 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div css={tw`flex ml-auto`}>
|
|
|
|
<Button type={'submit'} disabled={isSubmitting || !isValid}>
|
|
|
|
Save
|
|
|
|
</Button>
|
|
|
|
</div>
|
2021-01-08 22:34:55 +00:00
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</AdminBox>
|
|
|
|
</React.Fragment>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</Formik>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-01-08 22:12:43 +00:00
|
|
|
const MountEditContainer = () => {
|
|
|
|
const match = useRouteMatch<{ id?: string }>();
|
|
|
|
|
|
|
|
const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
|
|
|
const [ loading, setLoading ] = useState(true);
|
|
|
|
|
|
|
|
const mount = Context.useStoreState(state => state.mount);
|
|
|
|
const setMount = Context.useStoreActions(actions => actions.setMount);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
clearFlashes('mount');
|
|
|
|
|
|
|
|
getMount(Number(match.params?.id))
|
|
|
|
.then(mount => setMount(mount))
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
clearAndAddHttpError({ key: 'mount', error });
|
|
|
|
})
|
|
|
|
.then(() => setLoading(false));
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (loading || mount === undefined) {
|
|
|
|
return (
|
|
|
|
<AdminContentBlock>
|
|
|
|
<FlashMessageRender byKey={'mount'} 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={'Mount - ' + mount.name}>
|
|
|
|
<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`}>{mount.name}</h2>
|
2021-01-08 22:34:55 +00:00
|
|
|
{
|
|
|
|
(mount.description || '').length < 1 ?
|
|
|
|
<p css={tw`text-base text-neutral-400`}>
|
|
|
|
<span css={tw`italic`}>No description</span>
|
|
|
|
</p>
|
|
|
|
:
|
2021-01-10 18:34:14 +00:00
|
|
|
<p css={tw`text-base text-neutral-400 whitespace-nowrap overflow-ellipsis overflow-hidden`}>{mount.description}</p>
|
2021-01-08 22:34:55 +00:00
|
|
|
}
|
2021-01-08 22:12:43 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<FlashMessageRender byKey={'mount'} css={tw`mb-4`}/>
|
2021-01-08 22:34:55 +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>
|
|
|
|
<MountEditContainer/>
|
|
|
|
</Context.Provider>
|
2021-01-06 22:50:21 +00:00
|
|
|
);
|
|
|
|
};
|