Convert portal email store to TS

This commit is contained in:
Andrew Kingston 2025-01-02 11:57:42 +00:00
parent c21581f4cc
commit de3f70066a
No known key found for this signature in database
4 changed files with 45 additions and 38 deletions

View File

@ -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")

View File

@ -5,7 +5,7 @@
onMount(async () => {
try {
await email.templates.fetch()
await email.fetchTemplates()
} catch (error) {
notifications.error("Error fetching email templates")
}

View File

@ -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()

View File

@ -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()