-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from appwrite/feat-roles-component
feat: add roles component
- Loading branch information
Showing
4 changed files
with
255 additions
and
117 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<script lang="ts"> | ||
import { createEventDispatcher } from 'svelte'; | ||
import { Button } from '$lib/elements/forms'; | ||
import { DropList, DropListItem } from '..'; | ||
import Custom from './custom.svelte'; | ||
import Team from './team.svelte'; | ||
import User from './user.svelte'; | ||
import type { Permission } from './permissions.svelte'; | ||
import type { Writable } from 'svelte/store'; | ||
export let showDropdown: boolean; | ||
export let showUser: boolean; | ||
export let showTeam: boolean; | ||
export let showCustom: boolean; | ||
export let groups: Writable<Map<string, Permission>>; | ||
const dispatch = createEventDispatcher(); | ||
</script> | ||
|
||
<DropList | ||
bind:show={showDropdown} | ||
position="bottom" | ||
horizontal="right" | ||
arrow={true} | ||
arrowPosition="start"> | ||
<Button text noMargin on:click={() => (showDropdown = !showDropdown)}> | ||
<span class="icon-plus" aria-hidden="true" /> | ||
<span class="text">Add role</span> | ||
</Button> | ||
<svelte:fragment slot="list"> | ||
<DropListItem disabled={$groups.has('any')} on:click={() => dispatch('create', ['any'])}> | ||
Any | ||
</DropListItem> | ||
<DropListItem | ||
disabled={$groups.has('guests')} | ||
on:click={() => dispatch('create', ['guests'])}> | ||
All guests | ||
</DropListItem> | ||
<DropListItem | ||
disabled={$groups.has('users')} | ||
on:click={() => dispatch('create', ['users'])}> | ||
All users | ||
</DropListItem> | ||
<DropListItem on:click={() => (showUser = true)}>Select users</DropListItem> | ||
<DropListItem on:click={() => (showTeam = true)}>Select teams</DropListItem> | ||
<DropListItem on:click={() => (showCustom = true)}>Custom permission</DropListItem> | ||
</svelte:fragment> | ||
</DropList> | ||
|
||
<User bind:show={showUser} on:create {groups} /> | ||
<Team bind:show={showTeam} on:create {groups} /> | ||
<Custom bind:show={showCustom} on:create {groups} /> |
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,107 @@ | ||
<script lang="ts"> | ||
import { | ||
Table, | ||
TableBody, | ||
TableCellHead, | ||
TableCellText, | ||
TableHeader, | ||
TableRow | ||
} from '$lib/elements/table'; | ||
import { difference } from '$lib/helpers/array'; | ||
import { onDestroy, onMount } from 'svelte'; | ||
import { writable, type Unsubscriber } from 'svelte/store'; | ||
import Actions from './actions.svelte'; | ||
import type { Permission } from './permissions.svelte'; | ||
import Row from './row.svelte'; | ||
export let roles: string[] = []; | ||
let showUser = false; | ||
let showTeam = false; | ||
let showCustom = false; | ||
let showDropdown = false; | ||
let unsubscribe: Unsubscriber; | ||
const groups = writable<Map<string, Permission>>(new Map()); | ||
onMount(() => { | ||
roles.forEach(addRole); | ||
unsubscribe = groups.subscribe((n) => { | ||
const current = Array.from(n.keys()); | ||
if (difference(current, roles).length || difference(roles, current).length) { | ||
roles = current; | ||
} | ||
}); | ||
}); | ||
onDestroy(() => { | ||
if (unsubscribe) { | ||
unsubscribe(); | ||
} | ||
}); | ||
function create(event: CustomEvent<string[]>) { | ||
for (const role of event.detail) { | ||
addRole(role); | ||
} | ||
showTeam = showUser = false; | ||
} | ||
function addRole(role: string) { | ||
if ($groups.has(role)) { | ||
return; | ||
} | ||
groups.update((n) => { | ||
n.set(role, null); | ||
return n; | ||
}); | ||
showDropdown = false; | ||
} | ||
function deleteRole(role: string): void { | ||
groups.update((n) => { | ||
n.delete(role); | ||
return n; | ||
}); | ||
} | ||
</script> | ||
|
||
<Table noMargin noStyles noMobile> | ||
<TableHeader> | ||
<TableCellHead>Role</TableCellHead> | ||
<TableCellHead width={40} /> | ||
</TableHeader> | ||
<TableBody> | ||
{#each [...$groups.keys()] as role} | ||
<TableRow> | ||
<TableCellText title="Role"> | ||
<Row {role} /> | ||
</TableCellText> | ||
<TableCellText title="Remove"> | ||
<div class="u-flex"> | ||
<button | ||
class="button is-text is-only-icon" | ||
type="button" | ||
aria-label="delete" | ||
on:click={() => deleteRole(role)}> | ||
<span class="icon-x" aria-hidden="true" /> | ||
</button> | ||
</div> | ||
</TableCellText> | ||
</TableRow> | ||
{/each} | ||
</TableBody> | ||
</Table> | ||
|
||
<Actions | ||
bind:showCustom | ||
bind:showDropdown | ||
bind:showTeam | ||
bind:showUser | ||
{groups} | ||
on:create={create} /> |
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,3 +1,14 @@ | ||
<div class="table is-vertical u-margin-block-start-32" role="table"> | ||
<script lang="ts"> | ||
export let noMargin = false; | ||
export let noStyles = false; | ||
export let noMobile = false; | ||
</script> | ||
|
||
<div | ||
class="table is-vertical" | ||
class:is-vertical={!noMobile} | ||
class:u-margin-block-start-32={!noMargin} | ||
class:is-remove-outer-styles={noStyles} | ||
role="table"> | ||
<slot /> | ||
</div> |
9586aab
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
console – ./
svelte-console.vercel.app
console-appwrite.vercel.app
console-git-main-appwrite.vercel.app
brand-new-console.torsten.appwrite.org