Convert portal email store to TS
This commit is contained in:
parent
c21581f4cc
commit
de3f70066a
|
@ -34,7 +34,7 @@
|
||||||
async function saveTemplate() {
|
async function saveTemplate() {
|
||||||
try {
|
try {
|
||||||
// Save your template config
|
// Save your template config
|
||||||
await email.templates.save(selectedTemplate)
|
await email.saveTemplate(selectedTemplate)
|
||||||
notifications.success("Template saved")
|
notifications.success("Template saved")
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
notifications.error("Failed to update template settings")
|
notifications.error("Failed to update template settings")
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
try {
|
try {
|
||||||
await email.templates.fetch()
|
await email.fetchTemplates()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
notifications.error("Error fetching email templates")
|
notifications.error("Error fetching email templates")
|
||||||
}
|
}
|
||||||
|
|
|
@ -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()
|
|
|
@ -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()
|
Loading…
Reference in New Issue