Feat: added tab dashboard under General Settings (System Setting and Mail Settings). Improved the Setting API to dynamically use the baseURL.
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
|
||||
import { pb } from "@/lib/pocketbase";
|
||||
|
||||
export interface GeneralSettings {
|
||||
id: string;
|
||||
created: string;
|
||||
updated: string;
|
||||
system_name: string;
|
||||
id?: string;
|
||||
created?: string;
|
||||
updated?: string;
|
||||
system_name?: string;
|
||||
system_name_kh?: string;
|
||||
logo_url?: string;
|
||||
system_description?: string;
|
||||
@@ -47,22 +45,55 @@ export interface GeneralSettings {
|
||||
export const settingsService = {
|
||||
async getGeneralSettings(): Promise<GeneralSettings | null> {
|
||||
try {
|
||||
const result = await pb.collection('general_settings').getList(1, 1);
|
||||
if (result.items.length > 0) {
|
||||
// Type cast to GeneralSettings to resolve the type mismatch
|
||||
return result.items[0] as unknown as GeneralSettings;
|
||||
console.log('Fetching settings from /api/settings endpoint...');
|
||||
const response = await fetch('/api/settings', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ action: 'getSettings' })
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
return null;
|
||||
|
||||
const result = await response.json();
|
||||
console.log('Settings API response:', result);
|
||||
|
||||
return result.success ? result.data : null;
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch general settings:", error);
|
||||
return null;
|
||||
}
|
||||
},
|
||||
|
||||
async updateGeneralSettings(id: string, data: Partial<GeneralSettings>): Promise<GeneralSettings | null> {
|
||||
async updateGeneralSettings(data: Partial<GeneralSettings>): Promise<GeneralSettings | null> {
|
||||
try {
|
||||
// Type cast to GeneralSettings to resolve the type mismatch
|
||||
return await pb.collection('general_settings').update(id, data) as unknown as GeneralSettings;
|
||||
console.log('Updating settings via /api/settings:', data);
|
||||
|
||||
// Remove id and timestamp fields for settings update
|
||||
const { id, created, updated, ...updateData } = data;
|
||||
|
||||
const response = await fetch('/api/settings', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
action: 'updateSettings',
|
||||
data: updateData
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
const result = await response.json();
|
||||
console.log('Settings update response:', result);
|
||||
|
||||
return result.success ? result.data : null;
|
||||
} catch (error) {
|
||||
console.error("Failed to update general settings:", error);
|
||||
return null;
|
||||
@@ -71,9 +102,24 @@ export const settingsService = {
|
||||
|
||||
async testEmailConnection(smtpConfig: any): Promise<boolean> {
|
||||
try {
|
||||
// In a real application, we would send a test email here
|
||||
// For now, let's simulate a successful connection if the host is provided
|
||||
return !!smtpConfig.host;
|
||||
console.log('Testing email connection via /api/settings:', smtpConfig);
|
||||
const response = await fetch('/api/settings', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
action: 'testEmailConnection',
|
||||
data: smtpConfig
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
const result = await response.json();
|
||||
return result.success || false;
|
||||
} catch (error) {
|
||||
console.error("Failed to test email connection:", error);
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user