|
1 | 1 | import React from 'react'; |
2 | 2 | import { ServerContext } from '@/state/server'; |
3 | 3 | import TitledGreyBox from '@/components/elements/TitledGreyBox'; |
4 | | -import { Form, FormikProps, withFormik } from 'formik'; |
5 | | -import { Server } from '@/api/server/getServer'; |
6 | | -import { ActionCreator } from 'easy-peasy'; |
| 4 | +import { Form, Formik, FormikHelpers, useFormikContext } from 'formik'; |
| 5 | +import { Actions, useStoreActions } from 'easy-peasy'; |
7 | 6 | import renameServer from '@/api/server/renameServer'; |
8 | 7 | import Field from '@/components/elements/Field'; |
9 | 8 | import { object, string } from 'yup'; |
10 | 9 | import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; |
11 | | - |
12 | | -interface OwnProps { |
13 | | - server: Server; |
14 | | - setServer: ActionCreator<Server>; |
15 | | -} |
| 10 | +import { ApplicationStore } from '@/state'; |
| 11 | +import { httpErrorToHuman } from '@/api/http'; |
16 | 12 |
|
17 | 13 | interface Values { |
18 | 14 | name: string; |
19 | 15 | } |
20 | 16 |
|
21 | | -const RenameServerBox = ({ isSubmitting, ...props }: OwnProps & FormikProps<Values>) => ( |
22 | | - <TitledGreyBox title={'Change Server Name'} className={'relative'}> |
23 | | - <SpinnerOverlay size={'normal'} visible={isSubmitting}/> |
24 | | - <Form className={'mb-0'}> |
25 | | - <Field |
26 | | - id={'name'} |
27 | | - name={'name'} |
28 | | - label={'Server Name'} |
29 | | - type={'text'} |
30 | | - /> |
31 | | - <div className={'mt-6 text-right'}> |
32 | | - <button type={'submit'} className={'btn btn-sm btn-primary'}> |
33 | | - Save |
34 | | - </button> |
35 | | - </div> |
36 | | - </Form> |
37 | | - </TitledGreyBox> |
38 | | -); |
39 | | - |
40 | | -const EnhancedForm = withFormik<OwnProps, Values>({ |
41 | | - displayName: 'RenameServerBoxForm', |
| 17 | +const RenameServerBox = () => { |
| 18 | + const { isSubmitting } = useFormikContext<Values>(); |
42 | 19 |
|
43 | | - mapPropsToValues: props => ({ |
44 | | - name: props.server.name, |
45 | | - }), |
| 20 | + return ( |
| 21 | + <TitledGreyBox title={'Change Server Name'} className={'relative'}> |
| 22 | + <SpinnerOverlay size={'normal'} visible={isSubmitting}/> |
| 23 | + <Form className={'mb-0'}> |
| 24 | + <Field |
| 25 | + id={'name'} |
| 26 | + name={'name'} |
| 27 | + label={'Server Name'} |
| 28 | + type={'text'} |
| 29 | + /> |
| 30 | + <div className={'mt-6 text-right'}> |
| 31 | + <button type={'submit'} className={'btn btn-sm btn-primary'}> |
| 32 | + Save |
| 33 | + </button> |
| 34 | + </div> |
| 35 | + </Form> |
| 36 | + </TitledGreyBox> |
| 37 | + ); |
| 38 | +}; |
46 | 39 |
|
47 | | - validationSchema: () => object().shape({ |
48 | | - name: string().required().min(1), |
49 | | - }), |
| 40 | +export default () => { |
| 41 | + const server = ServerContext.useStoreState(state => state.server.data!); |
| 42 | + const setServer = ServerContext.useStoreActions(actions => actions.server.setServer); |
| 43 | + const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes); |
50 | 44 |
|
51 | | - handleSubmit: (values, { props, setSubmitting }) => { |
52 | | - renameServer(props.server.uuid, values.name) |
53 | | - .then(() => props.setServer({ ...props.server, name: values.name })) |
| 45 | + const submit = ({ name }: Values, { setSubmitting }: FormikHelpers<Values>) => { |
| 46 | + clearFlashes('settings'); |
| 47 | + renameServer(server.uuid, name) |
| 48 | + .then(() => setServer({ ...server, name })) |
54 | 49 | .catch(error => { |
55 | 50 | console.error(error); |
| 51 | + addError({ key: 'settings', message: httpErrorToHuman(error) }); |
56 | 52 | }) |
57 | 53 | .then(() => setSubmitting(false)); |
58 | | - }, |
59 | | -})(RenameServerBox); |
60 | | - |
61 | | -export default () => { |
62 | | - const server = ServerContext.useStoreState(state => state.server.data!); |
63 | | - const setServer = ServerContext.useStoreActions(actions => actions.server.setServer); |
| 54 | + }; |
64 | 55 |
|
65 | | - return <EnhancedForm server={server} setServer={setServer}/>; |
| 56 | + return ( |
| 57 | + <Formik |
| 58 | + onSubmit={submit} |
| 59 | + initialValues={{ |
| 60 | + name: server.name, |
| 61 | + }} |
| 62 | + validationSchema={object().shape({ |
| 63 | + name: string().required().min(1), |
| 64 | + })} |
| 65 | + > |
| 66 | + <RenameServerBox/> |
| 67 | + </Formik> |
| 68 | + ); |
66 | 69 | }; |
0 commit comments