mirror of
https://github.com/diced/zipline.git
synced 2026-01-08 03:11:24 -08:00
66 lines
2.1 KiB
TypeScript
66 lines
2.1 KiB
TypeScript
import { Button, Group, Modal, Switch, TextInput, Title } from '@mantine/core';
|
|
import { useForm } from '@mantine/form';
|
|
import { showNotification } from '@mantine/notifications';
|
|
import { IconUserPlus, IconUserX } from '@tabler/icons-react';
|
|
import useFetch from 'hooks/useFetch';
|
|
|
|
export function CreateUserModal({ open, setOpen, updateUsers }) {
|
|
const form = useForm({
|
|
initialValues: {
|
|
username: '',
|
|
password: '',
|
|
administrator: false,
|
|
},
|
|
});
|
|
|
|
const onSubmit = async (values) => {
|
|
const cleanUsername = values.username.trim();
|
|
const cleanPassword = values.password.trim();
|
|
if (cleanUsername === '') return form.setFieldError('username', "Username can't be nothing");
|
|
if (cleanPassword === '') return form.setFieldError('password', "Password can't be nothing");
|
|
|
|
const data = {
|
|
username: cleanUsername,
|
|
password: cleanPassword,
|
|
administrator: values.administrator,
|
|
};
|
|
|
|
setOpen(false);
|
|
const res = await useFetch('/api/auth/create', 'POST', data);
|
|
if (res.error) {
|
|
showNotification({
|
|
title: 'Failed to create user',
|
|
message: res.error,
|
|
icon: <IconUserX size='1rem' />,
|
|
color: 'red',
|
|
});
|
|
} else {
|
|
showNotification({
|
|
title: 'Created user: ' + cleanUsername,
|
|
message: '',
|
|
icon: <IconUserPlus size='1rem' />,
|
|
color: 'green',
|
|
});
|
|
}
|
|
|
|
updateUsers();
|
|
};
|
|
|
|
return (
|
|
<Modal opened={open} onClose={() => setOpen(false)} title={<Title>Create User</Title>}>
|
|
<form onSubmit={form.onSubmit((v) => onSubmit(v))}>
|
|
<TextInput id='username' label='Username' {...form.getInputProps('username')} />
|
|
<TextInput id='password' label='Password' type='password' {...form.getInputProps('password')} />
|
|
<Switch mt='sm' id='administrator' label='Administrator' {...form.getInputProps('administrator')} />
|
|
|
|
<Group position='right' mt='md'>
|
|
<Button onClick={() => setOpen(false)}>Cancel</Button>
|
|
<Button type='submit' rightIcon={<IconUserPlus size='1rem' />}>
|
|
Create
|
|
</Button>
|
|
</Group>
|
|
</form>
|
|
</Modal>
|
|
);
|
|
}
|