Skip to content

Commit

Permalink
Add support for saving messages as draft
Browse files Browse the repository at this point in the history
  • Loading branch information
stnguyen90 committed Jan 19, 2024
1 parent 99fd70b commit c0e2cbb
Show file tree
Hide file tree
Showing 16 changed files with 566 additions and 204 deletions.
1 change: 1 addition & 0 deletions src/lib/actions/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ export enum Submit {
MessagingProviderDelete = 'submit_messaging_provider_delete',
MessagingProviderUpdate = 'submit_messaging_provider_update',
MessagingMessageCreate = 'submit_messaging_message_create',
MessagingMessageUpdate = 'submit_messaging_message_update',
MessagingMessageDelete = 'submit_messaging_message_delete',
MessagingTopicCreate = 'submit_messaging_topic_create',
MessagingTopicDelete = 'submit_messaging_topic_delete',
Expand Down
114 changes: 0 additions & 114 deletions src/routes/console/project-[project]/messaging/create.svelte

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import { Button } from '$lib/elements/forms';
import { wizard } from '$lib/stores/wizard';
import { providers } from './providers/store';
import Create from './create.svelte';
import { messageParams, providerType, targetsById } from './wizard/store';
import Wizard from './wizard.svelte';
import { messageParams, operation, providerType, targetsById } from './wizard/store';
import { ProviderTypes } from './providerType.svelte';
import { topicsById } from './store';
Expand All @@ -30,6 +30,7 @@
)
return;
$providerType = type;
$operation = 'create';
$topicsById = {};
$targetsById = {};
const common = {
Expand Down Expand Up @@ -60,7 +61,7 @@
break;
}
showCreateDropdown = false;
wizard.start(Create);
wizard.start(Wizard);
}}>
{option.name}
</DropListItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,51 @@ export const load: LayoutLoad = async ({ params, depends }) => {
}
);

const topicsById = {};
const topicsPromise = Promise.allSettled(
response.topics.map((topicId) => {
return sdk.forProject.client.call(
'GET',
new URL(`${sdk.forProject.client.config.endpoint}/messaging/topics/${topicId}`),
{
'X-Appwrite-Project': sdk.forProject.client.config.project,
'content-type': 'application/json',
'X-Appwrite-Mode': 'admin'
}
);
})
).then((results) => {
results.forEach((result) => {
if (result.status === 'fulfilled') {
topicsById[result.value.$id] = result.value;
}
});
});

const targetsById = {};
const targetsPromise = sdk.forProject.client
.call(
'GET',
new URL(
`${sdk.forProject.client.config.endpoint}/messaging/messages/${params.message}/targets`
),
{
'X-Appwrite-Project': sdk.forProject.client.config.project,
'content-type': 'application/json',
'X-Appwrite-Mode': 'admin'
}
)
.then((response) => {
response.targets.forEach((target) => {
targetsById[target.$id] = target;
});
});

await Promise.allSettled([topicsPromise, targetsPromise]);

