|
1 | 1 | <script lang="ts">
|
2 |
| - import { Submit, trackEvent, trackError } from '$lib/actions/analytics'; |
3 |
| - import { Modal, CustomId } from '$lib/components'; |
4 |
| - import { Pill } from '$lib/elements'; |
5 |
| - import { Button, InputText, FormList } from '$lib/elements/forms'; |
6 |
| - import { addNotification } from '$lib/stores/notifications'; |
| 2 | + import { onDestroy } from 'svelte'; |
| 3 | + import { Wizard } from '$lib/layout'; |
| 4 | + import type { WizardStepsType } from '$lib/layout/wizard.svelte'; |
| 5 | + import Step1 from './wizard/step1.svelte'; |
| 6 | + import Step2 from './wizard/step2.svelte'; |
| 7 | + import Step3 from './wizard/step3.svelte'; |
7 | 8 | import { sdk } from '$lib/stores/sdk';
|
| 9 | + import { Submit, trackError, trackEvent } from '$lib/actions/analytics'; |
| 10 | + import { addNotification } from '$lib/stores/notifications'; |
| 11 | + import { goto } from '$app/navigation'; |
| 12 | + import { base } from '$app/paths'; |
| 13 | + import { project } from '../store'; |
| 14 | + import { wizard } from '$lib/stores/wizard'; |
| 15 | + import { providerType, messageParams } from './wizard/store'; |
| 16 | + import { ProviderTypes } from './providerType.svelte'; |
8 | 17 | import { ID } from '@appwrite.io/console';
|
9 |
| - import { createEventDispatcher } from 'svelte'; |
10 |
| -
|
11 |
| - export let showCreate = false; |
12 | 18 |
|
13 |
| - const dispatch = createEventDispatcher(); |
14 |
| -
|
15 |
| - let name = ''; |
16 |
| - let id: string = null; |
17 |
| - let showCustomId = false; |
18 |
| - let error: string; |
19 |
| -
|
20 |
| - const create = async () => { |
| 19 | + async function create() { |
21 | 20 | try {
|
22 |
| - const bucket = await sdk.forProject.storage.createBucket(id ? id : ID.unique(), name); |
23 |
| - showCreate = false; |
24 |
| - dispatch('created', bucket); |
| 21 | + let response = { $id: '' }; |
| 22 | + const messageId = $messageParams[$providerType].messageId || ID.unique(); |
| 23 | + switch ($providerType) { |
| 24 | + case ProviderTypes.Email: |
| 25 | + response = await sdk.forProject.client.call( |
| 26 | + 'POST', |
| 27 | + new URL( |
| 28 | + sdk.forProject.client.config.endpoint + '/messaging/messages/email' |
| 29 | + ), |
| 30 | + { |
| 31 | + 'X-Appwrite-Project': sdk.forProject.client.config.project, |
| 32 | + 'content-type': 'application/json', |
| 33 | + 'X-Appwrite-Mode': 'admin' |
| 34 | + }, |
| 35 | + { |
| 36 | + ...$messageParams[$providerType], |
| 37 | + messageId |
| 38 | + } |
| 39 | + ); |
| 40 | + break; |
| 41 | + case ProviderTypes.Sms: |
| 42 | + response = await sdk.forProject.client.call( |
| 43 | + 'POST', |
| 44 | + new URL(sdk.forProject.client.config.endpoint + '/messaging/messages/sms'), |
| 45 | + { |
| 46 | + 'X-Appwrite-Project': sdk.forProject.client.config.project, |
| 47 | + 'content-type': 'application/json', |
| 48 | + 'X-Appwrite-Mode': 'admin' |
| 49 | + }, |
| 50 | + { |
| 51 | + ...$messageParams[$providerType], |
| 52 | + messageId |
| 53 | + } |
| 54 | + ); |
| 55 | + break; |
| 56 | + case ProviderTypes.Push: |
| 57 | + response = await sdk.forProject.client.call( |
| 58 | + 'POST', |
| 59 | + new URL( |
| 60 | + sdk.forProject.client.config.endpoint + '/messaging/providers/telesign' |
| 61 | + ), |
| 62 | + { |
| 63 | + 'X-Appwrite-Project': sdk.forProject.client.config.project, |
| 64 | + 'content-type': 'application/json', |
| 65 | + 'X-Appwrite-Mode': 'admin' |
| 66 | + }, |
| 67 | + { |
| 68 | + ...$messageParams[$providerType], |
| 69 | + messageId |
| 70 | + } |
| 71 | + ); |
| 72 | + break; |
| 73 | + } |
| 74 | + wizard.hide(); |
25 | 75 | addNotification({
|
26 | 76 | type: 'success',
|
27 |
| - message: `${name} has been created` |
| 77 | + message: `The message has been sent.` |
| 78 | + }); |
| 79 | + trackEvent(Submit.MessagingMessageCreate, { |
| 80 | + providerType: $providerType |
28 | 81 | });
|
29 |
| - name = null; |
30 |
| - trackEvent(Submit.BucketCreate, { |
31 |
| - customId: !!id |
| 82 | + await goto(`${base}/console/project-${$project.$id}/messaging/message-${response.$id}`); |
| 83 | + } catch (error) { |
| 84 | + addNotification({ |
| 85 | + type: 'error', |
| 86 | + message: error.message |
32 | 87 | });
|
33 |
| - } catch (e) { |
34 |
| - error = e.message; |
35 |
| - trackError(e, Submit.BucketCreate); |
| 88 | + trackError(error, Submit.MessagingProviderCreate); |
36 | 89 | }
|
37 |
| - }; |
38 |
| -
|
39 |
| - $: if (!showCustomId) { |
40 |
| - id = null; |
41 |
| - } |
42 |
| - $: if (!showCreate) { |
43 |
| - showCustomId = false; |
44 |
| - error = null; |
45 | 90 | }
|
46 |
| -</script> |
47 | 91 |
|
48 |
| -<Modal title="Create message" {error} onSubmit={create} size="big" bind:show={showCreate}> |
49 |
| - <FormList> |
50 |
| - <InputText |
51 |
| - id="name" |
52 |
| - label="Name" |
53 |
| - placeholder="New bucket" |
54 |
| - bind:value={name} |
55 |
| - autofocus |
56 |
| - required /> |
| 92 | + onDestroy(() => { |
| 93 | + console.log('destroy'); |
| 94 | + }); |
| 95 | +
|
| 96 | + const stepsComponents: WizardStepsType = new Map(); |
| 97 | + stepsComponents.set(1, { |
| 98 | + label: 'Message', |
| 99 | + component: Step1 |
| 100 | + }); |
| 101 | + stepsComponents.set(2, { |
| 102 | + label: 'Targets', |
| 103 | + component: Step2 |
| 104 | + }); |
| 105 | + stepsComponents.set(3, { |
| 106 | + label: 'Schedule', |
| 107 | + component: Step3 |
| 108 | + }); |
| 109 | +</script> |
57 | 110 |
|
58 |
| - {#if !showCustomId} |
59 |
| - <div> |
60 |
| - <Pill button on:click={() => (showCustomId = !showCustomId)}> |
61 |
| - <span class="icon-pencil" aria-hidden="true" /> |
62 |
| - <span class="text"> Bucket ID </span> |
63 |
| - </Pill> |
64 |
| - </div> |
65 |
| - {:else} |
66 |
| - <CustomId bind:show={showCustomId} name="Bucket" bind:id /> |
67 |
| - {/if} |
68 |
| - </FormList> |
69 |
| - <svelte:fragment slot="footer"> |
70 |
| - <Button secondary on:click={() => (showCreate = false)}>Cancel</Button> |
71 |
| - <Button submit>Create</Button> |
72 |
| - </svelte:fragment> |
73 |
| -</Modal> |
| 111 | +<Wizard title="Create message" steps={stepsComponents} on:finish={create} finalAction="Send" /> |
0 commit comments