|
| 1 | +<script lang="ts"> |
| 2 | + import { base } from '$app/paths'; |
| 3 | + import { page } from '$app/stores'; |
| 4 | + import { InputText } from '$lib/elements/forms/index.js'; |
| 5 | + import { Wizard } from '$lib/layout'; |
| 6 | + import { Fieldset, Layout, Typography } from '@appwrite.io/pink-svelte'; |
| 7 | + import ExpirationInput from './expirationInput.svelte'; |
| 8 | + import Button from '$lib/elements/forms/button.svelte'; |
| 9 | + import Form from '$lib/elements/forms/form.svelte'; |
| 10 | + import { sdk } from '$lib/stores/sdk'; |
| 11 | + import { onboarding } from '../../store'; |
| 12 | + import { goto, invalidate } from '$app/navigation'; |
| 13 | + import { Dependencies } from '$lib/constants'; |
| 14 | + import { Submit, trackError, trackEvent } from '$lib/actions/analytics'; |
| 15 | + import { addNotification } from '$lib/stores/notifications'; |
| 16 | + import { writable } from 'svelte/store'; |
| 17 | + import Scopes from '../keys/scopes.svelte'; |
| 18 | +
|
| 19 | + const projectId = $page.params.project; |
| 20 | +
|
| 21 | + let showExitModal = false; |
| 22 | + let formComponent: Form; |
| 23 | + let isSubmitting = writable(false); |
| 24 | +
|
| 25 | + let scopes = []; |
| 26 | + let name = '', |
| 27 | + expire = ''; |
| 28 | +
|
| 29 | + async function create() { |
| 30 | + try { |
| 31 | + const { $id } = await sdk.forConsole.projects.createKey( |
| 32 | + projectId, |
| 33 | + name, |
| 34 | + scopes, |
| 35 | + expire || undefined |
| 36 | + ); |
| 37 | +
|
| 38 | + if ($onboarding) { |
| 39 | + await invalidate(Dependencies.PROJECT); |
| 40 | + } |
| 41 | +
|
| 42 | + trackEvent(Submit.KeyCreate); |
| 43 | + await goto(`${base}/project-${projectId}/overview/keys/${$id}`); |
| 44 | + addNotification({ |
| 45 | + message: `API key has been created`, |
| 46 | + type: 'success' |
| 47 | + }); |
| 48 | + } catch (error) { |
| 49 | + addNotification({ |
| 50 | + type: 'error', |
| 51 | + message: error.message |
| 52 | + }); |
| 53 | + trackError(error, Submit.KeyCreate); |
| 54 | + } |
| 55 | + } |
| 56 | +</script> |
| 57 | + |
| 58 | +<Wizard |
| 59 | + title="Create API key" |
| 60 | + href={`${base}/project-${projectId}/overview/keys/`} |
| 61 | + bind:showExitModal |
| 62 | + column |
| 63 | + columnSize="s" |
| 64 | + confirmExit> |
| 65 | + <Form bind:this={formComponent} onSubmit={create} bind:isSubmitting> |
| 66 | + <Layout.Stack gap="xxl"> |
| 67 | + <Fieldset legend="Configuration"> |
| 68 | + <Layout.Stack> |
| 69 | + <InputText |
| 70 | + id="name" |
| 71 | + label="Name" |
| 72 | + placeholder="Enter key name" |
| 73 | + required |
| 74 | + bind:value={name} /> |
| 75 | + |
| 76 | + <ExpirationInput bind:value={expire} keyType="api" /> |
| 77 | + </Layout.Stack> |
| 78 | + </Fieldset> |
| 79 | + |
| 80 | + <Fieldset legend="Scopes"> |
| 81 | + <Layout.Stack gap="xl"> |
| 82 | + <Typography.Text> |
| 83 | + Choose which permission scopes to grant your application. It is best |
| 84 | + practice to allow only the permissions you need to meet your project goals. |
| 85 | + </Typography.Text> |
| 86 | + <Scopes bind:scopes /> |
| 87 | + </Layout.Stack> |
| 88 | + </Fieldset> |
| 89 | + </Layout.Stack> |
| 90 | + </Form> |
| 91 | + |
| 92 | + <svelte:fragment slot="footer"> |
| 93 | + <Button fullWidthMobile secondary on:click={() => (showExitModal = true)}>Cancel</Button> |
| 94 | + <Button |
| 95 | + fullWidthMobile |
| 96 | + on:click={() => formComponent.triggerSubmit()} |
| 97 | + disabled={$isSubmitting}> |
| 98 | + Create |
| 99 | + </Button> |
| 100 | + </svelte:fragment> |
| 101 | +</Wizard> |
0 commit comments