return {
topicsById,
targetsById,
header: Header,
breadcrumbs: Breadcrumbs,
message: response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,107 @@
import { ProviderTypes } from '../providerType.svelte';
import SMSPreview from './smsPreview.svelte';
import PushPreview from './pushPreview.svelte';
import {
MessageStatuses,
messageParams,
operation,
providerType,
targetsById
} from '../wizard/store';
import { topicsById } from '../store';
import { wizard } from '$lib/stores/wizard';
import Wizard from '../wizard.svelte';
import type { PageData } from './$types';
export let data: PageData;
async function onEdit() {
$operation = 'update';
$providerType = $message.providerType;
$topicsById = {};
$targetsById = {};
$topicsById = data.topicsById;
$targetsById = data.targetsById;
$messageParams[$providerType] = {
messageId: $message.$id,
topics: $message.topics,
users: $message.users,
targets: $message.targets,
description: $message.description,
status: MessageStatuses.DRAFT,
scheduledAt: $message.scheduledAt
};
switch ($providerType) {
case ProviderTypes.Email:
{
const { data } = $message;
const params = ['subject', 'content', 'html'];
params.forEach((key) => {
if (typeof data[key] !== 'undefined') {
$messageParams[$providerType][key] = data[key];
}
});
}
break;
case ProviderTypes.Sms:
{
const { data } = $message;
const params = ['content'];
params.forEach((key) => {
if (typeof data[key] !== 'undefined') {
$messageParams[$providerType][key] = data[key];
}
});
}
break;
case ProviderTypes.Push:
{
const { data } = $message;
const params = [
'title',
'body',
'action',
'icon',
'sound',
'color',
'tag',
'badge'
];
params.forEach((key) => {
if (typeof data[key] !== 'undefined') {
$messageParams[$providerType][key] = data[key];
}
});
const dataEntries: [string, string][] = [];
Object.entries(data['data'] ?? {}).forEach(([key, value]) => {
dataEntries.push([key, value.toString()]);
});
$messageParams[$providerType]['data'] = dataEntries || [['', '']];
}
break;
}
wizard.start(Wizard);
}
</script>

<Container>
<Overview />
{#if $message.providerType === ProviderTypes.Email}
<EmailPreview />
<EmailPreview
message={$message}
onEdit={$message.status === MessageStatuses.DRAFT ? onEdit : null} />
{:else if $message.providerType === ProviderTypes.Sms}
<SMSPreview />
<SMSPreview
message={$message}
onEdit={$message.status === MessageStatuses.DRAFT ? onEdit : null} />
{:else if $message.providerType === ProviderTypes.Push}
<PushPreview />
<PushPreview
message={$message}
onEdit={$message.status === MessageStatuses.DRAFT ? onEdit : null} />
{/if}
<Delete />
</Container>
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<script lang="ts">
import { CardGrid, Heading } from '$lib/components';
import { FormList, InputText, InputTextarea } from '$lib/elements/forms';
import { message } from './store';
import { Button, FormList, InputText, InputTextarea } from '$lib/elements/forms';
import type { Message } from '../store';
export let message: Message;
export let onEdit: () => void = null;
</script>

<CardGrid>
Expand All @@ -14,20 +17,17 @@
id="subject"
label="Subject"
disabled={true}
bind:value={$message.data.subject}>
bind:value={message.data.subject}>
</InputText>
<InputTextarea
id="message"
label="Message"
disabled={true}
bind:value={$message.data.content}>
bind:value={message.data.content}>
</InputTextarea>
<div class="u-flex u-main-end">
<Button secondary disabled={onEdit == null} on:click={onEdit}>Edit message</Button>
</div>
</FormList>
</svelte:fragment>

<svelte:fragment slot="actions">
<!-- TODO: Add support for editing draft messages -->
<!-- <Button disabled={$message.status !== 'draft'} on:click={() => console.log('click')}
>Edit message</Button> -->
</svelte:fragment>
</CardGrid>
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
<script lang="ts">
import { CardGrid, Heading } from '$lib/components';
import { FormList, InputText, InputTextarea } from '$lib/elements/forms';
import { message } from './store';
import { Button, FormList, InputText, InputTextarea } from '$lib/elements/forms';
import PushPhone from '../pushPhone.svelte';
import type { Message } from '../store';
export let message: Message;
export let onEdit: () => void = null;
</script>

<CardGrid>
<div class="grid-1-2-col-1 u-flex-vertical u-cross-start u-gap-16">
<Heading tag="h6" size="7">Preview</Heading>
<div class="u-flex u-main-center u-margin-block-start-24 u-width-full-line">
<PushPhone title={$message.data.title} body={$message.data.body} />
<PushPhone title={message.data.title} body={message.data.body} />
</div>
</div>
<svelte:fragment slot="aside">
<FormList>
<InputText id="title" label="Title" disabled={true} bind:value={$message.data.title}>
<InputText id="title" label="Title" disabled={true} bind:value={message.data.title}>
</InputText>
<InputTextarea
id="message"
label="Message"
disabled={true}
bind:value={$message.data.body}>
bind:value={message.data.body}>
</InputTextarea>
<div class="u-flex u-main-end">
<Button secondary disabled={onEdit == null} on:click={onEdit}>Edit message</Button>
</div>
</FormList>
</svelte:fragment>

<svelte:fragment slot="actions">
<!-- TODO: Add support for editing draft messages -->
<!-- <Button disabled={$message.status !== 'draft'} on:click={() => console.log('click')}
>Edit message</Button> -->
</svelte:fragment>
</CardGrid>
Loading

0 comments on commit c0e2cbb

Please sign in to comment.