Skip to content

Commit

Permalink
Add user disable functionality; update messages and validation for se…
Browse files Browse the repository at this point in the history
…lf-disable action
  • Loading branch information
haukened committed Nov 7, 2024
1 parent 1307755 commit 55d9300
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
5 changes: 3 additions & 2 deletions messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
"adminUserDeleted": "User {email} has been deleted",
"adminUserDeleteDescription": "User {email} will be permanently deleted",
"adminUserDeleteWarning": "Are you ABSOLUTELY sure? This action cannot be undone.",
"adminUserDisable": "Disable User",
"adminUserDisabled": "User is disabled",
"adminUserEdit": "Edit User",
"adminUserEnabled": "User is enabled",
"adminUserToggle": "Enable / Disable User",
"adminUserUpdated": "User updated",
"cancel": "Cancel",
"close": "Close",
Expand All @@ -34,7 +34,8 @@
"errorCannotDeleteUser": "Unable to delete user",
"errorCreatingRegistrationToken": "Error creating registration token",
"errorDatabaseError": "A database error occurred",
"errorDeleteSelf": "Bro, you can't delete yourself",
"errorDeleteSelf": "You can't delete yourself",
"errorDisableSelf": "You can't disable yourself",
"errorEmailExists": "Email already exists",
"errorPasswordHashing": "Unable to produce reliable password hash",
"errorPasswordRequired": "Password is required",
Expand Down
5 changes: 5 additions & 0 deletions src/routes/admin/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,16 @@ export const actions: Actions = {
if (!form.data.id) {
return setError(form, '', m.errorUserIdRequired());
}
// you cannot disable yourself
if (event.locals.user.id === form.data.id && form.data.disabled) {
return setError(form, '', m.errorDisableSelf());
}
// update the user
const updated: { id: number }[] = await db.update(users).set({
firstname: form.data.firstname,
lastname: form.data.lastname,
email: form.data.email,
disabled: form.data.disabled,
}).where(eq(users.id, form.data.id)).returning({ id: users.id});
// make sure the user was updated
if (updated.length === 0) {
Expand Down
21 changes: 19 additions & 2 deletions src/routes/admin/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
let disableRegCloseButton = $state(true);
let actuallyDeleteCheckBox = $state(false);
let disableDeleteButton = $derived(!actuallyDeleteCheckBox);
let userDisableClass = $derived.by(() => {
if (dialogAction === "update") {
return "visible";
} else {
return "invisible";
}
})
let regToken = $state({
value: "",
Expand Down Expand Up @@ -82,6 +89,7 @@
lastname: "",
email: "",
id: undefined,
disabled: false,
});
}
Expand Down Expand Up @@ -123,7 +131,7 @@
if ($message.token) {
closeDialog();
openRegDialog($message.token);
clearRegToken();
$message.token = undefined;
clearCheckbox();
}
}
Expand Down Expand Up @@ -300,7 +308,16 @@
</Form.Control>
<Form.FieldErrors/>
</Form.Field>
<Dialog.Footer>
<Dialog.Footer class="flex flex-row w-full !justify-between items-center space-x-2">
<Form.Field {form} name="disabled" class={userDisableClass}>
<Form.Control let:attrs>
<div class="flex flex-row justify-start items-center space-x-2">
<Checkbox bind:checked={$formData.disabled} {...attrs}/>
<Form.Label>{m.adminUserDisable()}</Form.Label>
</div>
</Form.Control>
<Form.FieldErrors/>
</Form.Field>
<Button type="submit">{m._save()}</Button>
</Dialog.Footer>
{/if}
Expand Down
1 change: 1 addition & 0 deletions src/routes/admin/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const userFormSchema = z.object({
firstname: z.string().min(3).max(50),
lastname: z.string().min(3).max(50),
email: z.string().email(),
disabled: z.boolean().default(false).optional(),
});

export type UserFormSchema = typeof userFormSchema;

0 comments on commit 55d9300

Please sign in to comment.