|
| 1 | +<script context="module" lang="ts"> |
| 2 | + export async function createSMSMessage(params: EmailMessageParams) { |
| 3 | + const response = await sdk.forProject.client.call( |
| 4 | + 'POST', |
| 5 | + new URL(sdk.forProject.client.config.endpoint + '/messaging/messages/sms'), |
| 6 | + { |
| 7 | + 'X-Appwrite-Project': sdk.forProject.client.config.project, |
| 8 | + 'content-type': 'application/json', |
| 9 | + 'X-Appwrite-Mode': 'admin' |
| 10 | + }, |
| 11 | + params |
| 12 | + ); |
| 13 | +
|
| 14 | + return response.json(); |
| 15 | + } |
| 16 | +</script> |
| 17 | + |
| 18 | +<script lang="ts"> |
| 19 | + import { messageParams, providerType, type EmailMessageParams, MessageStatuses } from './store'; |
| 20 | + import { |
| 21 | + Button, |
| 22 | + FormList, |
| 23 | + InputEmail, |
| 24 | + InputRadio, |
| 25 | + InputTextarea |
| 26 | + } from '$lib/elements/forms'; |
| 27 | + import { Pill } from '$lib/elements'; |
| 28 | + import { CustomId, Modal } from '$lib/components'; |
| 29 | + import { user } from '$lib/stores/user'; |
| 30 | + import { clickOnEnter } from '$lib/helpers/a11y'; |
| 31 | + import { ID } from '@appwrite.io/console'; |
| 32 | + import { sdk } from '$lib/stores/sdk'; |
| 33 | + import { ProviderTypes } from '../providerType.svelte'; |
| 34 | +
|
| 35 | + let showCustomId = false; |
| 36 | + let showTest = false; |
| 37 | + let selected = 'self'; |
| 38 | + let otherEmail = ''; |
| 39 | +
|
| 40 | + async function sendTestSMS() { |
| 41 | + const email = selected === 'self' ? $user.email : otherEmail; |
| 42 | + console.log(email); |
| 43 | +
|
| 44 | + createSMSMessage({ |
| 45 | + topics: $messageParams[ProviderTypes.Email]?.topics || [], |
| 46 | + targets: $messageParams[ProviderTypes.Email]?.targets || [], |
| 47 | + description: $messageParams[ProviderTypes.Email]?.description || 'Test message', |
| 48 | + status: MessageStatuses.PROCESSING, |
| 49 | + messageId: ID.unique(), |
| 50 | + // TODO: properly handle the test email address |
| 51 | + users: ['steven'], |
| 52 | + subject: $messageParams[ProviderTypes.Email]?.subject || '', |
| 53 | + content: $messageParams[ProviderTypes.Email]?.content || '', |
| 54 | + html: $messageParams[ProviderTypes.Email]?.html || false |
| 55 | + }); |
| 56 | + } |
| 57 | +
|
| 58 | + $: otherEmail = selected === 'self' ? '' : otherEmail; |
| 59 | +</script> |
| 60 | + |
| 61 | +<div class="u-flex u-gap-24"> |
| 62 | + <FormList class="u-stretch"> |
| 63 | + <div class="u-colum-gap-2"> |
| 64 | + <InputTextarea |
| 65 | + id="message" |
| 66 | + label="Message" |
| 67 | + placeholder="Type here..." |
| 68 | + bind:value={$messageParams[$providerType]['content']}> |
| 69 | + </InputTextarea> |
| 70 | + <!-- TODO: Add support for draft messages --> |
| 71 | + <!-- <div class="u-flex u-main-end"> |
| 72 | + <Button text on:click={() => (showTest = true)}>Send test message</Button> |
| 73 | + </div> --> |
| 74 | + <Modal title="Send test message" bind:show={showTest} onSubmit={sendTestSMS} size="big"> |
| 75 | + <slot /> |
| 76 | + <InputRadio |
| 77 | + label={$user.phone} |
| 78 | + bind:group={selected} |
| 79 | + value="self" |
| 80 | + id="self" |
| 81 | + name="selected" /> |
| 82 | + <InputRadio |
| 83 | + label="Other" |
| 84 | + bind:group={selected} |
| 85 | + value="other" |
| 86 | + id="other" |
| 87 | + name="selected" |
| 88 | + fullWidth> |
| 89 | + <svelte:fragment slot="description"> |
| 90 | + Enter the phone number to which the test message will be |
| 91 | + <div |
| 92 | + on:click={() => (selected = 'other')} |
| 93 | + on:keyup|self={clickOnEnter} |
| 94 | + role="button" |
| 95 | + tabindex="0"> |
| 96 | + <InputEmail |
| 97 | + showLabel={false} |
| 98 | + id="email" |
| 99 | + label="Email" |
| 100 | + placeholder="Enter email" |
| 101 | + bind:value={otherEmail} /> |
| 102 | + </div> |
| 103 | + </svelte:fragment> |
| 104 | + </InputRadio> |
| 105 | + |
| 106 | + <svelte:fragment slot="footer"> |
| 107 | + <Button secondary on:click={() => (showTest = false)}>Cancel</Button> |
| 108 | + <Button submit>Send</Button> |
| 109 | + </svelte:fragment> |
| 110 | + </Modal> |
| 111 | + </div> |
| 112 | + {#if !showCustomId} |
| 113 | + <div> |
| 114 | + <Pill button on:click={() => (showCustomId = !showCustomId)} |
| 115 | + ><span class="icon-pencil" aria-hidden="true" /><span class="text"> |
| 116 | + Message ID |
| 117 | + </span></Pill> |
| 118 | + </div> |
| 119 | + {:else} |
| 120 | + <CustomId |
| 121 | + bind:show={showCustomId} |
| 122 | + name="Message" |
| 123 | + bind:id={$messageParams[$providerType].messageId} |
| 124 | + autofocus={false} /> |
| 125 | + {/if} |
| 126 | + </FormList> |
| 127 | + <!-- TODO: show a preview of the SMS --> |
| 128 | + <!-- <div class="u-width-250 card is-only-desktop"> |
| 129 | + aksldjfk |
| 130 | + </div> --> |
| 131 | +</div> |
0 commit comments