2021-11-17 21:26:23 +00:00
|
|
|
import CopyOnClick from '@/components/elements/CopyOnClick';
|
2021-09-12 23:13:00 +00:00
|
|
|
import FormikSwitch from '@/components/elements/FormikSwitch';
|
2021-11-17 21:26:23 +00:00
|
|
|
import Input from '@/components/elements/Input';
|
|
|
|
import Label from '@/components/elements/Label';
|
2021-09-12 23:00:22 +00:00
|
|
|
import React from 'react';
|
2021-01-08 22:12:43 +00:00
|
|
|
import tw from 'twin.macro';
|
2021-09-12 23:00:22 +00:00
|
|
|
import { action, Action, createContextStore } from 'easy-peasy';
|
2022-02-27 20:26:26 +00:00
|
|
|
import { User, UserRole } from '@definitions/admin';
|
2021-07-24 18:23:33 +00:00
|
|
|
import AdminBox from '@/components/admin/AdminBox';
|
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
|
|
|
import { Form, Formik, FormikHelpers } from 'formik';
|
2021-09-12 23:13:00 +00:00
|
|
|
import { bool, object, string } from 'yup';
|
2022-02-27 20:26:26 +00:00
|
|
|
import { UpdateUserValues } from '@/api/admin/users';
|
2021-07-25 21:51:39 +00:00
|
|
|
import Button from '@/components/elements/Button';
|
2021-11-17 21:26:23 +00:00
|
|
|
import Field, { FieldRow } from '@/components/elements/Field';
|
2021-07-25 21:51:39 +00:00
|
|
|
import RoleSelect from '@/components/admin/users/RoleSelect';
|
2021-01-08 22:12:43 +00:00
|
|
|
|
|
|
|
interface ctx {
|
|
|
|
user: User | undefined;
|
|
|
|
setUser: Action<ctx, User | undefined>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const Context = createContextStore<ctx>({
|
|
|
|
user: undefined,
|
|
|
|
|
|
|
|
setUser: action((state, payload) => {
|
|
|
|
state.user = payload;
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
2021-07-24 18:23:33 +00:00
|
|
|
export interface Params {
|
|
|
|
title: string;
|
2022-02-27 20:26:26 +00:00
|
|
|
initialValues?: UpdateUserValues;
|
2021-07-24 18:23:33 +00:00
|
|
|
children?: React.ReactNode;
|
|
|
|
|
2022-02-27 20:26:26 +00:00
|
|
|
onSubmit: (values: UpdateUserValues, helpers: FormikHelpers<UpdateUserValues>) => void;
|
2021-07-25 21:51:39 +00:00
|
|
|
|
2021-11-17 21:26:23 +00:00
|
|
|
uuid?: string;
|
2022-02-27 20:26:26 +00:00
|
|
|
role: UserRole | null;
|
2021-07-24 18:23:33 +00:00
|
|
|
}
|
|
|
|
|
2021-11-17 21:26:23 +00:00
|
|
|
export default function UserForm ({ title, initialValues, children, onSubmit, uuid, role }: Params) {
|
2022-02-27 20:26:26 +00:00
|
|
|
const submit = (values: UpdateUserValues, helpers: FormikHelpers<UpdateUserValues>) => {
|
2021-07-24 18:23:33 +00:00
|
|
|
onSubmit(values, helpers);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!initialValues) {
|
|
|
|
initialValues = {
|
2021-11-17 21:26:23 +00:00
|
|
|
externalId: '',
|
2021-07-24 18:23:33 +00:00
|
|
|
username: '',
|
|
|
|
email: '',
|
|
|
|
password: '',
|
2021-09-14 01:15:21 +00:00
|
|
|
adminRoleId: null,
|
2021-09-12 23:13:00 +00:00
|
|
|
rootAdmin: false,
|
2021-07-24 18:23:33 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Formik
|
|
|
|
onSubmit={submit}
|
|
|
|
initialValues={initialValues}
|
|
|
|
validationSchema={object().shape({
|
|
|
|
username: string().min(1).max(32),
|
|
|
|
email: string(),
|
2021-09-12 23:13:00 +00:00
|
|
|
rootAdmin: bool().required(),
|
2021-07-24 18:23:33 +00:00
|
|
|
})}
|
|
|
|
>
|
|
|
|
{
|
|
|
|
({ isSubmitting, isValid }) => (
|
|
|
|
<>
|
|
|
|
<AdminBox title={title} css={tw`relative`}>
|
|
|
|
<SpinnerOverlay visible={isSubmitting}/>
|
|
|
|
|
|
|
|
<Form css={tw`mb-0`}>
|
2021-11-17 21:26:23 +00:00
|
|
|
<FieldRow>
|
|
|
|
{uuid &&
|
|
|
|
<div>
|
|
|
|
<Label>UUID</Label>
|
|
|
|
<CopyOnClick text={uuid}>
|
|
|
|
<Input
|
|
|
|
type={'text'}
|
|
|
|
value={uuid}
|
|
|
|
readOnly
|
|
|
|
/>
|
|
|
|
</CopyOnClick>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
<Field
|
|
|
|
id={'externalId'}
|
|
|
|
name={'externalId'}
|
|
|
|
label={'External ID'}
|
|
|
|
type={'text'}
|
|
|
|
description={'Used by external integrations, this field should not be modified unless you know what you are doing.'}
|
|
|
|
/>
|
|
|
|
<Field
|
|
|
|
id={'username'}
|
|
|
|
name={'username'}
|
|
|
|
label={'Username'}
|
|
|
|
type={'text'}
|
|
|
|
description={'The user\'s username, what else would go here?'}
|
|
|
|
/>
|
|
|
|
<Field
|
|
|
|
id={'email'}
|
|
|
|
name={'email'}
|
|
|
|
label={'Email Address'}
|
|
|
|
type={'email'}
|
|
|
|
description={'The user\'s email address, what else would go here?'}
|
|
|
|
/>
|
|
|
|
<Field
|
|
|
|
id={'password'}
|
|
|
|
name={'password'}
|
|
|
|
label={'Password'}
|
|
|
|
type={'password'}
|
|
|
|
placeholder={'••••••••'}
|
|
|
|
autoComplete={'new-password'}
|
|
|
|
/* TODO: Change description depending on if user is being created or updated. */
|
|
|
|
description={'Leave empty to email the user a link where they will be required to set a password.'}
|
|
|
|
/>
|
|
|
|
<RoleSelect selected={role}/>
|
|
|
|
</FieldRow>
|
2021-07-24 18:23:33 +00:00
|
|
|
|
2021-11-17 21:26:23 +00:00
|
|
|
{/* TODO: Remove toggle once role permissions are implemented. */}
|
2021-09-12 23:13:00 +00:00
|
|
|
<div css={tw`w-full flex flex-row mb-6`}>
|
2021-11-17 21:26:23 +00:00
|
|
|
<div css={tw`w-full bg-neutral-800 border border-neutral-900 shadow-inner p-4 rounded`}>
|
2021-09-12 23:13:00 +00:00
|
|
|
<FormikSwitch
|
|
|
|
name={'rootAdmin'}
|
|
|
|
label={'Root Admin'}
|
|
|
|
description={'Should this user be a root administrator?'}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2021-07-24 18:23:33 +00:00
|
|
|
<div css={tw`w-full flex flex-row items-center mt-6`}>
|
|
|
|
{children}
|
|
|
|
<div css={tw`flex ml-auto`}>
|
|
|
|
<Button type={'submit'} disabled={isSubmitting || !isValid}>
|
2021-10-03 23:10:37 +00:00
|
|
|
Save Changes
|
2021-07-24 18:23:33 +00:00
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</AdminBox>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</Formik>
|
|
|
|
);
|
|
|
|
}
|