-
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.
- Loading branch information
1 parent
686c877
commit de72c2c
Showing
12 changed files
with
245 additions
and
29 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
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 { getErrorMsg } from "$lib/typeUtils"; | ||
export let user: User; | ||
export let update: (user: UserEdit) => Promise<User>; | ||
type UserForm = { | ||
id: string; | ||
username: string; | ||
isAdmin: boolean; | ||
isManager: boolean; | ||
isPerformer: boolean; | ||
} | ||
let {id, username, isAdmin, isManager, isPerformer} = fillForm(user); | ||
let roles: UserRole[] = []; | ||
$: { | ||
roles = []; | ||
if (isAdmin) roles.push("admin"); | ||
if (isManager) roles.push("manager"); | ||
if (isPerformer) roles.push("performer"); | ||
} | ||
let status: "idle" | "pending" | "success" | "error" = "idle"; | ||
let errormsg = ""; | ||
$: newUser = {id, username, roles} as UserEdit; | ||
$: isEdited = user.username !== newUser.username || !setsEqual(user.roles, newUser.roles); | ||
function setsEqual(a: any[], b: any[]) { | ||
return a.length === b.length && a.every((v, i) => b.includes(v)); | ||
} | ||
async function handelSubmit(event: Event) { | ||
status = "pending"; | ||
try { | ||
user = await update({id, username, roles} as UserEdit) | ||
status = "success"; | ||
} catch (error) { | ||
refillForm(user); | ||
console.log(user); | ||
errormsg = getErrorMsg(error); | ||
console.log(errormsg); | ||
status = "error"; | ||
} | ||
} | ||
function fillForm(user: User): UserForm { | ||
return { | ||
id: user.id, | ||
username: user.username, | ||
isAdmin: user.roles.includes("admin"), | ||
isManager: user.roles.includes("manager"), | ||
isPerformer: user.roles.includes("performer"), | ||
} | ||
} | ||
function refillForm(user: User) { | ||
let userForm = fillForm(user); | ||
id = userForm.id; | ||
username = userForm.username; | ||
isAdmin = userForm.isAdmin; | ||
isManager = userForm.isManager; | ||
isPerformer = userForm.isPerformer; | ||
} | ||
</script> | ||
|
||
<div class="card"> | ||
<form on:submit|preventDefault={handelSubmit}> | ||
<input type="text" placeholder="Username" bind:value={username} /> | ||
|
||
<label for="admin"> | ||
<input type="checkbox" bind:checked={isAdmin} disabled={!isAdmin}/> | ||
Admin | ||
</label> | ||
<label for="manager"> | ||
<input type="checkbox" bind:checked={isManager} disabled={!isAdmin}/> | ||
Manager | ||
</label> | ||
|
||
<label for="performer"> | ||
<input type="checkbox" bind:checked={isPerformer} disabled={!isAdmin}/> | ||
Performer | ||
</label> | ||
|
||
<input | ||
type="submit" | ||
value="Update" | ||
class="margin-0" | ||
disabled={!isEdited} | ||
class:secondary={!isEdited} | ||
aria-busy={status === "pending"} | ||
/> | ||
|
||
</form> | ||
</div> | ||
|
||
<style> | ||
.card { | ||
display: flex; | ||
flex-direction: column; | ||
gap: 1em; | ||
padding: 1em; | ||
max-width: calc(100%/3 - 1em); | ||
min-width: 10em; | ||
background-color: #1a202ccc; | ||
border-radius: 0.5em; | ||
} | ||
.margin-0 { | ||
margin: 0.1em 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
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,20 @@ | ||
import { RequestError } from "$lib"; | ||
import { isHttpError } from "@sveltejs/kit"; | ||
|
||
export function checkUserRoles(user: User, roles: UserRole[]) { | ||
return roles.some(role => user.roles.includes(role)); | ||
} | ||
|
||
|
||
export function getErrorMsg(e: any) { | ||
if (isHttpError(e)) { | ||
return `${e.body.message}: ${e.body.data?.detail?.detail}`; | ||
} | ||
|
||
if (e instanceof RequestError) { | ||
return e.message; | ||
} | ||
|
||
let er = e as { status: number; body: { message: string } }; | ||
return `${er.status}: ${er.body.message}`; | ||
} |
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 |
---|---|---|
@@ -1,8 +1,18 @@ | ||
type UserRole = "admin" | "manager" | "performer"; | ||
|
||
type User = { | ||
id: string; | ||
username: string; | ||
name: string; | ||
token: string; | ||
roles: string[]; | ||
roles: UserRole[]; | ||
publicId: string; | ||
}; | ||
|
||
|
||
|
||
type UserEdit = { | ||
id: string; | ||
username: string; | ||
roles: UserRole[]; | ||
}; |
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 |
---|---|---|
@@ -1,13 +1,64 @@ | ||
<script lang="ts"> | ||
import { UserService } from '$lib/api/user.js'; | ||
import UserCard from '$lib/components/UserCard.svelte'; | ||
import { checkUserRoles } from '$lib/typeUtils'; | ||
import { onMount } from 'svelte'; | ||
export let data; | ||
let user = data.session?.user; | ||
const userService = new UserService(data.accessToken); | ||
let me = data.me; | ||
let users: User[] = []; | ||
$: meAdmin = checkUserRoles(me, ['admin']); | ||
onMount(async () => { | ||
await refreshManagedUsers(); | ||
}); | ||
async function update(user: UserEdit): Promise<User> { | ||
let updated = await userService.editUser(user); | ||
if (updated.publicId === me.publicId) { | ||
me = updated; | ||
await refreshManagedUsers(); | ||
} | ||
return updated; | ||
} | ||
async function refreshManagedUsers() { | ||
if (!meAdmin) return; | ||
users = (await userService.listUsers()).filter(u => u.publicId !== me.publicId); | ||
} | ||
</script> | ||
|
||
<h1>🦜 Me</h1> | ||
<h1>🦜 {me.name}</h1> | ||
|
||
<h2>Edit personal info</h2> | ||
|
||
<UserCard user={me} {update}/> | ||
|
||
|
||
<div class="card"> | ||
<h2>User</h2> | ||
<pre>{JSON.stringify(data.me, null, 2)}</pre> | ||
<h2>Session</h2> | ||
<pre>{JSON.stringify(user, null, 2)}</pre> | ||
{#if meAdmin} | ||
<br/> | ||
<h2>Manage users</h2> | ||
|
||
<div class="cards"> | ||
{#each users as user} | ||
<UserCard {user} {update}/> | ||
{/each} | ||
</div> | ||
|
||
{/if} | ||
|
||
|
||
<style> | ||
.cards { | ||
display: flex; | ||
flex-wrap: wrap; | ||
gap: 1em; | ||
} | ||
</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