feat(alertas): add alerts CRUD with stats and management UI
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
61
apps/web/lib/hooks/use-alertas.ts
Normal file
61
apps/web/lib/hooks/use-alertas.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import * as alertasApi from '../api/alertas';
|
||||
import type { AlertaCreate, AlertaUpdate } from '@horux/shared';
|
||||
|
||||
export function useAlertas(filters?: { leida?: boolean; resuelta?: boolean }) {
|
||||
return useQuery({
|
||||
queryKey: ['alertas', filters],
|
||||
queryFn: () => alertasApi.getAlertas(filters),
|
||||
});
|
||||
}
|
||||
|
||||
export function useAlertasStats() {
|
||||
return useQuery({
|
||||
queryKey: ['alertas-stats'],
|
||||
queryFn: alertasApi.getStats,
|
||||
});
|
||||
}
|
||||
|
||||
export function useCreateAlerta() {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (data: AlertaCreate) => alertasApi.createAlerta(data),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['alertas'] });
|
||||
queryClient.invalidateQueries({ queryKey: ['alertas-stats'] });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useUpdateAlerta() {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: ({ id, data }: { id: number; data: AlertaUpdate }) => alertasApi.updateAlerta(id, data),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['alertas'] });
|
||||
queryClient.invalidateQueries({ queryKey: ['alertas-stats'] });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useDeleteAlerta() {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (id: number) => alertasApi.deleteAlerta(id),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['alertas'] });
|
||||
queryClient.invalidateQueries({ queryKey: ['alertas-stats'] });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useMarkAllAsRead() {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: alertasApi.markAllAsRead,
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['alertas'] });
|
||||
queryClient.invalidateQueries({ queryKey: ['alertas-stats'] });
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user