Merge pull request #257 from appwrite/refactor/api-key-forms

Refactor: API Key forms
This commit is contained in:
Torsten Dittmann authored and GitHub committed 2023-02-08 10:52:26 +01:00
commit 76a30d77b7
4 files changed
+128 -114

No files matched your search

@@ -1,8 +1,9 @@
<script lang="ts">
import { invalidate } from '$app/navigation';
import { trackEvent } from '$lib/actions/analytics';
import { CardGrid, Heading, Secret } from '$lib/components';
import { Dependencies } from '$lib/constants';
import { Button, Form, FormList, InputText, InputDateTime } from '$lib/elements/forms';
import { Button, Form, FormList, InputText } from '$lib/elements/forms';
import { symmetricDifference } from '$lib/helpers/array';
import { toLocaleDateTime } from '$lib/helpers/date';
import { Container } from '$lib/layout';
@@ -10,21 +11,19 @@
import { sdkForConsole } from '$lib/stores/sdk';
import { onMount } from 'svelte';
import { project } from '../../../store';
import { key } from './store';
import Scopes from '../scopes.svelte';
import Delete from './delete.svelte';
import { trackEvent } from '$lib/actions/analytics';
import { key } from './store';
import UpdateExpirationDate from './updateExpirationDate.svelte';
let showDelete = false;
let name: string = null;
let secret: string = null;
let expire: string = null;
let scopes: string[] = null;
onMount(() => {
name ??= $key.name;
secret ??= $key.secret;
expire ??= $key.expire;
scopes ??= $key.scopes;
});
@@ -51,29 +50,6 @@
}
}
async function updateExpire() {
try {
await sdkForConsole.projects.updateKey(
$project.$id,
$key.$id,
$key.name,
$key.scopes,
expire
);
invalidate(Dependencies.KEY);
trackEvent('submit_key_update_expire');
addNotification({
type: 'success',
message: 'API Key expiration has been updated'
});
} catch (error) {
addNotification({
type: 'error',
message: error.message
});
}
}
async function updateScopes() {
try {
await sdkForConsole.projects.updateKey(
@@ -165,21 +141,9 @@
</svelte:fragment>
</CardGrid>
</Form>
<Form on:submit={updateExpire}>
<CardGrid>
<Heading tag="h6" size="7">Update Expiration Date</Heading>
<p class="text">Choose any name that will help you distinguish between API keys.</p>
<svelte:fragment slot="aside">
<FormList>
<InputDateTime id="expire" label="Expiration Date" bind:value={expire} />
</FormList>
</svelte:fragment>
<svelte:fragment slot="actions">
<Button disabled={expire === $key.expire} submit>Update</Button>
</svelte:fragment>
</CardGrid>
</Form>
<UpdateExpirationDate />
<CardGrid danger>
<div>
<Heading tag="h6" size="7">Delete API Key</Heading>
@@ -0,0 +1,56 @@
<script lang="ts">
import { invalidate } from '$app/navigation';
import { trackEvent } from '$lib/actions/analytics';
import CardGrid from '$lib/components/cardGrid.svelte';
import Heading from '$lib/components/heading.svelte';
import { Dependencies } from '$lib/constants';
import { Form } from '$lib/elements/forms';
import Button from '$lib/elements/forms/button.svelte';
import FormList from '$lib/elements/forms/formList.svelte';
import { addNotification } from '$lib/stores/notifications';
import { sdkForConsole } from '$lib/stores/sdk';
import { project } from '../../../store';
import ExpirationInput from '../expirationInput.svelte';
import { key } from './store';
let expiration = $key.expire;
async function updateExpire() {
try {
await sdkForConsole.projects.updateKey(
$project.$id,
$key.$id,
$key.name,
$key.scopes,
expiration
);
invalidate(Dependencies.KEY);
trackEvent('submit_key_update_expire');
addNotification({
type: 'success',
message: 'API Key expiration has been updated'
});
} catch (error) {
addNotification({
type: 'error',
message: error.message
});
}
}
</script>
<Form on:submit={updateExpire}>
<CardGrid>
<Heading tag="h6" size="7">Update Expiration Date</Heading>
<p class="text">Set a date after which your API Key will expire.</p>
<svelte:fragment slot="aside">
<FormList>
<ExpirationInput bind:value={expiration} />
</FormList>
</svelte:fragment>
<svelte:fragment slot="actions">
<Button disabled={expiration === $key.expire} submit>Update</Button>
</svelte:fragment>
</CardGrid>
</Form>
@@ -0,0 +1,63 @@
<script lang="ts">
import InputDateTime from '$lib/elements/forms/inputDateTime.svelte';
import InputSelect from '$lib/elements/forms/inputSelect.svelte';
function incrementToday(value: number, type: 'day' | 'month' | 'year'): string {
const date = new Date();
switch (type) {
case 'day':
date.setDate(date.getDate() + value);
break;
case 'month':
date.setMonth(date.getMonth() + value);
break;
case 'year':
date.setMonth(date.getMonth() + value * 12);
break;
}
return date.toISOString();
}
export let value: string | null = null;
let expirationSelect = value === null ? null : 'custom';
let expirationCustom: string | null = value ?? null;
$: {
value = expirationSelect === 'custom' ? expirationCustom : expirationSelect;
}
</script>
<InputSelect
bind:value={expirationSelect}
options={[
{
label: 'Never',
value: null
},
{
label: '7 Days',
value: incrementToday(7, 'day')
},
{
label: '30 days',
value: incrementToday(30, 'day')
},
{
label: '90 days',
value: incrementToday(90, 'day')
},
{
label: '1 Year',
value: incrementToday(1, 'year')
},
{
label: 'Custom Date',
value: 'custom'
}
]}
id="preset"
label="Expiration Date" />
{#if expirationSelect === 'custom'}
<InputDateTime id="expire" label="" bind:value={expirationCustom} showLabel={false} />
{/if}
@@ -1,45 +1,8 @@
<script lang="ts">
import { FormList, InputText, InputDateTime, InputSelect } from '$lib/elements/forms';
import { FormList, InputText } from '$lib/elements/forms';
import { WizardStep } from '$lib/layout';
import ExpirationInput from '../expirationInput.svelte';
import { key } from './store';
let preset = 'never';
function incrementToday(value: number, type: 'day' | 'month' | 'year'): string {
const date = new Date();
switch (type) {
case 'day':
date.setDate(date.getDate() + value);
break;
case 'month':
date.setMonth(date.getMonth() + value);
break;
case 'year':
date.setMonth(date.getMonth() + value * 12);
break;
}
return date.toISOString();
}
$: {
switch (preset) {
case 'never':
$key.expire = null;
break;
case '1day':
$key.expire = incrementToday(1, 'day');
break;
case '7days':
$key.expire = incrementToday(7, 'day');
break;
case '1month':
$key.expire = incrementToday(1, 'month');
break;
case '1year':
$key.expire = incrementToday(1, 'year');
break;
}
}
</script>
<WizardStep>
@@ -52,38 +15,6 @@
placeholder="API Key Name"
required
bind:value={$key.name} />
<InputSelect
bind:value={preset}
options={[
{
label: 'Never',
value: 'never'
},
{
label: '1 Day',
value: '1day'
},
{
label: '7 Days',
value: '7days'
},
{
label: '1 Month',
value: '1month'
},
{
label: '1 Year',
value: '1year'
},
{
label: 'Custom Date',
value: 'custom'
}
]}
id="preset"
label="Expiration Date" />
{#if preset === 'custom'}
<InputDateTime id="expire" label="" bind:value={$key.expire} showLabel={false} />
{/if}
<ExpirationInput bind:value={$key.expire} />
</FormList>
</WizardStep>