Improve the frontend and repository layer
This commit is contained in:
@@ -280,9 +280,7 @@ export const usePOSTransaction = (
|
||||
tenant_id: string;
|
||||
transaction_id: string;
|
||||
},
|
||||
options?: Omit<UseQueryOptions<{
|
||||
transaction: POSTransaction;
|
||||
}, ApiError>, 'queryKey' | 'queryFn'>
|
||||
options?: Omit<UseQueryOptions<POSTransaction, ApiError>, 'queryKey' | 'queryFn'>
|
||||
) => {
|
||||
return useQuery({
|
||||
queryKey: posKeys.transaction(params.tenant_id, params.transaction_id),
|
||||
@@ -293,6 +291,40 @@ export const usePOSTransaction = (
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Get POS transactions dashboard summary
|
||||
*/
|
||||
export const usePOSTransactionsDashboard = (
|
||||
params: {
|
||||
tenant_id: string;
|
||||
},
|
||||
options?: Omit<UseQueryOptions<{
|
||||
total_transactions_today: number;
|
||||
total_transactions_this_week: number;
|
||||
total_transactions_this_month: number;
|
||||
revenue_today: number;
|
||||
revenue_this_week: number;
|
||||
revenue_this_month: number;
|
||||
average_transaction_value: number;
|
||||
status_breakdown: Record<string, number>;
|
||||
payment_method_breakdown: Record<string, number>;
|
||||
sync_status: {
|
||||
synced: number;
|
||||
pending: number;
|
||||
failed: number;
|
||||
last_sync_at?: string;
|
||||
};
|
||||
}, ApiError>, 'queryKey' | 'queryFn'>
|
||||
) => {
|
||||
return useQuery({
|
||||
queryKey: [...posKeys.transactions(), 'dashboard', params.tenant_id],
|
||||
queryFn: () => posService.getPOSTransactionsDashboard(params),
|
||||
enabled: !!params.tenant_id,
|
||||
staleTime: 30 * 1000, // 30 seconds
|
||||
...options,
|
||||
});
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// SYNC OPERATIONS
|
||||
// ============================================================================
|
||||
|
||||
140
frontend/src/api/hooks/settings.ts
Normal file
140
frontend/src/api/hooks/settings.ts
Normal file
@@ -0,0 +1,140 @@
|
||||
// frontend/src/api/hooks/settings.ts
|
||||
/**
|
||||
* React Query hooks for Tenant Settings
|
||||
* Provides data fetching, caching, and mutation hooks
|
||||
*/
|
||||
|
||||
import { useQuery, useMutation, useQueryClient, UseQueryOptions } from '@tanstack/react-query';
|
||||
import { settingsApi } from '../services/settings';
|
||||
import { useToast } from '../../hooks/ui/useToast';
|
||||
import type {
|
||||
TenantSettings,
|
||||
TenantSettingsUpdate,
|
||||
SettingsCategory,
|
||||
CategoryResetResponse,
|
||||
} from '../types/settings';
|
||||
|
||||
// Query keys
|
||||
export const settingsKeys = {
|
||||
all: ['settings'] as const,
|
||||
tenant: (tenantId: string) => ['settings', tenantId] as const,
|
||||
category: (tenantId: string, category: SettingsCategory) =>
|
||||
['settings', tenantId, category] as const,
|
||||
};
|
||||
|
||||
/**
|
||||
* Hook to fetch all settings for a tenant
|
||||
*/
|
||||
export const useSettings = (
|
||||
tenantId: string,
|
||||
options?: Omit<UseQueryOptions<TenantSettings, Error>, 'queryKey' | 'queryFn'>
|
||||
) => {
|
||||
return useQuery<TenantSettings, Error>({
|
||||
queryKey: settingsKeys.tenant(tenantId),
|
||||
queryFn: () => settingsApi.getSettings(tenantId),
|
||||
staleTime: 5 * 60 * 1000, // 5 minutes
|
||||
...options,
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Hook to fetch settings for a specific category
|
||||
*/
|
||||
export const useCategorySettings = (
|
||||
tenantId: string,
|
||||
category: SettingsCategory,
|
||||
options?: Omit<UseQueryOptions<Record<string, any>, Error>, 'queryKey' | 'queryFn'>
|
||||
) => {
|
||||
return useQuery<Record<string, any>, Error>({
|
||||
queryKey: settingsKeys.category(tenantId, category),
|
||||
queryFn: () => settingsApi.getCategorySettings(tenantId, category),
|
||||
staleTime: 5 * 60 * 1000, // 5 minutes
|
||||
...options,
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Hook to update tenant settings
|
||||
*/
|
||||
export const useUpdateSettings = () => {
|
||||
const queryClient = useQueryClient();
|
||||
const { addToast } = useToast();
|
||||
|
||||
return useMutation<
|
||||
TenantSettings,
|
||||
Error,
|
||||
{ tenantId: string; updates: TenantSettingsUpdate }
|
||||
>({
|
||||
mutationFn: ({ tenantId, updates }) => settingsApi.updateSettings(tenantId, updates),
|
||||
onSuccess: (data, variables) => {
|
||||
// Invalidate all settings queries for this tenant
|
||||
queryClient.invalidateQueries({ queryKey: settingsKeys.tenant(variables.tenantId) });
|
||||
addToast('Ajustes guardados correctamente', { type: 'success' });
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error('Failed to update settings:', error);
|
||||
addToast('Error al guardar los ajustes', { type: 'error' });
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Hook to update a specific category
|
||||
*/
|
||||
export const useUpdateCategorySettings = () => {
|
||||
const queryClient = useQueryClient();
|
||||
const { addToast } = useToast();
|
||||
|
||||
return useMutation<
|
||||
TenantSettings,
|
||||
Error,
|
||||
{ tenantId: string; category: SettingsCategory; settings: Record<string, any> }
|
||||
>({
|
||||
mutationFn: ({ tenantId, category, settings }) =>
|
||||
settingsApi.updateCategorySettings(tenantId, category, settings),
|
||||
onSuccess: (data, variables) => {
|
||||
// Invalidate all settings queries for this tenant
|
||||
queryClient.invalidateQueries({ queryKey: settingsKeys.tenant(variables.tenantId) });
|
||||
// Also invalidate the specific category query
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: settingsKeys.category(variables.tenantId, variables.category),
|
||||
});
|
||||
addToast('Ajustes de categoría guardados correctamente', { type: 'success' });
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error('Failed to update category settings:', error);
|
||||
addToast('Error al guardar los ajustes de categoría', { type: 'error' });
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Hook to reset a category to defaults
|
||||
*/
|
||||
export const useResetCategory = () => {
|
||||
const queryClient = useQueryClient();
|
||||
const { addToast } = useToast();
|
||||
|
||||
return useMutation<
|
||||
CategoryResetResponse,
|
||||
Error,
|
||||
{ tenantId: string; category: SettingsCategory }
|
||||
>({
|
||||
mutationFn: ({ tenantId, category }) => settingsApi.resetCategory(tenantId, category),
|
||||
onSuccess: (data, variables) => {
|
||||
// Invalidate all settings queries for this tenant
|
||||
queryClient.invalidateQueries({ queryKey: settingsKeys.tenant(variables.tenantId) });
|
||||
// Also invalidate the specific category query
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: settingsKeys.category(variables.tenantId, variables.category),
|
||||
});
|
||||
addToast(`Categoría '${variables.category}' restablecida a valores predeterminados`, {
|
||||
type: 'success',
|
||||
});
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error('Failed to reset category:', error);
|
||||
addToast('Error al restablecer la categoría', { type: 'error' });
|
||||
},
|
||||
});
|
||||
};
|
||||
123
frontend/src/api/hooks/sustainability.ts
Normal file
123
frontend/src/api/hooks/sustainability.ts
Normal file
@@ -0,0 +1,123 @@
|
||||
/**
|
||||
* React Query hooks for Sustainability API
|
||||
*/
|
||||
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import {
|
||||
getSustainabilityMetrics,
|
||||
getSustainabilityWidgetData,
|
||||
getSDGCompliance,
|
||||
getEnvironmentalImpact,
|
||||
exportGrantReport
|
||||
} from '../services/sustainability';
|
||||
import type {
|
||||
SustainabilityMetrics,
|
||||
SustainabilityWidgetData,
|
||||
SDGCompliance,
|
||||
EnvironmentalImpact,
|
||||
GrantReport
|
||||
} from '../types/sustainability';
|
||||
|
||||
// Query keys
|
||||
export const sustainabilityKeys = {
|
||||
all: ['sustainability'] as const,
|
||||
metrics: (tenantId: string, startDate?: string, endDate?: string) =>
|
||||
['sustainability', 'metrics', tenantId, startDate, endDate] as const,
|
||||
widget: (tenantId: string, days: number) =>
|
||||
['sustainability', 'widget', tenantId, days] as const,
|
||||
sdg: (tenantId: string) =>
|
||||
['sustainability', 'sdg', tenantId] as const,
|
||||
environmental: (tenantId: string, days: number) =>
|
||||
['sustainability', 'environmental', tenantId, days] as const,
|
||||
};
|
||||
|
||||
/**
|
||||
* Hook to get comprehensive sustainability metrics
|
||||
*/
|
||||
export function useSustainabilityMetrics(
|
||||
tenantId: string,
|
||||
startDate?: string,
|
||||
endDate?: string,
|
||||
options?: { enabled?: boolean }
|
||||
) {
|
||||
return useQuery({
|
||||
queryKey: sustainabilityKeys.metrics(tenantId, startDate, endDate),
|
||||
queryFn: () => getSustainabilityMetrics(tenantId, startDate, endDate),
|
||||
enabled: options?.enabled !== false && !!tenantId,
|
||||
staleTime: 5 * 60 * 1000, // 5 minutes
|
||||
refetchInterval: 10 * 60 * 1000, // Refetch every 10 minutes
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to get sustainability widget data (simplified metrics)
|
||||
*/
|
||||
export function useSustainabilityWidget(
|
||||
tenantId: string,
|
||||
days: number = 30,
|
||||
options?: { enabled?: boolean }
|
||||
) {
|
||||
return useQuery({
|
||||
queryKey: sustainabilityKeys.widget(tenantId, days),
|
||||
queryFn: () => getSustainabilityWidgetData(tenantId, days),
|
||||
enabled: options?.enabled !== false && !!tenantId,
|
||||
staleTime: 5 * 60 * 1000, // 5 minutes
|
||||
refetchInterval: 10 * 60 * 1000, // Refetch every 10 minutes
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to get SDG 12.3 compliance status
|
||||
*/
|
||||
export function useSDGCompliance(
|
||||
tenantId: string,
|
||||
options?: { enabled?: boolean }
|
||||
) {
|
||||
return useQuery({
|
||||
queryKey: sustainabilityKeys.sdg(tenantId),
|
||||
queryFn: () => getSDGCompliance(tenantId),
|
||||
enabled: options?.enabled !== false && !!tenantId,
|
||||
staleTime: 10 * 60 * 1000, // 10 minutes
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to get environmental impact data
|
||||
*/
|
||||
export function useEnvironmentalImpact(
|
||||
tenantId: string,
|
||||
days: number = 30,
|
||||
options?: { enabled?: boolean }
|
||||
) {
|
||||
return useQuery({
|
||||
queryKey: sustainabilityKeys.environmental(tenantId, days),
|
||||
queryFn: () => getEnvironmentalImpact(tenantId, days),
|
||||
enabled: options?.enabled !== false && !!tenantId,
|
||||
staleTime: 5 * 60 * 1000, // 5 minutes
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to export grant report
|
||||
*/
|
||||
export function useExportGrantReport() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: ({
|
||||
tenantId,
|
||||
grantType,
|
||||
startDate,
|
||||
endDate
|
||||
}: {
|
||||
tenantId: string;
|
||||
grantType?: string;
|
||||
startDate?: string;
|
||||
endDate?: string;
|
||||
}) => exportGrantReport(tenantId, grantType, startDate, endDate),
|
||||
onSuccess: () => {
|
||||
// Optionally invalidate related queries
|
||||
queryClient.invalidateQueries({ queryKey: sustainabilityKeys.all });
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user