Skip to content

Commit

Permalink
Merge pull request #15282 from Budibase/type-portal-email-store
Browse files Browse the repository at this point in the history
Convert portal email store to TS
  • Loading branch information
aptkingston authored Jan 6, 2025
2 parents 9ca9416 + 69b9509 commit 7dc5cdd
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
onMount(async () => {
try {
await email.templates.fetch()
await email.fetchTemplates()
} catch (error) {
notifications.error("Error fetching email templates")
}
Expand Down
36 changes: 0 additions & 36 deletions packages/builder/src/stores/portal/email.js

This file was deleted.

43 changes: 43 additions & 0 deletions packages/builder/src/stores/portal/email.ts
Original file line number Diff line number Diff line change
@@ -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<EmailState> {
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()

0 comments on commit 7dc5cdd

Please sign in to comment.