2023-03-28 09:49:20 +02:00
|
|
|
import { useState } from 'react';
|
2020-09-06 02:27:01 +02:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2023-03-28 09:49:20 +02:00
|
|
|
import { Formik, Form, Field, useFormikContext, useField } from 'formik';
|
2020-08-09 08:48:43 +02:00
|
|
|
import Button from 'components/common/Button';
|
2023-03-28 09:49:20 +02:00
|
|
|
import Checkbox from 'components/common/Checkbox';
|
2020-08-09 08:48:43 +02:00
|
|
|
import FormLayout, {
|
|
|
|
FormButtons,
|
|
|
|
FormError,
|
|
|
|
FormMessage,
|
|
|
|
FormRow,
|
|
|
|
} from 'components/layout/FormLayout';
|
2022-02-23 08:52:31 +01:00
|
|
|
import useApi from 'hooks/useApi';
|
2023-03-28 09:49:20 +02:00
|
|
|
import useFetch from 'hooks/useFetch';
|
2020-08-09 08:48:43 +02:00
|
|
|
|
|
|
|
const initialValues = {
|
|
|
|
username: '',
|
|
|
|
password: '',
|
2023-03-28 09:49:20 +02:00
|
|
|
isViewer: false,
|
|
|
|
websiteIds: [],
|
2020-08-09 08:48:43 +02:00
|
|
|
};
|
|
|
|
|
2022-10-12 22:11:44 +02:00
|
|
|
const validate = ({ 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
|
|
|
}
|
2022-10-12 22:11:44 +02:00
|
|
|
if (!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;
|
|
|
|
};
|
|
|
|
|
2023-03-28 09:49:20 +02:00
|
|
|
const WebsiteSelect = props => {
|
|
|
|
const { data } = useFetch(`/websites`);
|
|
|
|
const [field, meta] = useField(props);
|
|
|
|
const {
|
|
|
|
values: { websiteIds },
|
|
|
|
} = useFormikContext();
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{data && data.length > 0 && (
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
maxHeight: '20vh',
|
|
|
|
overflowY: 'auto',
|
|
|
|
padding: '0 1rem',
|
|
|
|
margin: '0 20px',
|
|
|
|
background: 'var(--gray100)',
|
|
|
|
border: '1px solid var(--gray500)',
|
|
|
|
borderRadius: '5px',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{data.map(item => (
|
|
|
|
<div key={`websiteIds-${item.id}`}>
|
|
|
|
<Checkbox
|
|
|
|
{...field}
|
|
|
|
value={item.id.toString()}
|
|
|
|
label={item.name}
|
|
|
|
valueArray={websiteIds}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{!!meta.touched && !!meta.error && <div>{meta.error}</div>}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-08-09 08:48:43 +02:00
|
|
|
export default function AccountEditForm({ values, onSave, onClose }) {
|
2022-02-23 08:52:31 +01:00
|
|
|
const { post } = useApi();
|
2020-08-09 08:48:43 +02:00
|
|
|
const [message, setMessage] = useState();
|
|
|
|
|
|
|
|
const handleSubmit = async values => {
|
2022-10-12 22:11:44 +02:00
|
|
|
const { id } = values;
|
|
|
|
const { ok, data } = await post(id ? `/accounts/${id}` : '/accounts', values);
|
2020-08-09 08:48:43 +02:00
|
|
|
|
2020-10-01 00:14:44 +02:00
|
|
|
if (ok) {
|
2020-08-09 08:48:43 +02:00
|
|
|
onSave();
|
|
|
|
} else {
|
2020-09-06 02:27:01 +02:00
|
|
|
setMessage(
|
2020-10-01 00:14:44 +02:00
|
|
|
data || <FormattedMessage id="message.failure" defaultMessage="Something went wrong." />,
|
2020-09-06 02:27:01 +02:00
|
|
|
);
|
2020-08-09 08:48:43 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FormLayout>
|
|
|
|
<Formik
|
|
|
|
initialValues={{ ...initialValues, ...values }}
|
|
|
|
validate={validate}
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
>
|
2023-03-28 09:49:20 +02:00
|
|
|
{values => (
|
2020-08-09 08:48:43 +02:00
|
|
|
<Form>
|
|
|
|
<FormRow>
|
2020-09-06 02:27:01 +02:00
|
|
|
<label htmlFor="username">
|
|
|
|
<FormattedMessage id="label.username" defaultMessage="Username" />
|
|
|
|
</label>
|
2020-10-22 20:00:58 +02:00
|
|
|
<div>
|
|
|
|
<Field name="username" type="text" />
|
|
|
|
<FormError name="username" />
|
|
|
|
</div>
|
2020-08-09 08:48:43 +02:00
|
|
|
</FormRow>
|
|
|
|
<FormRow>
|
2020-09-06 02:27:01 +02:00
|
|
|
<label htmlFor="password">
|
|
|
|
<FormattedMessage id="label.password" defaultMessage="Password" />
|
|
|
|
</label>
|
2020-10-22 20:00:58 +02:00
|
|
|
<div>
|
|
|
|
<Field name="password" type="password" />
|
|
|
|
<FormError name="password" />
|
|
|
|
</div>
|
2020-08-09 08:48:43 +02:00
|
|
|
</FormRow>
|
2023-03-28 09:49:20 +02:00
|
|
|
{!values.values.isAdmin && (
|
|
|
|
<FormRow>
|
|
|
|
<label />
|
|
|
|
<Field name="isViewer">
|
|
|
|
{({ field }) => (
|
|
|
|
<Checkbox
|
|
|
|
{...field}
|
|
|
|
label={<FormattedMessage id="label.is-viewer" defaultMessage="Viewer" />}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Field>
|
|
|
|
</FormRow>
|
|
|
|
)}
|
|
|
|
{values.values.isViewer && <WebsiteSelect name="websiteIds" />}
|
2020-08-09 08:48:43 +02:00
|
|
|
<FormButtons>
|
|
|
|
<Button type="submit" variant="action">
|
2020-10-13 07:53:59 +02:00
|
|
|
<FormattedMessage id="label.save" defaultMessage="Save" />
|
2020-09-06 02:27:01 +02:00
|
|
|
</Button>
|
|
|
|
<Button onClick={onClose}>
|
2020-10-13 07:53:59 +02:00
|
|
|
<FormattedMessage id="label.cancel" defaultMessage="Cancel" />
|
2020-08-09 08:48:43 +02:00
|
|
|
</Button>
|
|
|
|
</FormButtons>
|
|
|
|
<FormMessage>{message}</FormMessage>
|
|
|
|
</Form>
|
|
|
|
)}
|
|
|
|
</Formik>
|
|
|
|
</FormLayout>
|
|
|
|
);
|
|
|
|
}
|