| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- /**
- * store/templates.js
- * Pinia store — Email Templates domain
- */
- import { defineStore } from 'pinia'
- import { ref, computed } from 'vue'
- import api from '@/api/index.js'
- export const useTemplatesStore = defineStore('templates', () => {
- // ── State ────────────────────────────────────────────────────────────────
- const templates = ref([])
- const loading = ref(false)
- const error = ref('')
- // ── Getters ──────────────────────────────────────────────────────────────
- const count = computed(() => templates.value.length)
- const notificationTemplates = computed(() => templates.value.filter(t => t.template_type === 'Notification'))
- const overdueTemplates = computed(() => templates.value.filter(t => t.template_type === 'Overdue'))
- function nameById(id) {
- return templates.value.find(t => t.id === id)?.name ?? `#${id}`
- }
- // ── Actions ──────────────────────────────────────────────────────────────
- async function fetchAll() {
- loading.value = true
- error.value = ''
- try {
- const { data } = await api.get('/templates')
- templates.value = data
- } catch (e) {
- error.value = e.response?.data?.detail ?? 'Failed to load email templates.'
- } finally {
- loading.value = false
- }
- }
- async function create(payload) {
- const { data } = await api.post('/templates', payload)
- templates.value.push(data)
- return data
- }
- async function update(id, payload) {
- const { data } = await api.put(`/templates/${id}`, payload)
- const idx = templates.value.findIndex(t => t.id === id)
- if (idx !== -1) templates.value[idx] = data
- return data
- }
- async function remove(id) {
- await api.delete(`/templates/${id}`)
- templates.value = templates.value.filter(t => t.id !== id)
- }
- return {
- templates, loading, error, count,
- notificationTemplates, overdueTemplates,
- nameById,
- fetchAll, create, update, remove,
- }
- })
|