Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for saving messages as draft #737

Merged
merged 2 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 13 additions & 1 deletion src/lib/layout/wizard.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
component: typeof SvelteComponent<unknown>;
optional?: boolean;
disabled?: boolean;
actions?: {
label: string;
onClick: () => Promise<void>;
}[];
}
>;
</script>
Expand Down Expand Up @@ -127,6 +131,7 @@

$: sortedSteps = [...steps].sort(([a], [b]) => (a > b ? 1 : -1));
$: isLastStep = $wizard.step === steps.size;
$: currentStep = steps.get($wizard.step);
</script>

<svelte:window on:keydown={handleKeydown} />
Expand Down Expand Up @@ -175,7 +180,7 @@
{/each}
<div class="form-footer">
<div class="u-flex u-main-end u-gap-12">
{#if !isLastStep && sortedSteps[$wizard.step - 1]?.[1]?.optional}
{#if !isLastStep && currentStep?.optional}
<Button text on:click={() => dispatch('finish')}>
Skip optional steps
</Button>
Expand All @@ -187,6 +192,13 @@
<Button secondary on:click={previousStep}>Back</Button>
{/if}

{#if currentStep?.actions}
{#each currentStep.actions as action}
<Button secondary on:click={action.onClick}>
{action.label}</Button>
{/each}
{/if}

<Button submit disabled={$wizard.nextDisabled}>
{isLastStep ? finalAction : 'Next'}
</Button>
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>
Loading