Repository navigation
Expand file tree
/
Copy pathdialogs.tsx
More file actions
90 lines (84 loc) · 2.52 KB
/
Copy pathdialogs.tsx
File metadata and controls
90 lines (84 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import styles from "../dialogs.module.css";
import type { SubmitEvent } from "react";
import { useRef } from "react";
import { api, useMutation } from "@/api";
import { Dialog } from "@/components/ui/dialog";
import { createToast } from "@/components/ui/toast";
import { invalidateUsers, useMe } from "@/hooks/api";
import { cls } from "@/utils";
export const CreateUser = () => {
const { role } = useMe();
const closeRef = useRef<HTMLButtonElement>(null);
const { mutate, error } = useMutation({
mutationFn: api["/api/dashboard/user"].post,
onSuccess: () => {
closeRef?.current?.click();
createToast("User created", "success");
invalidateUsers();
},
onError: console.error,
});
const handleSubmit = (event: SubmitEvent<HTMLFormElement>) => {
event.preventDefault();
event.stopPropagation();
const { username, password, admin } = Object.fromEntries(new FormData(event.currentTarget)) as {
username: string;
password: string;
admin: string;
};
const role = admin === "on" ? "admin" : "user";
mutate({ json: { username, password, role } });
};
return (
<Dialog
title="Create a new user"
description="Non-admin users can only view projects they belong to. They cannot create or edit projects, entities, or users."
trigger={
role === "admin" && (
<button type="button" className={cls("contrast", styles.new)}>
Create
</button>
)
}
>
<form onSubmit={handleSubmit}>
<label>
Username <small>(cannot be changed later)</small>
<input
required
pattern="^[A-Za-z0-9_\-]{2,20}$"
name="username"
type="text"
placeholder="MyUsername"
autoComplete="username"
/>
</label>
<label>
Password
<input required name="password" type="password" autoComplete="new-password" minLength={8} />
</label>
<label>
{/* biome-ignore lint/a11y/useAriaPropsForRole: this is an uncontrolled component */}
<input name="admin" type="checkbox" role="switch" />
Enable administrator access
<br />
<small>Administrators can edit and create projects, entities, and users.</small>
</label>
<br />
<div className="grid">
<Dialog.Close className="secondary outline" ref={closeRef}>
Cancel
</Dialog.Close>
<button type="submit">Create user</button>
</div>
{error && (
<article role="alert" className={styles.error}>
{"An error occurred while creating the user:"}
<br />
{error?.message ?? "Unknown error"}
</article>
)}
</form>
</Dialog>
);
};