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

66 lines
1.8 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';
2023-01-25 16:42:46 +01:00
import { useIntl } from 'react-intl';
2023-01-10 08:59:26 +01:00
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-25 16:42:46 +01:00
import { labels } from 'components/messages';
2023-01-10 08:59:26 +01:00
const generateId = () => getRandomChars(16);
export default function TeamEditForm({ teamId, data, onSave }) {
2023-01-25 16:42:46 +01:00
const { formatMessage } = useIntl();
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}>
2023-01-25 16:42:46 +01:00
<FormRow label={formatMessage(labels.teamId)}>
<TextField value={teamId} readOnly allowCopy />
</FormRow>
2023-01-25 16:42:46 +01:00
<FormRow label={formatMessage(labels.name)}>
2023-01-06 07:56:36 +01:00
<FormInput name="name" rules={{ required: 'Required' }}>
<TextField />
</FormInput>
</FormRow>
2023-01-25 16:42:46 +01:00
<FormRow label={formatMessage(labels.accessCode)}>
2023-01-10 08:59:26 +01:00
<Flexbox gap={10}>
<TextField value={accessCode} readOnly allowCopy />
2023-01-25 16:42:46 +01:00
<Button onClick={handleRegenerate}>{formatMessage(labels.regenerate)}</Button>
2023-01-10 08:59:26 +01:00
</Flexbox>
</FormRow>
<FormButtons>
2023-01-25 16:42:46 +01:00
<SubmitButton variant="primary">{formatMessage(labels.save)}</SubmitButton>
</FormButtons>
</Form>
);
}