feat: add expiration select to update api-key

This commit is contained in:
tglide committed 2023-02-07 17:59:03 +00:00
1 parent 7afdf95ef3
commit 453948dc24
4 files changed
+128 -100

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">Choose any name that will help you distinguish between API keys.</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,31 +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';
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();
}
let expirationSelect = null;
let expirationCustom: string | undefined = undefined;
$: {
$key.expire = expirationSelect === 'custom' ? expirationCustom : expirationSelect;
}
</script>
<WizardStep>
@@ -38,38 +15,6 @@
placeholder="API Key Name"
required
bind:value={$key.name} />
<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 expiration Date',
value: 'custom'
}
]}
id="preset"
label="Expiration Date" />
{#if expirationSelect === 'custom'}
<InputDateTime id="expire" label="" bind:value={expirationCustom} showLabel={false} />
{/if}
<ExpirationInput bind:value={$key.expire} />
</FormList>
</WizardStep>