diff --git a/packages/builder/src/pages/builder/portal/settings/email/[template].svelte b/packages/builder/src/pages/builder/portal/settings/email/[template].svelte index a9c02753678..7fbf6d3361b 100644 --- a/packages/builder/src/pages/builder/portal/settings/email/[template].svelte +++ b/packages/builder/src/pages/builder/portal/settings/email/[template].svelte @@ -34,7 +34,7 @@ async function saveTemplate() { try { // Save your template config - await email.templates.save(selectedTemplate) + await email.saveTemplate(selectedTemplate) notifications.success("Template saved") } catch (error) { notifications.error("Failed to update template settings") diff --git a/packages/builder/src/pages/builder/portal/settings/email/_layout.svelte b/packages/builder/src/pages/builder/portal/settings/email/_layout.svelte index c0d4d4463e1..14e7d2c3ca9 100644 --- a/packages/builder/src/pages/builder/portal/settings/email/_layout.svelte +++ b/packages/builder/src/pages/builder/portal/settings/email/_layout.svelte @@ -5,7 +5,7 @@ onMount(async () => { try { - await email.templates.fetch() + await email.fetchTemplates() } catch (error) { notifications.error("Error fetching email templates") } diff --git a/packages/builder/src/stores/portal/email.js b/packages/builder/src/stores/portal/email.js deleted file mode 100644 index 7afd5430025..00000000000 --- a/packages/builder/src/stores/portal/email.js +++ /dev/null @@ -1,36 +0,0 @@ -import { writable } from "svelte/store" -import { API } from "@/api" - -export function createEmailStore() { - const store = writable({}) - - return { - subscribe: store.subscribe, - templates: { - fetch: async () => { - // Fetch the email template definitions and templates - const definitions = await API.getEmailTemplateDefinitions() - const templates = await API.getEmailTemplates() - store.set({ - definitions, - templates, - }) - }, - save: async template => { - // Save your template config - const savedTemplate = await API.saveEmailTemplate(template) - template._rev = savedTemplate._rev - template._id = savedTemplate._id - store.update(state => { - const currentIdx = state.templates.findIndex( - template => template.purpose === savedTemplate.purpose - ) - state.templates.splice(currentIdx, 1, template) - return state - }) - }, - }, - } -} - -export const email = createEmailStore() diff --git a/packages/builder/src/stores/portal/email.ts b/packages/builder/src/stores/portal/email.ts new file mode 100644 index 00000000000..290f443c780 --- /dev/null +++ b/packages/builder/src/stores/portal/email.ts @@ -0,0 +1,43 @@ +import { API } from "@/api" +import { BudiStore } from "../BudiStore" +import { + FetchGlobalTemplateDefinitionResponse, + Template, +} from "@budibase/types" + +interface EmailState { + definitions?: FetchGlobalTemplateDefinitionResponse + templates: Template[] +} + +class EmailStore extends BudiStore { + constructor() { + super({ + templates: [], + }) + } + + async fetchTemplates() { + const definitions = await API.getEmailTemplateDefinitions() + const templates = await API.getEmailTemplates() + this.set({ + definitions, + templates, + }) + } + + async saveTemplate(template: Template) { + const savedTemplate = await API.saveEmailTemplate(template) + template._rev = savedTemplate._rev + template._id = savedTemplate._id + this.update(state => { + const currentIdx = state.templates.findIndex( + template => template.purpose === savedTemplate.purpose + ) + state.templates.splice(currentIdx, 1, template) + return state + }) + } +} + +export const email = new EmailStore()