|
| 1 | +<script lang="ts"> |
| 2 | + import { onMount } from 'svelte'; |
| 3 | + import { invalidate } from '$app/navigation'; |
| 4 | + import { Submit, trackEvent, trackError } from '$lib/actions/analytics'; |
| 5 | + import { CardGrid } from '$lib/components'; |
| 6 | + import { Dependencies } from '$lib/constants'; |
| 7 | + import { Button, Form, InputTags } from '$lib/elements/forms'; |
| 8 | + import { symmetricDifference } from '$lib/helpers/array'; |
| 9 | + import { addNotification } from '$lib/stores/notifications'; |
| 10 | + import { sdk } from '$lib/stores/sdk'; |
| 11 | + import { project } from '../store'; |
| 12 | + import { Tag, Input, Layout, Icon } from '@appwrite.io/pink-svelte'; |
| 13 | + import { IconPlus } from '@appwrite.io/pink-icons-svelte'; |
| 14 | +
|
| 15 | + const alphaNumericRegExp = /^[a-zA-Z0-9]+$/; |
| 16 | + let suggestedLabels = ['live', 'stage', 'internal']; |
| 17 | + let labels: string[] = []; |
| 18 | + let error = ''; |
| 19 | +
|
| 20 | + onMount(async () => { |
| 21 | + labels = [...$project.labels]; |
| 22 | + }); |
| 23 | +
|
| 24 | + async function updateLabels() { |
| 25 | + try { |
| 26 | + await sdk.forConsole.projects.updateLabels({ projectId: $project.$id, labels }); |
| 27 | + await invalidate(Dependencies.PROJECT); |
| 28 | + isDisabled = true; |
| 29 | + addNotification({ |
| 30 | + type: 'success', |
| 31 | + message: 'Project labels have been updated' |
| 32 | + }); |
| 33 | + trackEvent(Submit.ProjectUpdateLabels); |
| 34 | + } catch (error) { |
| 35 | + addNotification({ |
| 36 | + message: error.message, |
| 37 | + type: 'error' |
| 38 | + }); |
| 39 | + trackError(error, Submit.ProjectUpdateLabels); |
| 40 | + } |
| 41 | + } |
| 42 | +
|
| 43 | + $: isDisabled = !!error || !symmetricDifference(labels, $project.labels).length; |
| 44 | +
|
| 45 | + $: if (labels) { |
| 46 | + const invalidLabels = []; |
| 47 | +
|
| 48 | + labels.forEach((label) => { |
| 49 | + if (!alphaNumericRegExp.test(label)) { |
| 50 | + invalidLabels.push(label); |
| 51 | + } |
| 52 | + }); |
| 53 | +
|
| 54 | + if (invalidLabels.length) { |
| 55 | + error = `Invalid labels: ${invalidLabels.join(', ')}`; |
| 56 | + } else { |
| 57 | + error = ''; |
| 58 | + } |
| 59 | + } |
| 60 | +</script> |
| 61 | + |
| 62 | +<Form onSubmit={updateLabels}> |
| 63 | + <CardGrid> |
| 64 | + <svelte:fragment slot="title">Labels</svelte:fragment> |
| 65 | + Categorize and manage your projects for easy searching based on specific criteria by assigning |
| 66 | + them customizable labels. |
| 67 | + <svelte:fragment slot="aside"> |
| 68 | + <Layout.Stack gap="s"> |
| 69 | + {#key labels.length} |
| 70 | + <InputTags |
| 71 | + id="project-labels" |
| 72 | + label="Labels" |
| 73 | + placeholder="Select or type project labels" |
| 74 | + bind:tags={labels} /> |
| 75 | + {/key} |
| 76 | + <Layout.Stack direction="row"> |
| 77 | + {#each suggestedLabels as suggestedLabel} |
| 78 | + <Tag |
| 79 | + size="s" |
| 80 | + selected={labels.includes(suggestedLabel)} |
| 81 | + on:click={() => { |
| 82 | + if (!labels.includes(suggestedLabel)) { |
| 83 | + labels = [...labels, suggestedLabel]; |
| 84 | + } else { |
| 85 | + labels = labels.filter((e) => e !== suggestedLabel); |
| 86 | + } |
| 87 | + }}> |
| 88 | + <Icon icon={IconPlus} size="s" slot="start" /> |
| 89 | + {suggestedLabel} |
| 90 | + </Tag> |
| 91 | + {/each} |
| 92 | + </Layout.Stack> |
| 93 | + <Input.Helper state={error ? 'warning' : 'default'} |
| 94 | + >{error ? error : 'Only alphanumeric characters are allowed'}</Input.Helper> |
| 95 | + </Layout.Stack> |
| 96 | + </svelte:fragment> |
| 97 | + |
| 98 | + <svelte:fragment slot="actions"> |
| 99 | + <Button disabled={isDisabled} submit>Update</Button> |
| 100 | + </svelte:fragment> |
| 101 | + </CardGrid> |
| 102 | +</Form> |
0 commit comments