Skip to content

Commit

Permalink
Fixing creating new roles
Browse files Browse the repository at this point in the history
  • Loading branch information
Dexter committed Jun 16, 2023
1 parent da9f3b6 commit 4f2e7ca
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/lib/Form/RoleSelector.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
export let refetch: () => void;
// export let options = [];
let newRole = '';
$: allRoles = graphql(`
query AllRoles {
roles: auth_person_role(distinct_on: role) {
Expand Down Expand Up @@ -66,30 +68,43 @@
({ role }) => !currentRoles.find((currentRole) => currentRole.role === role)
);
let roleSelect: string;
let filterText = '';
let value: string = '';
let items: { value: string; label: string; created: boolean }[] = [];
$: if (possibleRoles) {
items = possibleRoles.map(({ role }) => ({ value: role, label: role }));
}
async function handleSelect(event) {
const role = event.detail.value.toLowerCase();
// Check for duplicates
if (!currentRoles.some((currentRole) => role === currentRole.role)) {
await createRole.mutate({ personId: person.id, role }, { metadata: { isBoard: true } });
}
roleSelect = '';
value = '';
await refetch();
}
function handleFilter(e) {
if (e.detail.length === 0 && filterText.length > 0) {
const prev = items.filter((i) => !i.created);
items = [...prev, { value: filterText, label: filterText, created: true }];
}
}
</script>

<section class="wide">
<label>Roles</label>

{#if !readonly}
<Select
items={possibleRoles?.map(({ role }) => ({ value: role, label: role }))}
bind:value={roleSelect}
on:select={handleSelect}
isCreatable
/>
<Select {items} bind:value on:select={handleSelect} on:filter={handleFilter} bind:filterText>
<div slot="item" let:item>
{item.created ? 'Add new: ' : ''}
{item.label}
</div>
</Select>
{/if}

{#if roles}
Expand Down

0 comments on commit 4f2e7ca

Please sign in to comment.