All files / src/pages/suppliers/handlers useDialogHandlers.ts

100% Statements 63/63
100% Branches 4/4
100% Functions 1/1
100% Lines 63/63

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 641x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 1x 1x 1x 4x 4x 4x 1x 1x 1x 1x 4x 4x 4x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x  
/**
 * @file useDialogHandlers.ts
 * @module pages/suppliers/handlers/useDialogHandlers
 *
 * @summary
 * Custom hook that provides dialog event handlers for SuppliersBoard.
 * Manages: Create, Edit, Delete dialog callbacks with toast notifications and cache invalidation.
 *
 * @enterprise
 * - Separation of concerns: handler logic isolated from component
 * - Centralized CRUD operation callbacks
 * - Toast notifications for user feedback
 * - React Query cache invalidation
 */
 
import { useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import { useQueryClient } from '@tanstack/react-query';
import { useToast } from '../../../context/toast';
import type { UseSuppliersBoardStateReturn } from '../hooks/useSuppliersBoardState';
 
/**
 * Hook providing dialog operation callbacks.
 *
 * @param state - Suppliers board state object from useSuppliersBoardState
 * @returns Object with handler functions for dialog operations
 *
 * @example
 * ```tsx
 * const { handleSupplierCreated, handleSupplierUpdated, handleSupplierDeleted } = useDialogHandlers(state);
 * ```
 */
export function useDialogHandlers(state: UseSuppliersBoardStateReturn) {
  const { t } = useTranslation(['suppliers']);
  const toast = useToast();
  const queryClient = useQueryClient();
 
  const handleSupplierCreated = useCallback(() => {
    toast(t('suppliers:status.created', 'Supplier created successfully'), 'success');
    queryClient.invalidateQueries({ queryKey: ['suppliers'] });
    state.setOpenCreate(false);
  }, [state, toast, t, queryClient]);
 
  const handleSupplierUpdated = useCallback(() => {
    toast(t('suppliers:status.updated', 'Supplier updated successfully'), 'success');
    queryClient.invalidateQueries({ queryKey: ['suppliers'] });
    state.setOpenEdit(false);
    state.setSelectedId(null);
  }, [state, toast, t, queryClient]);
 
  const handleSupplierDeleted = useCallback(() => {
    toast(t('suppliers:status.deleted', 'Supplier deleted successfully'), 'success');
    queryClient.invalidateQueries({ queryKey: ['suppliers'] });
    state.setOpenDelete(false);
    state.setSelectedId(null);
  }, [state, toast, t, queryClient]);
 
  return {
    handleSupplierCreated,
    handleSupplierUpdated,
    handleSupplierDeleted,
  };
}