2021-08-05 04:41:52 +00:00
|
|
|
import { action, Action, Actions, createContextStore, useStoreActions } from 'easy-peasy';
|
2021-01-08 22:12:43 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2021-06-06 20:08:39 +00:00
|
|
|
import { useHistory } from 'react-router';
|
2021-05-19 02:53:42 +00:00
|
|
|
import { useRouteMatch } from 'react-router-dom';
|
2021-08-05 04:41:52 +00:00
|
|
|
import tw from 'twin.macro';
|
2021-01-08 22:12:43 +00:00
|
|
|
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';
|
2021-08-05 04:41:52 +00:00
|
|
|
import MountDeleteButton from '@/components/admin/mounts/MountDeleteButton';
|
|
|
|
import MountForm from '@/components/admin/mounts/MountForm';
|
2021-01-08 22:12:43 +00:00
|
|
|
import { ApplicationStore } from '@/state';
|
2021-08-05 04:41:52 +00:00
|
|
|
import { FormikHelpers } from 'formik';
|
2021-01-08 22:34:55 +00:00
|
|
|
import updateMount from '@/api/admin/mounts/updateMount';
|
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-08-05 04:41:52 +00:00
|
|
|
const MountEditContainer = () => {
|
2021-06-06 20:08:39 +00:00
|
|
|
const history = useHistory();
|
|
|
|
|
2021-08-05 04:41:52 +00:00
|
|
|
const match = useRouteMatch<{ id?: string }>();
|
|
|
|
|
2021-01-08 22:34:55 +00:00
|
|
|
const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
2021-08-05 04:41:52 +00:00
|
|
|
const [ loading, setLoading ] = useState(true);
|
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);
|
|
|
|
|
2021-08-05 04:41:52 +00:00
|
|
|
const submit = ({ name, description, source, target, readOnly, userMountable }: any, { setSubmitting }: FormikHelpers<any>) => {
|
|
|
|
if (mount === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2021-01-08 22:34:55 +00:00
|
|
|
|
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));
|
|
|
|
};
|
|
|
|
|
2021-01-08 22:12:43 +00:00
|
|
|
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
|
|
|
|
2021-08-05 04:41:52 +00:00
|
|
|
<MountForm
|
2021-10-03 23:10:37 +00:00
|
|
|
action={'Save Changes'}
|
2021-08-05 04:41:52 +00:00
|
|
|
title={'Edit Mount'}
|
|
|
|
initialValues={{
|
|
|
|
name: mount.name,
|
|
|
|
description: mount.description || '',
|
|
|
|
source: mount.source,
|
|
|
|
target: mount.target,
|
|
|
|
readOnly: mount.readOnly ? '1' : '0',
|
|
|
|
userMountable: mount.userMountable ? '1' : '0',
|
|
|
|
}}
|
|
|
|
onSubmit={submit}
|
|
|
|
>
|
|
|
|
<div css={tw`flex`}>
|
|
|
|
<MountDeleteButton
|
|
|
|
mountId={mount.id}
|
|
|
|
onDeleted={() => history.push('/admin/mounts')}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</MountForm>
|
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
|
|
|
);
|
|
|
|
};
|