Refactorings, error handling and logout
This commit is contained in:
@@ -40,15 +40,16 @@ export interface DashboardStats {
|
||||
*/
|
||||
export function useDashboardStats(): DashboardStats {
|
||||
// Fetch activity stats (last 30 days by default)
|
||||
const {data: activityStats, error: activityError} = useSWR<ActivityStats>('/activity/stats');
|
||||
const {data: activityStats, error: activityError, isLoading: isLoadingActivity} = useSWR<ActivityStats>('/activity/stats');
|
||||
|
||||
// Fetch contacts (only need the total count)
|
||||
const {data: contactsData, error: contactsError} = useSWR<ContactsResponse>('/contacts?limit=1');
|
||||
const {data: contactsData, error: contactsError, isLoading: isLoadingContacts} = useSWR<ContactsResponse>('/contacts?limit=1');
|
||||
|
||||
// Fetch campaigns (only need the total count)
|
||||
const {data: campaignsData, error: campaignsError} = useSWR<CampaignsResponse>('/campaigns?pageSize=1');
|
||||
const {data: campaignsData, error: campaignsError, isLoading: isLoadingCampaigns} = useSWR<CampaignsResponse>('/campaigns?pageSize=1');
|
||||
|
||||
const isLoading = !activityStats && !contactsData && !campaignsData;
|
||||
// Still loading if ANY of the requests are still in progress
|
||||
const isLoading = isLoadingActivity || isLoadingContacts || isLoadingCampaigns;
|
||||
const error = activityError || contactsError || campaignsError;
|
||||
|
||||
return {
|
||||
|
||||
@@ -14,6 +14,11 @@ interface TypedSchema extends ZodSchema {
|
||||
|
||||
interface ApiResponse {
|
||||
message?: string;
|
||||
error?: {
|
||||
message?: string;
|
||||
code?: string;
|
||||
[key: string]: unknown;
|
||||
};
|
||||
|
||||
[key: string]: unknown;
|
||||
}
|
||||
@@ -52,7 +57,9 @@ export class network {
|
||||
const res = (await response.json()) as ApiResponse;
|
||||
|
||||
if (response.status >= 400) {
|
||||
throw new Error(res.message ?? 'Something went wrong!');
|
||||
// Extract error message from standardized error response or fall back to direct message property
|
||||
const errorMessage = res.error?.message ?? res.message ?? 'Something went wrong!';
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
|
||||
return res as T;
|
||||
@@ -84,7 +91,9 @@ export class network {
|
||||
const res = (await response.json()) as ApiResponse;
|
||||
|
||||
if (response.status >= 400) {
|
||||
throw new Error(res.message ?? 'Something went wrong!');
|
||||
// Extract error message from standardized error response or fall back to direct message property
|
||||
const errorMessage = res.error?.message ?? res.message ?? 'Something went wrong!';
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
|
||||
return res as T;
|
||||
|
||||
Reference in New Issue
Block a user