-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ALS-6325] Add New/Edit/View actions to users page (#50)
- Loading branch information
Showing
29 changed files
with
639 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<script lang="ts"> | ||
import { goto } from '$app/navigation'; | ||
import { getToastStore } from '@skeletonlabs/skeleton'; | ||
const toastStore = getToastStore(); | ||
import { type ExtendedUser } from '$lib/models/User'; | ||
import { type Connection } from '$lib/models/Connection'; | ||
import UsersStore from '$lib/stores/Users'; | ||
import ConnectionStore from '$lib/stores/Connections'; | ||
import RoleStore from '$lib/stores/Roles'; | ||
import PrivilegesStore from '$lib/stores/Privileges'; | ||
const { addUser, updateUser } = UsersStore; | ||
const { getConnection } = ConnectionStore; | ||
const { getRole } = RoleStore; | ||
const { getPrivilege } = PrivilegesStore; | ||
export let user: ExtendedUser | undefined = undefined; | ||
export let roleList: string[][]; | ||
export let connections: Connection[]; | ||
let email = user ? user.email : ''; | ||
let connection = user ? user.connection : ''; | ||
let active = user ? user.active : true; | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
let roles = roleList.map(([_name, uuid]) => ({ | ||
uuid, | ||
checked: user ? user.roles.includes(uuid) : false, | ||
})); | ||
async function saveUser() { | ||
const generalMetadata = JSON.parse(user?.generalMetadata || '{"email":""}'); | ||
generalMetadata.email = email; | ||
let newUser = { | ||
email, | ||
connection: await getConnection(connection), | ||
generalMetadata: JSON.stringify(generalMetadata), | ||
active, | ||
roles: await Promise.all( | ||
roles | ||
.filter((role) => role.checked) | ||
.map((role) => | ||
getRole(role.uuid).then((role) => ({ | ||
...role, | ||
privileges: role.privileges.map((uuid: string) => getPrivilege(uuid)), | ||
})), | ||
), | ||
), | ||
}; | ||
try { | ||
if (user) { | ||
await updateUser({ ...newUser, uuid: user.uuid }); | ||
} else { | ||
await addUser(newUser); | ||
} | ||
toastStore.trigger({ | ||
message: `Successfully saved ${newUser ? 'new user' : 'user'} '${email}'`, | ||
background: 'variant-filled-success', | ||
}); | ||
goto('/admin/users'); | ||
} catch (error) { | ||
console.error(error); | ||
toastStore.trigger({ | ||
message: `An error occured while saving ${newUser ? 'new user' : 'user'} '${email}'`, | ||
background: 'variant-filled-error', | ||
}); | ||
} | ||
} | ||
</script> | ||
|
||
<form on:submit|preventDefault={saveUser}> | ||
<label class="flex items-center space-x-2"> | ||
<input class="checkbox" type="checkbox" bind:checked={active} /> | ||
<p>Active</p> | ||
</label> | ||
|
||
<label class="label required"> | ||
<span>Email:</span> | ||
<input type="email" bind:value={email} class="input" required minlength="1" maxlength="255" /> | ||
</label> | ||
|
||
<label class="label required"> | ||
<span>Connection:</span> | ||
<select class="select" bind:value={connection} required> | ||
<option selected={!user || !user.connection} disabled value>none</option> | ||
{#each connections as connection} | ||
<option value={connection.uuid} selected={user && user.connection === connection.uuid} | ||
>{connection.label}</option | ||
> | ||
{/each} | ||
</select> | ||
</label> | ||
|
||
<fieldset data-testid="privilege-checkboxes"> | ||
<legend>Roles:</legend> | ||
{#each roleList as [name], index} | ||
<label class="flex items-center space-x-2"> | ||
<input class="checkbox" type="checkbox" bind:checked={roles[index].checked} /> | ||
<p>{name}</p> | ||
</label> | ||
{/each} | ||
</fieldset> | ||
|
||
<button type="submit" class="btn variant-ghost-primary hover:variant-filled-primary"> | ||
Save | ||
</button> | ||
<a href="/admin/users" class="btn variant-ghost-secondary hover:variant-filled-secondary"> | ||
Cancel | ||
</a> | ||
</form> | ||
|
||
<style> | ||
label, | ||
fieldset { | ||
margin: 0.5em 0; | ||
} | ||
fieldset label { | ||
margin: 0; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<script lang="ts"> | ||
import { getModalStore, getToastStore } from '@skeletonlabs/skeleton'; | ||
const modalStore = getModalStore(); | ||
const toastStore = getToastStore(); | ||
import UsersStore from '$lib/stores/Users'; | ||
import ConnectionStore from '$lib/stores/Connections'; | ||
import RoleStore from '$lib/stores/Roles'; | ||
import PrivilegesStore from '$lib/stores/Privileges'; | ||
const { getUser, updateUser } = UsersStore; | ||
const { getConnection } = ConnectionStore; | ||
const { getRole } = RoleStore; | ||
const { getPrivilege } = PrivilegesStore; | ||
export let data = { cell: '', row: { status: '', email: '' } }; | ||
async function userActivation(activate: boolean) { | ||
const user = await getUser(data.cell); | ||
modalStore.trigger({ | ||
type: 'confirm', | ||
title: `${activate ? 'R' : 'D'}eactivate User?`, | ||
body: `Are you sure you want to ${activate ? 'r' : 'd'}eactiveate user '${user.email}'?`, | ||
buttonTextConfirm: activate ? 'Reactivate' : 'Deactivate', | ||
response: async (confirm: boolean) => { | ||
if (!confirm) return; | ||
let newUser = { | ||
...user, | ||
active: activate, | ||
connection: await getConnection(user.connection), | ||
roles: await Promise.all( | ||
user.roles.map((uuid: string) => | ||
getRole(uuid).then((role) => ({ | ||
...role, | ||
privileges: role.privileges.map((uuid: string) => getPrivilege(uuid)), | ||
})), | ||
), | ||
), | ||
}; | ||
try { | ||
await updateUser(newUser); | ||
toastStore.trigger({ | ||
message: `Successfully ${activate ? 'activated' : 'deactivated'} user '${user.email}'`, | ||
background: 'variant-filled-success', | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
toastStore.trigger({ | ||
message: `An error occured while ${activate ? 'activating' : 'deactivating'} user '${ | ||
user.email | ||
}'`, | ||
background: 'variant-filled-error', | ||
}); | ||
} | ||
}, | ||
}); | ||
} | ||
</script> | ||
|
||
<a | ||
data-testid={`user-view-btn-${data.cell}`} | ||
href={`/admin/users/${data.cell}`} | ||
class="text-secondary-600 hover:text-primary-600" | ||
> | ||
<i class="fa-solid fa-circle-info fa-xl"></i> | ||
<span class="sr-only">View User</span> | ||
</a> | ||
|
||
{#if data.row.status === 'Active'} | ||
<a | ||
data-testid={`user-edit-btn-${data.cell}`} | ||
href={`/admin/users/${data.cell}/edit`} | ||
class="text-secondary-600 hover:text-primary-600" | ||
> | ||
<i class="fa-solid fa-pen-to-square fa-xl"></i> | ||
<span class="sr-only">Edit</span> | ||
</a> | ||
<button | ||
data-testid={`user-deactivate-btn-${data.cell}`} | ||
type="button" | ||
title="Deactivate user" | ||
class="bg-initial text-secondary-600 hover:text-primary-600" | ||
on:click={() => userActivation(false)} | ||
> | ||
<i class="fa-solid fa-circle-xmark fa-xl"></i> | ||
<span class="sr-only">Deactivate user</span> | ||
</button> | ||
{:else} | ||
<button | ||
data-testid={`user-activate-btn-${data.cell}`} | ||
type="button" | ||
title="Reactivate user" | ||
class="bg-initial text-secondary-600 hover:text-primary-600" | ||
on:click={() => userActivation(true)} | ||
> | ||
<i class="fa-solid fa-circle-check fa-xl"></i> | ||
<span class="sr-only">Reactivate user</span> | ||
</button> | ||
{/if} |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.