2020-04-04 17:59:25 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import Modal, { RequiredModalProps } from '@/components/elements/Modal';
|
|
|
|
import { Field as FormikField, Form, Formik, FormikHelpers, useFormikContext } from 'formik';
|
2021-05-04 04:26:09 +00:00
|
|
|
import { boolean, object, string } from 'yup';
|
2020-04-04 17:59:25 +00:00
|
|
|
import Field from '@/components/elements/Field';
|
|
|
|
import FormikFieldWrapper from '@/components/elements/FormikFieldWrapper';
|
|
|
|
import useFlash from '@/plugins/useFlash';
|
|
|
|
import createServerBackup from '@/api/server/backups/createServerBackup';
|
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
2020-07-04 23:26:07 +00:00
|
|
|
import Button from '@/components/elements/Button';
|
|
|
|
import tw from 'twin.macro';
|
2020-07-05 01:46:09 +00:00
|
|
|
import { Textarea } from '@/components/elements/Input';
|
2020-08-21 04:44:33 +00:00
|
|
|
import getServerBackups from '@/api/swr/getServerBackups';
|
2020-08-26 05:09:54 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
2021-05-04 04:26:09 +00:00
|
|
|
import FormikSwitch from '@/components/elements/FormikSwitch';
|
|
|
|
import Can from '@/components/elements/Can';
|
2020-04-04 17:59:25 +00:00
|
|
|
|
|
|
|
interface Values {
|
|
|
|
name: string;
|
|
|
|
ignored: string;
|
2021-05-04 04:26:09 +00:00
|
|
|
isLocked: boolean;
|
2020-04-04 17:59:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const ModalContent = ({ ...props }: RequiredModalProps) => {
|
|
|
|
const { isSubmitting } = useFormikContext<Values>();
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal {...props} showSpinnerOverlay={isSubmitting}>
|
2020-07-04 23:26:07 +00:00
|
|
|
<Form>
|
2022-06-26 19:13:52 +00:00
|
|
|
<FlashMessageRender byKey={'backups:create'} css={tw`mb-4`} />
|
2020-07-04 23:26:07 +00:00
|
|
|
<h2 css={tw`text-2xl mb-6`}>Create server backup</h2>
|
2021-05-04 04:26:09 +00:00
|
|
|
<Field
|
|
|
|
name={'name'}
|
|
|
|
label={'Backup name'}
|
|
|
|
description={'If provided, the name that should be used to reference this backup.'}
|
|
|
|
/>
|
|
|
|
<div css={tw`mt-6`}>
|
2020-04-04 17:59:25 +00:00
|
|
|
<FormikFieldWrapper
|
2020-04-19 06:26:59 +00:00
|
|
|
name={'ignored'}
|
2020-04-04 17:59:25 +00:00
|
|
|
label={'Ignored Files & Directories'}
|
|
|
|
description={`
|
|
|
|
Enter the files or folders to ignore while generating this backup. Leave blank to use
|
|
|
|
the contents of the .pteroignore file in the root of the server directory if present.
|
|
|
|
Wildcard matching of files and folders is supported in addition to negating a rule by
|
|
|
|
prefixing the path with an exclamation point.
|
|
|
|
`}
|
|
|
|
>
|
2022-06-26 19:13:52 +00:00
|
|
|
<FormikField as={Textarea} name={'ignored'} rows={6} />
|
2020-04-04 17:59:25 +00:00
|
|
|
</FormikFieldWrapper>
|
|
|
|
</div>
|
2021-05-04 04:26:09 +00:00
|
|
|
<Can action={'backup.delete'}>
|
|
|
|
<div css={tw`mt-6 bg-neutral-700 border border-neutral-800 shadow-inner p-4 rounded`}>
|
|
|
|
<FormikSwitch
|
|
|
|
name={'isLocked'}
|
|
|
|
label={'Locked'}
|
|
|
|
description={'Prevents this backup from being deleted until explicitly unlocked.'}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Can>
|
|
|
|
<div css={tw`flex justify-end mt-6`}>
|
2020-08-02 02:49:38 +00:00
|
|
|
<Button type={'submit'} disabled={isSubmitting}>
|
2020-04-04 17:59:25 +00:00
|
|
|
Start backup
|
2020-07-04 23:26:07 +00:00
|
|
|
</Button>
|
2020-04-04 17:59:25 +00:00
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-04-07 05:25:54 +00:00
|
|
|
export default () => {
|
2022-06-26 19:13:52 +00:00
|
|
|
const uuid = ServerContext.useStoreState((state) => state.server.data!.uuid);
|
2020-08-21 04:44:33 +00:00
|
|
|
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
2022-06-26 19:13:52 +00:00
|
|
|
const [visible, setVisible] = useState(false);
|
2020-08-21 04:44:33 +00:00
|
|
|
const { mutate } = getServerBackups();
|
2020-04-07 05:25:54 +00:00
|
|
|
|
2020-04-04 17:59:25 +00:00
|
|
|
useEffect(() => {
|
|
|
|
clearFlashes('backups:create');
|
2022-06-26 19:13:52 +00:00
|
|
|
}, [visible]);
|
2020-04-04 17:59:25 +00:00
|
|
|
|
2021-05-04 04:26:09 +00:00
|
|
|
const submit = (values: Values, { setSubmitting }: FormikHelpers<Values>) => {
|
2020-04-07 05:25:54 +00:00
|
|
|
clearFlashes('backups:create');
|
2021-05-04 04:26:09 +00:00
|
|
|
createServerBackup(uuid, values)
|
2022-06-26 19:13:52 +00:00
|
|
|
.then((backup) => {
|
|
|
|
mutate(
|
|
|
|
(data) => ({ ...data, items: data.items.concat(backup), backupCount: data.backupCount + 1 }),
|
|
|
|
false
|
|
|
|
);
|
2020-04-04 17:59:25 +00:00
|
|
|
setVisible(false);
|
|
|
|
})
|
2022-06-26 19:13:52 +00:00
|
|
|
.catch((error) => {
|
2020-08-21 04:44:33 +00:00
|
|
|
clearAndAddHttpError({ key: 'backups:create', error });
|
2020-04-04 17:59:25 +00:00
|
|
|
setSubmitting(false);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-06-26 19:13:52 +00:00
|
|
|
{visible && (
|
|
|
|
<Formik
|
|
|
|
onSubmit={submit}
|
|
|
|
initialValues={{ name: '', ignored: '', isLocked: false }}
|
|
|
|
validationSchema={object().shape({
|
|
|
|
name: string().max(191),
|
|
|
|
ignored: string(),
|
|
|
|
isLocked: boolean(),
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<ModalContent appear visible={visible} onDismissed={() => setVisible(false)} />
|
|
|
|
</Formik>
|
|
|
|
)}
|
2020-10-03 23:25:39 +00:00
|
|
|
<Button css={tw`w-full sm:w-auto`} onClick={() => setVisible(true)}>
|
2020-04-04 17:59:25 +00:00
|
|
|
Create backup
|
2020-07-04 23:26:07 +00:00
|
|
|
</Button>
|
2020-04-04 17:59:25 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|