Refactor provider settings form list into a component

This commit is contained in:
Steven Nguyen authored and Torsten Dittmann committed 2024-02-21 10:28:22 +01:00
1 parent 313b912864
commit c2fa908129
10 files changed
+348 -341

No files matched your search

@@ -11,7 +11,7 @@
import { base } from '$app/paths';
import { project } from '../../store';
import { wizard } from '$lib/stores/wizard';
import { provider, providerParams } from './wizard/store';
import { provider, providerParams } from './store';
import { ID, type Models } from '@appwrite.io/console';
import { Providers } from '../provider.svelte';
@@ -4,7 +4,7 @@
import { wizard } from '$lib/stores/wizard';
import { providers } from './store';
import Create from './create.svelte';
import { providerType, provider } from './wizard/store';
import { providerType, provider } from './store';
import { Providers } from '../provider.svelte';
import { isValueOfStringEnum } from '$lib/helpers/types';
import { MessagingProviderType } from '@appwrite.io/console';
@@ -14,146 +14,146 @@
import Provider, { Providers } from '../../provider.svelte';
import ProviderType from '../../providerType.svelte';
import Update from '../update.svelte';
import { providerParams, providerType, provider as wizardProvider } from '../wizard/store';
import { provider } from './store';
import { providerParams, providerType, provider } from '../store';
import { provider as providerData } from './store';
let enabled: boolean = null;
onMount(() => {
enabled ??= $provider.enabled;
enabled ??= $providerData.enabled;
});
function configure() {
if (!isValueOfStringEnum(MessagingProviderType, $provider.type)) {
throw new Error(`Invalid provider type: ${$provider.type}`);
if (!isValueOfStringEnum(MessagingProviderType, $providerData.type)) {
throw new Error(`Invalid provider type: ${$providerData.type}`);
}
if (!isValueOfStringEnum(Providers, $provider.provider)) {
throw new Error(`Invalid provider: ${$provider.provider}`);
if (!isValueOfStringEnum(Providers, $providerData.provider)) {
throw new Error(`Invalid provider: ${$providerData.provider}`);
}
$providerType = $provider.type;
$wizardProvider = $provider.provider;
$providerType = $providerData.type;
$provider = $providerData.provider;
switch ($wizardProvider) {
switch ($provider) {
case Providers.Twilio:
$providerParams[$wizardProvider] = {
providerId: $provider.$id,
name: $provider.name,
enabled: $provider.enabled,
accountSid: $provider.credentials['accountSid'],
authToken: $provider.credentials['authToken'],
from: $provider.options['from']
$providerParams[$provider] = {
providerId: $providerData.$id,
name: $providerData.name,
enabled: $providerData.enabled,
accountSid: $providerData.credentials['accountSid'],
authToken: $providerData.credentials['authToken'],
from: $providerData.options['from']
};
break;
case Providers.Msg91:
$providerParams[$wizardProvider] = {
providerId: $provider.$id,
name: $provider.name,
enabled: $provider.enabled,
from: $provider.options['from'],
senderId: $provider.credentials['senderId'],
authKey: $provider.credentials['authKey']
$providerParams[$provider] = {
providerId: $providerData.$id,
name: $providerData.name,
enabled: $providerData.enabled,
from: $providerData.options['from'],
senderId: $providerData.credentials['senderId'],
authKey: $providerData.credentials['authKey']
};
break;
case Providers.Telesign:
$providerParams[$wizardProvider] = {
providerId: $provider.$id,
name: $provider.name,
enabled: $provider.enabled,
username: $provider.credentials['username'],
password: $provider.credentials['password'],
from: $provider.options['from']
$providerParams[$provider] = {
providerId: $providerData.$id,
name: $providerData.name,
enabled: $providerData.enabled,
username: $providerData.credentials['username'],
password: $providerData.credentials['password'],
from: $providerData.options['from']
};
break;
case Providers.Textmagic:
$providerParams[$wizardProvider] = {
providerId: $provider.$id,
name: $provider.name,
enabled: $provider.enabled,
username: $provider.credentials['username'],
apiKey: $provider.credentials['apiKey'],
from: $provider.options['from']
$providerParams[$provider] = {
providerId: $providerData.$id,
name: $providerData.name,
enabled: $providerData.enabled,
username: $providerData.credentials['username'],
apiKey: $providerData.credentials['apiKey'],
from: $providerData.options['from']
};
break;
case Providers.Vonage:
$providerParams[$wizardProvider] = {
providerId: $provider.$id,
name: $provider.name,
enabled: $provider.enabled,
apiKey: $provider.credentials['apiKey'],
apiSecret: $provider.credentials['apiSecret'],
from: $provider.options['from']
$providerParams[$provider] = {
providerId: $providerData.$id,
name: $providerData.name,
enabled: $providerData.enabled,
apiKey: $providerData.credentials['apiKey'],
apiSecret: $providerData.credentials['apiSecret'],
from: $providerData.options['from']
};
break;
case Providers.Mailgun:
$providerParams[$wizardProvider] = {
providerId: $provider.$id,
name: $provider.name,
enabled: $provider.enabled,
$providerParams[$provider] = {
providerId: $providerData.$id,
name: $providerData.name,
enabled: $providerData.enabled,
isEuRegion: false,
fromEmail: $provider.options['fromEmail'],
fromName: $provider.options['fromName'],
replyToEmail: $provider.options['replyToEmail'],
replyToName: $provider.options['replyToName'],
apiKey: $provider.credentials['apiKey'],
domain: $provider.credentials['domain']
fromEmail: $providerData.options['fromEmail'],
fromName: $providerData.options['fromName'],
replyToEmail: $providerData.options['replyToEmail'],
replyToName: $providerData.options['replyToName'],
apiKey: $providerData.credentials['apiKey'],
domain: $providerData.credentials['domain']
};
break;
case Providers.Sendgrid:
$providerParams[$wizardProvider] = {
providerId: $provider.$id,
name: $provider.name,
enabled: $provider.enabled,
apiKey: $provider.credentials['apiKey'],
fromEmail: $provider.options['fromEmail'],
fromName: $provider.options['fromName'],
replyToEmail: $provider.options['replyToEmail'],
replyToName: $provider.options['replyToName']
$providerParams[$provider] = {
providerId: $providerData.$id,
name: $providerData.name,
enabled: $providerData.enabled,
apiKey: $providerData.credentials['apiKey'],
fromEmail: $providerData.options['fromEmail'],
fromName: $providerData.options['fromName'],
replyToEmail: $providerData.options['replyToEmail'],
replyToName: $providerData.options['replyToName']
};
break;
case Providers.SMTP:
$providerParams[$wizardProvider] = {
providerId: $provider.$id,
name: $provider.name,
enabled: $provider.enabled,
host: $provider.credentials['host'],
port: $provider.credentials['port'],
username: $provider.credentials['username'],
password: $provider.credentials['password'],
fromName: $provider.options['fromName'],
fromEmail: $provider.options['fromEmail'],
replyToName: $provider.options['replyToName'],
replyToEmail: $provider.options['replyToEmail'],
encryption: $provider.options['encryption'],
autoTLS: $provider.options['autoTLS'],
mailer: $provider.options['mailer']
$providerParams[$provider] = {
providerId: $providerData.$id,
name: $providerData.name,
enabled: $providerData.enabled,
host: $providerData.credentials['host'],
port: $providerData.credentials['port'],
username: $providerData.credentials['username'],
password: $providerData.credentials['password'],
fromName: $providerData.options['fromName'],
fromEmail: $providerData.options['fromEmail'],
replyToName: $providerData.options['replyToName'],
replyToEmail: $providerData.options['replyToEmail'],
encryption: $providerData.options['encryption'],
autoTLS: $providerData.options['autoTLS'],
mailer: $providerData.options['mailer']
};
break;
case Providers.FCM:
$providerParams[$wizardProvider] = {
providerId: $provider.$id,
name: $provider.name,
enabled: $provider.enabled
$providerParams[$provider] = {
providerId: $providerData.$id,
name: $providerData.name,
enabled: $providerData.enabled
};
if ('serviceAccountJSON' in $provider.credentials) {
const serviceAccountJSON = $provider.credentials['serviceAccountJSON'];
if ('serviceAccountJSON' in $providerData.credentials) {
const serviceAccountJSON = $providerData.credentials['serviceAccountJSON'];
if (typeof serviceAccountJSON === 'string') {
$providerParams[$wizardProvider].serviceAccountJSON = serviceAccountJSON;
$providerParams[$provider].serviceAccountJSON = serviceAccountJSON;
} else if (serviceAccountJSON instanceof Object) {
$providerParams[$wizardProvider].serviceAccountJSON =
$providerParams[$provider].serviceAccountJSON =
JSON.stringify(serviceAccountJSON);
}
}
break;
case Providers.APNS:
$providerParams[$wizardProvider] = {
providerId: $provider.$id,
name: $provider.name,
enabled: $provider.enabled,
authKey: $provider.credentials['authKey'],
authKeyId: $provider.credentials['authKeyId'],
teamId: $provider.credentials['teamId'],
bundleId: $provider.credentials['bundleId']
$providerParams[$provider] = {
providerId: $providerData.$id,
name: $providerData.name,
enabled: $providerData.enabled,
authKey: $providerData.credentials['authKey'],
authKeyId: $providerData.credentials['authKeyId'],
teamId: $providerData.credentials['teamId'],
bundleId: $providerData.credentials['bundleId']
};
break;
}
@@ -164,8 +164,8 @@
async function updateStatus() {
try {
let response = { $id: '', name: '' };
const providerId = $provider.$id;
switch ($provider.provider) {
const providerId = $providerData.$id;
switch ($providerData.provider) {
case Providers.Twilio:
response = await sdk.forProject.messaging.updateTwilioProvider(
providerId,
@@ -256,7 +256,7 @@
message: `${response.name} has been ${enabled ? 'enabled' : 'disabled'}`
});
trackEvent(Submit.MessagingProviderUpdate, {
provider: $provider
provider: $providerData
});
} catch (error) {
addNotification({
@@ -270,8 +270,8 @@
<CardGrid>
<div class="grid-1-2-col-1 u-flex u-cross-center u-gap-16" data-private>
<Provider provider={$provider.provider} size="l">
<Heading tag="h6" size="7">{$provider.name}</Heading>
<Provider provider={$providerData.provider} size="l">
<Heading tag="h6" size="7">{$providerData.name}</Heading>
</Provider>
</div>
<svelte:fragment slot="aside">
@@ -283,9 +283,9 @@
label={enabled ? 'Enabled' : 'Disabled'}
bind:value={enabled} />
</ul>
<p class="title">Provider: <Provider noIcon provider={$provider.provider} /></p>
<p class="title">Channel: <ProviderType noIcon type={$provider.type} /></p>
<p>Created: {toLocaleDateTime($provider.$createdAt)}</p>
<p class="title">Provider: <Provider noIcon provider={$providerData.provider} /></p>
<p class="title">Channel: <ProviderType noIcon type={$providerData.type} /></p>
<p>Created: {toLocaleDateTime($providerData.$createdAt)}</p>
</div>
</div>
</svelte:fragment>
@@ -293,7 +293,7 @@
<svelte:fragment slot="actions">
<div class="u-flex u-flex-wrap u-gap-12">
<Button secondary on:click={() => configure()}>Configure</Button>
<Button disabled={$provider.enabled === enabled} on:click={() => updateStatus()}
<Button disabled={$providerData.enabled === enabled} on:click={() => updateStatus()}
>Update</Button>
</div>
</svelte:fragment>
@@ -0,0 +1,114 @@
<script lang="ts">
import {
FormList,
InputDomain,
InputEmail,
InputFile,
InputSelect,
InputSwitch,
InputText,
InputPassword
} from '$lib/elements/forms';
import InputPhone from '$lib/elements/forms/inputPhone.svelte';
import { provider, providerType, providerParams } from './store';
import PopoverContent from './popoverContent.svelte';
import { providers } from './store';
export let files: Record<string, FileList> = {};
const inputs = providers[$providerType].providers[$provider].configure;
</script>
<FormList>
{#each inputs as input}
{@const popoverImage = input.popoverImage}
{#if input.type === 'text'}
<InputText
id={input.name}
label={input.label}
placeholder={input.placeholder}
required={!input.optional}
isPopoverDefined={input.popover !== undefined}
bind:value={$providerParams[$provider][input.name]}>
<svelte:fragment slot="popover">
<PopoverContent lines={input.popover} {popoverImage} />
</svelte:fragment>
</InputText>
{:else if input.type === 'password'}
<InputPassword
id={input.name}
label={input.label}
placeholder={input.placeholder}
required={!input.optional}
showPasswordButton
isPopoverDefined={input.popover !== undefined}
bind:value={$providerParams[$provider][input.name]}>
<svelte:fragment slot="popover">
<PopoverContent lines={input.popover} {popoverImage} />
</svelte:fragment>
</InputPassword>
{:else if input.type === 'email'}
<InputEmail
id={input.name}
label={input.label}
placeholder={input.placeholder}
required={!input.optional}
isPopoverDefined={input.popover !== undefined}
bind:value={$providerParams[$provider][input.name]}>
<svelte:fragment slot="popover">
<PopoverContent lines={input.popover} {popoverImage} />
</svelte:fragment>
</InputEmail>
{:else if input.type === 'domain'}
<InputDomain
id={input.name}
label={input.label}
placeholder={input.placeholder}
required={!input.optional}
isPopoverDefined={input.popover !== undefined}
bind:value={$providerParams[$provider][input.name]}>
<svelte:fragment slot="popover">
<PopoverContent lines={input.popover} {popoverImage} />
</svelte:fragment>
</InputDomain>
{:else if input.type === 'phone'}
<InputPhone
id={input.name}
label={input.label}
placeholder={input.placeholder}
required={!input.optional}
isPopoverDefined={input.popover !== undefined}
bind:value={$providerParams[$provider][input.name]}>
<svelte:fragment slot="popover">
<PopoverContent lines={input.popover} {popoverImage} />
</svelte:fragment>
</InputPhone>
{:else if input.type === 'file'}
<InputFile
label={input.label}
allowedFileExtensions={input.allowedFileExtensions}
required={!input.optional}
isPopoverDefined={input.popover !== undefined}
bind:files={files[input.name]}>
<svelte:fragment slot="popover">
<PopoverContent lines={input.popover} {popoverImage} />
</svelte:fragment>
</InputFile>
{:else if input.type === 'switch'}
<InputSwitch
label={input.label}
id={input.name}
bind:value={$providerParams[$provider][input.name]}>
<svelte:fragment slot="description">
{input.description}
</svelte:fragment>
</InputSwitch>
{:else if input.type === 'select'}
<InputSelect
label={input.label}
id={input.name}
options={input.options}
required={!input.optional}
bind:value={$providerParams[$provider][input.name]} />
{/if}
{/each}
</FormList>
@@ -67,7 +67,7 @@ export const providers: ProvidersMap = {
popover: [
'<b>How to get the <a class="link" href="https://firebase.google.com" target="_blank" rel="noopener noreferrer">FCM</a> service account JSON?</b>',
'Head to <b>Project settings -> Service accounts -> Generate new private key.</b>',
'Generating the new key will result in the download of a JSON file.',
'Generating the new key will result in the download of a JSON file.'
],
popoverImage: {
src: {
@@ -533,3 +533,124 @@ export const providers: ProvidersMap = {
}
}
};
type ProviderParams = {
providerId: string;
name: string;
enabled: boolean;
};
/**
* SMS providers
*/
export type TwilioProviderParams = ProviderParams & {
accountSid: string;
authToken: string;
from: string;
};
export type Msg91ProviderParams = ProviderParams & {
from: string;
senderId: string;
authKey: string;
};
export type TelesignProviderParams = ProviderParams & {
from: string;
username: string;
password: string;
};
export type TextmagicProviderParams = ProviderParams & {
from: string;
username: string;
apiKey: string;
};
export type VonageProviderParams = ProviderParams & {
from: string;
apiKey: string;
apiSecret: string;
};
/**
* Email providers
*/
export type MailgunProviderParams = ProviderParams & {
fromEmail: string;
fromName: string;
replyToEmail: string;
replyToName: string;
isEuRegion: boolean;
apiKey: string;
domain: string;
};
export type SendgridProviderParams = ProviderParams & {
fromEmail: string;
fromName: string;
replyToEmail: string;
replyToName: string;
apiKey: string;
};
export type SMTPProviderParams = ProviderParams & {
fromEmail: string;
fromName: string;
replyToEmail: string;
replyToName: string;
host: string;
port: number;
username: string;
password: string;
encryption: SMTPEncryption;
autoTLS: boolean;
mailer: string;
};
/**
* Push providers
*/
export type FCMProviderParams = ProviderParams & {
serviceAccountJSON: string;
};
export type APNSProviderParams = ProviderParams & {
authKey: string;
authKeyId: string;
teamId: string;
bundleId: string;
};
export type MQTTProviderParams = ProviderParams & {
serverKey: string;
};
export const providerType = writable<MessagingProviderType>(null);
export const provider = writable<Providers>(null);
export const providerParams = writable<{
twilio: Partial<TwilioProviderParams>;
msg91: Partial<Msg91ProviderParams>;
telesign: Partial<TelesignProviderParams>;
textmagic: Partial<TextmagicProviderParams>;
vonage: Partial<VonageProviderParams>;
mailgun: Partial<MailgunProviderParams>;
sendgrid: Partial<SendgridProviderParams>;
smtp: Partial<SMTPProviderParams>;
fcm: Partial<FCMProviderParams>;
apns: Partial<APNSProviderParams>;
}>({
twilio: null,
msg91: null,
telesign: null,
textmagic: null,
vonage: null,
mailgun: null,
sendgrid: null,
smtp: null,
fcm: null,
apns: null
});
@@ -9,7 +9,7 @@
import { base } from '$app/paths';
import { project } from '../../store';
import { wizard } from '$lib/stores/wizard';
import { provider, providerParams } from './wizard/store';
import { provider, providerParams } from './store';
import { Providers } from '../provider.svelte';
import { Dependencies } from '$lib/constants';
import type { Models } from '@appwrite.io/console';
@@ -1,6 +1,6 @@
<script lang="ts">
import { WizardStep } from '$lib/layout';
import { providerType, provider, providerParams } from './store';
import { providerType, provider, providerParams } from '../store';
import { CustomId, LabelCard } from '$lib/components';
import { providers } from '../store';
import { FormList, InputText } from '$lib/elements/forms';
@@ -1,21 +1,9 @@
<script lang="ts">
import {
FormList,
InputDomain,
InputEmail,
InputFile,
InputSelect,
InputSwitch,
InputText,
InputPassword
} from '$lib/elements/forms';
import InputPhone from '$lib/elements/forms/inputPhone.svelte';
import { WizardStep } from '$lib/layout';
import { onMount } from 'svelte';
import { providers } from '../store';
import { providerType, provider, providerParams } from './store';
import PopoverContent from './popoverContent.svelte';
import { providerType, provider, providerParams } from '../store';
import SettingsFormList from '../settingsFormList.svelte';
let files: Record<string, FileList> = {};
const inputs = providers[$providerType].providers[$provider].configure;
@@ -53,100 +41,8 @@
for sending
{providers[$providerType].text}.
</svelte:fragment>
<FormList>
{#each inputs as input}
{@const popoverImage = input.popoverImage}
{#if input.type === 'text'}
<InputText
id={input.name}
label={input.label}
placeholder={input.placeholder}
required={!input.optional}
isPopoverDefined={input.popover !== undefined}
bind:value={$providerParams[$provider][input.name]}>
<svelte:fragment slot="popover">
<PopoverContent lines={input.popover} {popoverImage} />
</svelte:fragment>
</InputText>
{:else if input.type === 'password'}
<InputPassword
id={input.name}
label={input.label}
placeholder={input.placeholder}
required={!input.optional}
showPasswordButton
isPopoverDefined={input.popover !== undefined}
bind:value={$providerParams[$provider][input.name]}>
<svelte:fragment slot="popover">
<PopoverContent lines={input.popover} {popoverImage} />
</svelte:fragment>
</InputPassword>
{:else if input.type === 'email'}
<InputEmail
id={input.name}
label={input.label}
placeholder={input.placeholder}
required={!input.optional}
isPopoverDefined={input.popover !== undefined}
bind:value={$providerParams[$provider][input.name]}>
<svelte:fragment slot="popover">
<PopoverContent lines={input.popover} {popoverImage} />
</svelte:fragment>
</InputEmail>
{:else if input.type === 'domain'}
<InputDomain
id={input.name}
label={input.label}
placeholder={input.placeholder}
required={!input.optional}
isPopoverDefined={input.popover !== undefined}
bind:value={$providerParams[$provider][input.name]}>
<svelte:fragment slot="popover">
<PopoverContent lines={input.popover} {popoverImage} />
</svelte:fragment>
</InputDomain>
{:else if input.type === 'phone'}
<InputPhone
id={input.name}
label={input.label}
placeholder={input.placeholder}
required={!input.optional}
isPopoverDefined={input.popover !== undefined}
bind:value={$providerParams[$provider][input.name]}>
<svelte:fragment slot="popover">
<PopoverContent lines={input.popover} {popoverImage} />
</svelte:fragment>
</InputPhone>
{:else if input.type === 'file'}
<InputFile
label={input.label}
allowedFileExtensions={input.allowedFileExtensions}
required={!input.optional}
isPopoverDefined={input.popover !== undefined}
bind:files={files[input.name]}>
<svelte:fragment slot="popover">
<PopoverContent lines={input.popover} {popoverImage} />
</svelte:fragment>
</InputFile>
{:else if input.type === 'switch'}
<InputSwitch
label={input.label}
id={input.name}
bind:value={$providerParams[$provider][input.name]}>
<svelte:fragment slot="description">
{input.description}
</svelte:fragment>
</InputSwitch>
{:else if input.type === 'select'}
<InputSelect
label={input.label}
id={input.name}
options={input.options}
required={!input.optional}
bind:value={$providerParams[$provider][input.name]} />
{/if}
{/each}
</FormList>
<SettingsFormList bind:files />
<p class="body-text-2 u-bold u-margin-block-start-48">Need a hand?</p>
@@ -1,124 +0,0 @@
import { writable } from 'svelte/store';
import type { Providers } from '../../provider.svelte';
import type { MessagingProviderType, SMTPEncryption } from '@appwrite.io/console';
type ProviderParams = {
providerId: string;
name: string;
enabled: boolean;
};
/**
* SMS providers
*/
export type TwilioProviderParams = ProviderParams & {
accountSid: string;
authToken: string;
from: string;
};
export type Msg91ProviderParams = ProviderParams & {
from: string;
senderId: string;
authKey: string;
};
export type TelesignProviderParams = ProviderParams & {
from: string;
username: string;
password: string;
};
export type TextmagicProviderParams = ProviderParams & {
from: string;
username: string;
apiKey: string;
};
export type VonageProviderParams = ProviderParams & {
from: string;
apiKey: string;
apiSecret: string;
};
/**
* Email providers
*/
export type MailgunProviderParams = ProviderParams & {
fromEmail: string;
fromName: string;
replyToEmail: string;
replyToName: string;
isEuRegion: boolean;
apiKey: string;
domain: string;
};
export type SendgridProviderParams = ProviderParams & {
fromEmail: string;
fromName: string;
replyToEmail: string;
replyToName: string;
apiKey: string;
};
export type SMTPProviderParams = ProviderParams & {
fromEmail: string;
fromName: string;
replyToEmail: string;
replyToName: string;
host: string;
port: number;
username: string;
password: string;
encryption: SMTPEncryption;
autoTLS: boolean;
mailer: string;
};
/**
* Push providers
*/
export type FCMProviderParams = ProviderParams & {
serviceAccountJSON: string;
};
export type APNSProviderParams = ProviderParams & {
authKey: string;
authKeyId: string;
teamId: string;
bundleId: string;
};
export type MQTTProviderParams = ProviderParams & {
serverKey: string;
};
export const providerType = writable<MessagingProviderType>(null);
export const provider = writable<Providers>(null);
export const providerParams = writable<{
twilio: Partial<TwilioProviderParams>;
msg91: Partial<Msg91ProviderParams>;
telesign: Partial<TelesignProviderParams>;
textmagic: Partial<TextmagicProviderParams>;
vonage: Partial<VonageProviderParams>;
mailgun: Partial<MailgunProviderParams>;
sendgrid: Partial<SendgridProviderParams>;
smtp: Partial<SMTPProviderParams>;
fcm: Partial<FCMProviderParams>;
apns: Partial<APNSProviderParams>;
}>({
twilio: null,
msg91: null,
telesign: null,
textmagic: null,
vonage: null,
mailgun: null,
sendgrid: null,
smtp: null,
fcm: null,
apns: null
});