2022-12-27 01:57:59 +01:00
|
|
|
import {
|
|
|
|
Dropdown,
|
|
|
|
Item,
|
|
|
|
Form,
|
2023-01-06 07:56:36 +01:00
|
|
|
FormRow,
|
2020-08-09 08:48:43 +02:00
|
|
|
FormButtons,
|
2022-12-27 01:57:59 +01:00
|
|
|
FormInput,
|
|
|
|
TextField,
|
|
|
|
SubmitButton,
|
|
|
|
} from 'react-basics';
|
|
|
|
import { useRef } from 'react';
|
|
|
|
import { useMutation } from '@tanstack/react-query';
|
2022-12-28 05:20:44 +01:00
|
|
|
import useApi from 'hooks/useApi';
|
2022-12-27 01:57:59 +01:00
|
|
|
import { ROLES } from 'lib/constants';
|
2020-08-09 08:48:43 +02:00
|
|
|
|
2022-12-27 01:57:59 +01:00
|
|
|
const items = [
|
|
|
|
{
|
|
|
|
value: ROLES.user,
|
|
|
|
label: 'User',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: ROLES.admin,
|
|
|
|
label: 'Admin',
|
|
|
|
},
|
|
|
|
];
|
2020-08-09 08:48:43 +02:00
|
|
|
|
2022-12-27 01:57:59 +01:00
|
|
|
export default function UserEditForm({ data, onSave }) {
|
|
|
|
const { id } = data;
|
2022-12-29 00:43:22 +01:00
|
|
|
const { post } = useApi();
|
2022-12-27 01:57:59 +01:00
|
|
|
const { mutate, error } = useMutation(({ username }) => post(`/user/${id}`, { username }));
|
|
|
|
const ref = useRef(null);
|
2020-08-09 08:48:43 +02:00
|
|
|
|
2022-12-27 01:57:59 +01:00
|
|
|
const handleSubmit = async data => {
|
|
|
|
mutate(data, {
|
|
|
|
onSuccess: async () => {
|
|
|
|
onSave(data);
|
|
|
|
ref.current.reset(data);
|
|
|
|
},
|
|
|
|
});
|
2020-08-09 08:48:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2023-01-11 02:18:59 +01:00
|
|
|
<Form key={id} ref={ref} onSubmit={handleSubmit} error={error} values={data}>
|
2023-01-06 07:56:36 +01:00
|
|
|
<FormRow label="Username">
|
|
|
|
<FormInput name="username">
|
|
|
|
<TextField />
|
|
|
|
</FormInput>
|
|
|
|
</FormRow>
|
|
|
|
<FormRow label="Role">
|
|
|
|
<FormInput name="role">
|
|
|
|
<Dropdown items={items} style={{ width: 200 }}>
|
|
|
|
{({ value, label }) => <Item key={value}>{label}</Item>}
|
|
|
|
</Dropdown>
|
|
|
|
</FormInput>
|
|
|
|
</FormRow>
|
2022-12-27 01:57:59 +01:00
|
|
|
<FormButtons>
|
|
|
|
<SubmitButton variant="primary">Save</SubmitButton>
|
|
|
|
</FormButtons>
|
|
|
|
</Form>
|
2020-08-09 08:48:43 +02:00
|
|
|
);
|
|
|
|
}
|