|
| 1 | +import { fileBitsToString } from '@/helpers'; |
| 2 | +import useFileManagerSwr from '@/plugins/useFileManagerSwr'; |
| 3 | +import React from 'react'; |
| 4 | +import Modal, { RequiredModalProps } from '@/components/elements/Modal'; |
| 5 | +import { Form, Formik, FormikHelpers } from 'formik'; |
| 6 | +import Field from '@/components/elements/Field'; |
| 7 | +import chmodFiles from '@/api/server/files/chmodFiles'; |
| 8 | +import { ServerContext } from '@/state/server'; |
| 9 | +import tw from 'twin.macro'; |
| 10 | +import Button from '@/components/elements/Button'; |
| 11 | +import useFlash from '@/plugins/useFlash'; |
| 12 | + |
| 13 | +interface FormikValues { |
| 14 | + mode: string; |
| 15 | +} |
| 16 | + |
| 17 | +interface File { |
| 18 | + file: string, |
| 19 | + mode: string, |
| 20 | +} |
| 21 | + |
| 22 | +type OwnProps = RequiredModalProps & { files: File[] }; |
| 23 | + |
| 24 | +const ChmodFileModal = ({ files, ...props }: OwnProps) => { |
| 25 | + const uuid = ServerContext.useStoreState(state => state.server.data!.uuid); |
| 26 | + const { mutate } = useFileManagerSwr(); |
| 27 | + const { clearFlashes, clearAndAddHttpError } = useFlash(); |
| 28 | + const directory = ServerContext.useStoreState(state => state.files.directory); |
| 29 | + const setSelectedFiles = ServerContext.useStoreActions(actions => actions.files.setSelectedFiles); |
| 30 | + |
| 31 | + const submit = ({ mode }: FormikValues, { setSubmitting }: FormikHelpers<FormikValues>) => { |
| 32 | + clearFlashes('files'); |
| 33 | + |
| 34 | + mutate(data => data.map(f => f.name === files[0].file ? { ...f, mode: fileBitsToString(mode, !f.isFile), modeBits: mode } : f), false); |
| 35 | + |
| 36 | + const data = files.map(f => ({ file: f.file, mode: mode })); |
| 37 | + |
| 38 | + chmodFiles(uuid, directory, data) |
| 39 | + .then((): Promise<any> => files.length > 0 ? mutate() : Promise.resolve()) |
| 40 | + .then(() => setSelectedFiles([])) |
| 41 | + .catch(error => { |
| 42 | + mutate(); |
| 43 | + setSubmitting(false); |
| 44 | + clearAndAddHttpError({ key: 'files', error }); |
| 45 | + }) |
| 46 | + .then(() => props.onDismissed()); |
| 47 | + }; |
| 48 | + |
| 49 | + return ( |
| 50 | + <Formik onSubmit={submit} initialValues={{ mode: files.length > 1 ? '' : (files[0].mode || '') }}> |
| 51 | + {({ isSubmitting }) => ( |
| 52 | + <Modal {...props} dismissable={!isSubmitting} showSpinnerOverlay={isSubmitting}> |
| 53 | + <Form css={tw`m-0`}> |
| 54 | + <div css={tw`flex flex-wrap items-end`}> |
| 55 | + <div css={tw`w-full sm:flex-1 sm:mr-4`}> |
| 56 | + <Field |
| 57 | + type={'string'} |
| 58 | + id={'file_mode'} |
| 59 | + name={'mode'} |
| 60 | + label={'File Mode'} |
| 61 | + autoFocus |
| 62 | + /> |
| 63 | + </div> |
| 64 | + <div css={tw`w-full sm:w-auto mt-4 sm:mt-0`}> |
| 65 | + <Button css={tw`w-full`}>Update</Button> |
| 66 | + </div> |
| 67 | + </div> |
| 68 | + </Form> |
| 69 | + </Modal> |
| 70 | + )} |
| 71 | + </Formik> |
| 72 | + ); |
| 73 | +}; |
| 74 | + |
| 75 | +export default ChmodFileModal; |
0 commit comments