umami/components/pages/settings/teams/TeamEditForm.js

63 lines
1.6 KiB
JavaScript
Raw Normal View History

2023-01-10 08:59:26 +01:00
import {
SubmitButton,
Form,
FormInput,
FormRow,
FormButtons,
TextField,
Button,
Flexbox,
} from 'react-basics';
import { getRandomChars } from 'next-basics';
import { useRef, useState } from 'react';
2022-12-28 05:20:44 +01:00
import useApi from 'hooks/useApi';
2023-01-10 08:59:26 +01:00
const generateId = () => getRandomChars(16);
export default function TeamEditForm({ teamId, data, onSave }) {
2023-01-10 08:59:26 +01:00
const { post, useMutation } = useApi();
const { mutate, error } = useMutation(data => post(`/teams/${teamId}`, data));
const ref = useRef(null);
2023-01-10 08:59:26 +01:00
const [accessCode, setAccessCode] = useState(data.accessCode);
const handleSubmit = async data => {
mutate(data, {
onSuccess: async () => {
ref.current.reset(data);
onSave(data);
},
});
};
2023-01-10 08:59:26 +01:00
const handleRegenerate = () => {
const code = generateId();
ref.current.setValue('accessCode', code, {
shouldValidate: true,
shouldDirty: true,
});
setAccessCode(code);
};
return (
<Form ref={ref} onSubmit={handleSubmit} error={error} values={data}>
<FormRow label="Team ID">
<TextField value={teamId} readOnly allowCopy />
</FormRow>
2023-01-06 07:56:36 +01:00
<FormRow label="Name">
<FormInput name="name" rules={{ required: 'Required' }}>
<TextField />
</FormInput>
</FormRow>
2023-01-10 08:59:26 +01:00
<FormRow label="Access code">
<Flexbox gap={10}>
<TextField value={accessCode} readOnly allowCopy />
<Button onClick={handleRegenerate}>Regenerate</Button>
</Flexbox>
</FormRow>
<FormButtons>
<SubmitButton variant="primary">Save</SubmitButton>
</FormButtons>
</Form>
);
}