Skip to content

Commit

Permalink
Add user labels to user list and detail pages
Browse files Browse the repository at this point in the history
  • Loading branch information
stnguyen90 committed Jun 2, 2023
1 parent d61bef7 commit 32d8575
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/lib/actions/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export enum Submit {
UserCreate = 'submit_user_create',
UserDelete = 'submit_user_delete',
UserUpdateEmail = 'submit_user_update_email',
UserUpdateLabels = 'submit_user_update_labels',
UserUpdateName = 'submit_user_update_name',
UserUpdatePassword = 'submit_user_update_password',
UserUpdatePhone = 'submit_user_update_phone',
Expand Down
20 changes: 19 additions & 1 deletion src/routes/console/project-[project]/auth/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
Copy,
SearchQuery,
AvatarInitials,
PaginationWithLimit
PaginationWithLimit,
Trim
} from '$lib/components';
import { Button } from '$lib/elements/forms';
import {
Expand All @@ -26,6 +27,7 @@
import Create from './createUser.svelte';
import type { Models } from '@appwrite.io/console';
import type { PageData } from './$types';
import { tooltip } from '$lib/actions/tooltip';
export let data: PageData;
Expand All @@ -49,6 +51,7 @@
<TableCellHead onlyDesktop>Identifiers</TableCellHead>
<TableCellHead onlyDesktop width={130}>Status</TableCellHead>
<TableCellHead onlyDesktop width={100}>ID</TableCellHead>
<TableCellHead onlyDesktop width={100}>Labels</TableCellHead>
<TableCellHead onlyDesktop>Joined</TableCellHead>
</TableHeader>
<TableBody>
Expand Down Expand Up @@ -102,6 +105,21 @@
</Pill>
</Copy>
</TableCell>
<TableCell onlyDesktop title="Labels">
{#each user.labels.slice(0, 2) as label, i}
{#if i == 0}
<span
use:tooltip={{
disabled: user.labels.length < 2,
content: user.labels.join(', ')
}}>
<Pill>{label}</Pill>
</span>
{:else}
+{user.labels.length - 1}
{/if}
{/each}
</TableCell>
<TableCellText onlyDesktop title="Joined">
{toLocaleDateTime(user.registration)}
</TableCellText>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { Container } from '$lib/layout';
import DangerZone from './dangerZone.svelte';
import UpdateEmail from './updateEmail.svelte';
import UpdateLabels from './updateLabels.svelte';
import UpdateName from './updateName.svelte';
import UpdatePassword from './updatePassword.svelte';
import UpdatePhone from './updatePhone.svelte';
Expand All @@ -15,6 +16,7 @@
<UpdateEmail />
<UpdatePhone />
<UpdatePassword />
<UpdateLabels />
<UpdatePrefs />
<DangerZone />
</Container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<script lang="ts">
import { invalidate } from '$app/navigation';
import { Submit, trackEvent, trackError } from '$lib/actions/analytics';
import { CardGrid, Heading } from '$lib/components';
import { Dependencies } from '$lib/constants';
import { Button, Form, Helper, InputTags } from '$lib/elements/forms';
import { addNotification } from '$lib/stores/notifications';
import { sdk } from '$lib/stores/sdk';
import { user } from './store';
import { Pill } from '$lib/elements';
const alphaNumericRegExp = /^[a-zA-Z0-9]+$/;
let suggestedLabels = ['admin', 'vip', 'subscriber'];
let labels = [...$user.labels];
let error = '';
$: isDisabled = !!error || JSON.stringify(labels) == JSON.stringify($user.labels);
async function updateLabels() {
try {
await sdk.forProject.users.updateLabels($user.$id, labels);
await invalidate(Dependencies.USER);
isDisabled = true;
addNotification({
message: 'User labels have been updated',
type: 'success'
});
trackEvent(Submit.UserUpdateLabels);
} catch (error) {
addNotification({
message: error.message,
type: 'error'
});
trackError(error, Submit.UserUpdateLabels);
}
}
$: if (labels) {
const invalidLabels = []
labels.forEach((label) => {
if (!alphaNumericRegExp.test(label)) {
invalidLabels.push(label);
}
});
if (invalidLabels.length) {
error = `Invalid labels: ${invalidLabels.join(', ')}`;
} else {
error = '';
}
}
</script>

<Form onSubmit={updateLabels}>
<CardGrid>
<Heading tag="h6" size="7">User labels</Heading>
<p class="text">
Categorize and manage your users based on specific criteria by assigning them
customizable labels. Roles will be assigned to users based on the assigned labels.
</p>
<svelte:fragment slot="aside">
<ul class="common-section">
<InputTags
id="user-labels"
label="User labels"
placeholder="Select or tyype user labels"
bind:tags={labels} />
<li class="u-flex u-gap-12 u-margin-block-start-8">
{#each suggestedLabels as suggestedLabel}
<Pill
selected={labels.includes(suggestedLabel)}
button
on:click={() => {
if (!labels.includes(suggestedLabel)) {
labels = [...labels, suggestedLabel];
} else {
labels = labels.filter((e) => e !== suggestedLabel);
}
}}>
<span class="icon-plus" aria-hidden="true" />
{suggestedLabel}
</Pill>
{/each}
</li>
{#if error}
<li>
<Helper type="warning">{error}</Helper>
</li>
{/if}
</ul>
</svelte:fragment>

<svelte:fragment slot="actions">
<Button disabled={isDisabled} submit>Update</Button>
</svelte:fragment>
</CardGrid>
</Form>

0 comments on commit 32d8575

Please sign in to comment.