2020-08-09 08:48:43 +02:00
|
|
|
import React, { useState } from 'react';
|
2020-09-06 02:27:01 +02:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2020-08-09 08:48:43 +02:00
|
|
|
import { Formik, Form, Field } from 'formik';
|
|
|
|
import { post } from 'lib/web';
|
|
|
|
import Button from 'components/common/Button';
|
|
|
|
import FormLayout, {
|
|
|
|
FormButtons,
|
|
|
|
FormError,
|
|
|
|
FormMessage,
|
|
|
|
FormRow,
|
|
|
|
} from 'components/layout/FormLayout';
|
|
|
|
|
|
|
|
const initialValues = {
|
|
|
|
username: '',
|
|
|
|
password: '',
|
|
|
|
};
|
|
|
|
|
2020-08-09 11:03:37 +02:00
|
|
|
const validate = ({ user_id, username, password }) => {
|
2020-08-09 08:48:43 +02:00
|
|
|
const errors = {};
|
|
|
|
|
|
|
|
if (!username) {
|
2020-09-06 02:27:01 +02:00
|
|
|
errors.username = <FormattedMessage id="label.required" defaultMessage="Required" />;
|
2020-08-09 08:48:43 +02:00
|
|
|
}
|
2020-08-09 11:03:37 +02:00
|
|
|
if (!user_id && !password) {
|
2020-09-06 02:27:01 +02:00
|
|
|
errors.password = <FormattedMessage id="label.required" defaultMessage="Required" />;
|
2020-08-09 08:48:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return errors;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function AccountEditForm({ values, onSave, onClose }) {
|
|
|
|
const [message, setMessage] = useState();
|
|
|
|
|
|
|
|
const handleSubmit = async values => {
|
|
|
|
const response = await post(`/api/account`, values);
|
|
|
|
|
2020-08-09 11:03:37 +02:00
|
|
|
if (typeof response !== 'string') {
|
2020-08-09 08:48:43 +02:00
|
|
|
onSave();
|
|
|
|
} else {
|
2020-09-06 02:27:01 +02:00
|
|
|
setMessage(
|
|
|
|
response || (
|
|
|
|
<FormattedMessage id="message.failure" defaultMessage="Something went wrong." />
|
|
|
|
),
|
|
|
|
);
|
2020-08-09 08:48:43 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FormLayout>
|
|
|
|
<Formik
|
|
|
|
initialValues={{ ...initialValues, ...values }}
|
|
|
|
validate={validate}
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
>
|
|
|
|
{() => (
|
|
|
|
<Form>
|
|
|
|
<FormRow>
|
2020-09-06 02:27:01 +02:00
|
|
|
<label htmlFor="username">
|
|
|
|
<FormattedMessage id="label.username" defaultMessage="Username" />
|
|
|
|
</label>
|
2020-08-09 08:48:43 +02:00
|
|
|
<Field name="username" type="text" />
|
|
|
|
<FormError name="username" />
|
|
|
|
</FormRow>
|
|
|
|
<FormRow>
|
2020-09-06 02:27:01 +02:00
|
|
|
<label htmlFor="password">
|
|
|
|
<FormattedMessage id="label.password" defaultMessage="Password" />
|
|
|
|
</label>
|
2020-08-09 11:03:37 +02:00
|
|
|
<Field name="password" type="password" />
|
2020-08-09 08:48:43 +02:00
|
|
|
<FormError name="password" />
|
|
|
|
</FormRow>
|
|
|
|
<FormButtons>
|
|
|
|
<Button type="submit" variant="action">
|
2020-09-06 02:27:01 +02:00
|
|
|
<FormattedMessage id="button.save" defaultMessage="Save" />
|
|
|
|
</Button>
|
|
|
|
<Button onClick={onClose}>
|
|
|
|
<FormattedMessage id="button.cancel" defaultMessage="Cancel" />
|
2020-08-09 08:48:43 +02:00
|
|
|
</Button>
|
|
|
|
</FormButtons>
|
|
|
|
<FormMessage>{message}</FormMessage>
|
|
|
|
</Form>
|
|
|
|
)}
|
|
|
|
</Formik>
|
|
|
|
</FormLayout>
|
|
|
|
);
|
|
|
|
}
|