2020-11-29 21:46:35 +00:00
|
|
|
import { fileBitsToString } from '@/helpers';
|
|
|
|
import useFileManagerSwr from '@/plugins/useFileManagerSwr';
|
|
|
|
import Modal, { RequiredModalProps } from '@/components/elements/Modal';
|
|
|
|
import { Form, Formik, FormikHelpers } from 'formik';
|
|
|
|
import Field from '@/components/elements/Field';
|
|
|
|
import chmodFiles from '@/api/server/files/chmodFiles';
|
|
|
|
import { ServerContext } from '@/state/server';
|
|
|
|
import tw from 'twin.macro';
|
|
|
|
import Button from '@/components/elements/Button';
|
|
|
|
import useFlash from '@/plugins/useFlash';
|
|
|
|
|
|
|
|
interface FormikValues {
|
|
|
|
mode: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface File {
|
2022-06-26 19:13:52 +00:00
|
|
|
file: string;
|
|
|
|
mode: string;
|
2020-11-29 21:46:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type OwnProps = RequiredModalProps & { files: File[] };
|
|
|
|
|
|
|
|
const ChmodFileModal = ({ files, ...props }: OwnProps) => {
|
2022-11-25 20:25:03 +00:00
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
2020-11-29 21:46:35 +00:00
|
|
|
const { mutate } = useFileManagerSwr();
|
|
|
|
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
2022-11-25 20:25:03 +00:00
|
|
|
const directory = ServerContext.useStoreState(state => state.files.directory);
|
|
|
|
const setSelectedFiles = ServerContext.useStoreActions(actions => actions.files.setSelectedFiles);
|
2020-11-29 21:46:35 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
const submit = async ({ mode }: FormikValues, { setSubmitting }: FormikHelpers<FormikValues>) => {
|
2020-11-29 21:46:35 +00:00
|
|
|
clearFlashes('files');
|
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
await mutate(
|
|
|
|
data =>
|
|
|
|
data!.map(f =>
|
|
|
|
f.name === files[0]?.file ? { ...f, mode: fileBitsToString(mode, !f.isFile), modeBits: mode } : f,
|
2022-06-26 19:13:52 +00:00
|
|
|
),
|
2022-11-25 20:25:03 +00:00
|
|
|
false,
|
2022-06-26 19:13:52 +00:00
|
|
|
);
|
2020-11-29 21:46:35 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
const data = files.map(f => ({ file: f.file, mode: mode }));
|
2020-11-29 21:46:35 +00:00
|
|
|
|
|
|
|
chmodFiles(uuid, directory, data)
|
2022-06-26 19:13:52 +00:00
|
|
|
.then((): Promise<any> => (files.length > 0 ? mutate() : Promise.resolve()))
|
2020-11-29 21:46:35 +00:00
|
|
|
.then(() => setSelectedFiles([]))
|
2022-11-25 20:25:03 +00:00
|
|
|
.catch(error => {
|
2020-11-29 21:46:35 +00:00
|
|
|
mutate();
|
|
|
|
setSubmitting(false);
|
|
|
|
clearAndAddHttpError({ key: 'files', error });
|
|
|
|
})
|
|
|
|
.then(() => props.onDismissed());
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2022-11-25 20:25:03 +00:00
|
|
|
<Formik onSubmit={submit} initialValues={{ mode: files.length > 1 ? '' : files[0]?.mode ?? '' }}>
|
2020-11-29 21:46:35 +00:00
|
|
|
{({ isSubmitting }) => (
|
|
|
|
<Modal {...props} dismissable={!isSubmitting} showSpinnerOverlay={isSubmitting}>
|
|
|
|
<Form css={tw`m-0`}>
|
|
|
|
<div css={tw`flex flex-wrap items-end`}>
|
|
|
|
<div css={tw`w-full sm:flex-1 sm:mr-4`}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<Field type={'string'} id={'file_mode'} name={'mode'} label={'File Mode'} autoFocus />
|
2020-11-29 21:46:35 +00:00
|
|
|
</div>
|
|
|
|
<div css={tw`w-full sm:w-auto mt-4 sm:mt-0`}>
|
|
|
|
<Button css={tw`w-full`}>Update</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</Modal>
|
|
|
|
)}
|
|
|
|
</Formik>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ChmodFileModal;
|