Skip to content

Commit

Permalink
feat(templateStore): add saved templates retrieval and enhance error …
Browse files Browse the repository at this point in the history
…handling

- Add `savedTemplates` method to fetch saved email templates.
- Improve error handling and messages.
- Change `deleteTemplate` to use template ID instead of the entire object.
- Ensure consistent formatting and code style.
  • Loading branch information
0xdevcollins committed Nov 30, 2024
1 parent 0e55d06 commit a40436f
Showing 1 changed file with 67 additions and 48 deletions.
115 changes: 67 additions & 48 deletions store/templateStore.ts
Original file line number Diff line number Diff line change
@@ -1,89 +1,108 @@
import apiService from '@/lib/api-service';
import { TemplateTypes } from '@/types/interface';
import { create } from 'zustand';
import apiService from "@/lib/api-service";
import { TemplateTypes } from "@/types/interface";
import { create } from "zustand";

interface TemplateState {
templates: TemplateTypes[] | null;
loading: boolean;
error: string | null;
listAllTemplates: () => Promise<void>;
savedTemplates: () => Promise<void>;
saveTemplate: (data: TemplateTypes) => Promise<void>;
updateTemplate: (data: TemplateTypes) => Promise<void>;
deleteTemplate: (data: TemplateTypes) => Promise<void>;
deleteTemplate: (id: string) => Promise<void>;
}

export const useTemplateStore = create<TemplateState>((set, get) => ({
templates: null,
loading: false,
loading: false,
error: null,

listAllTemplates: async () => {
set({ loading: true, error: null });
try {
set({ loading: true, error: null });
const response = await apiService.listAllEmailTemplate({}); // Assuming this API fetches templates
set({ templates: response.data.templates, loading: false }); // Store templates in state
const response = await apiService.listAllEmailTemplate();

if (!response.data || !Array.isArray(response.data.templates)) {
throw new Error("Invalid response format from the server.");
}

set({ templates: response.data.templates, loading: false });
} catch (error) {
console.error('Failed to load templates:', error);
set({ loading: false, error: 'Failed to load templates' }); // Set error state if failed
console.error("Error fetching templates:", error);
set({ loading: false, error: "Unable to fetch templates. Please try again later." });
}
},
},
savedTemplates: async () => {
set({ loading: true, error: null });
try {
const response = await apiService.savedEmailTemplates();

if (!response.data || !Array.isArray(response.data.templates)) {
throw new Error("Invalid response format from the server.");
}

set({ templates: response.data.templates, loading: false });
} catch (error) {
console.error("Error fetching templates:", error);
set({ loading: false, error: "Unable to fetch templates. Please try again later." });
}
},

// Save a new email template
saveTemplate: async (data: TemplateTypes) => {
set({ loading: true, error: null });
try {
set({ loading: true, error: null }); // Set loading state to true
const response = await apiService.saveEmailTemplate(data); // Save the template
const currentTemplates = get().templates;

// Add the new template to the existing templates array
set({
templates: currentTemplates ? [...currentTemplates, response.data.template] : [response.data.template],
loading: false,
});
const response = await apiService.saveEmailTemplate(data);
const newTemplate = response.data.template;

if (!newTemplate) {
throw new Error("Invalid response from the server.");
}

const currentTemplates = get().templates || [];
set({ templates: [...currentTemplates, newTemplate], loading: false });
} catch (error) {
console.error('Failed to save template:', error);
set({ loading: false, error: 'Failed to save template' }); // Set error state if failed
console.error("Error saving template:", error);
set({ loading: false, error: "Unable to save template. Please try again later." });
}
},

// Update an existing email template
updateTemplate: async (data: TemplateTypes) => {
set({ loading: true, error: null });
try {
set({ loading: true, error: null }); // Set loading state to true
const response = await apiService.updateEmailTemplate(data); // Update the template
const currentTemplates = get().templates;
const response = await apiService.updateEmailTemplate(data);
const updatedTemplate = response.data.template;

// Update the template in the existing templates list
if (currentTemplates) {
const updatedTemplates = currentTemplates.map(template =>
template.id === response.data.template.id ? response.data.template : template
);
set({ templates: updatedTemplates, loading: false });
} else {
set({ templates: [response.data.template], loading: false });
if (!updatedTemplate) {
throw new Error("Invalid response from the server.");
}

const currentTemplates = get().templates || [];
const updatedTemplates = currentTemplates.map((template) =>
template.id === updatedTemplate.id ? updatedTemplate : template
);

set({ templates: updatedTemplates, loading: false });
} catch (error) {
console.error('Failed to update template:', error);
set({ loading: false, error: 'Failed to update template' }); // Set error state if failed
console.error("Error updating template:", error);
set({ loading: false, error: "Unable to update template. Please try again later." });
}
},

// Delete an email template
deleteTemplate: async (data: TemplateTypes) => {
deleteTemplate: async (id: string) => {
set({ loading: true, error: null });
try {
set({ loading: true, error: null }); // Set loading state to true
await apiService.deleteEmailTemplate(data); // Delete the template
const currentTemplates = get().templates;
await apiService.deleteEmailTemplate({ id });
const currentTemplates = get().templates || [];
const updatedTemplates = currentTemplates.filter((template) => template.id !== id);

// Remove the deleted template from the state
if (currentTemplates) {
const updatedTemplates = currentTemplates.filter(
template => template.id !== data.id
);
set({ templates: updatedTemplates, loading: false });
}
set({ templates: updatedTemplates, loading: false });
} catch (error) {
console.error('Failed to delete template:', error);
set({ loading: false, error: 'Failed to delete template' }); // Set error state if failed
console.error("Error deleting template:", error);
set({ loading: false, error: "Unable to delete template. Please try again later." });
}
},
}));

0 comments on commit a40436f

Please sign in to comment.