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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | 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 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 1x 1x 1x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 4x 4x 4x 4x 14x 14x 14x 14x 14x 14x 14x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 1x 3x 3x 4x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 4x 1x 1x 1x 1x 1x 1x 1x 4x 4x 2x 2x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x | /**
* @file useDeleteSupplierForm.ts
* @module dialogs/DeleteSupplierDialog/useDeleteSupplierForm
*
* @summary
* Hook for supplier deletion workflow.
* Handles selection, confirmation, and deletion submission.
*
* @enterprise
* - Focuses on deletion-specific logic only
* - Delegates search to useSupplierSearch hook
* - Business rule enforcement (admin-only, linked items check)
* - Server error mapping with intelligent heuristics
*/
import * as React from 'react';
import { useTranslation } from 'react-i18next';
import { useAuth } from '../../../../hooks/useAuth';
import { deleteSupplier } from '../../../../api/suppliers';
import { useSupplierSearch } from './useSupplierSearch';
import type { SupplierRow } from '../../../../api/suppliers/types';
/**
* Hook return type for delete supplier workflow.
*
* @interface UseDeleteSupplierFormReturn
*/
export interface UseDeleteSupplierFormReturn {
// Search state (delegated to useSupplierSearch)
searchQuery: string;
setSearchQuery: (query: string) => void;
searchResults: SupplierRow[];
searchLoading: boolean;
handleSearchQueryChange: (query: string) => Promise<void>;
// Selection state
selectedSupplier: SupplierRow | null;
setSelectedSupplier: (supplier: SupplierRow | null) => void;
handleSelectSupplier: (supplier: SupplierRow) => void;
// Confirmation state
showConfirmation: boolean;
setShowConfirmation: (show: boolean) => void;
isDeleting: boolean;
error: string | null;
setError: (error: string | null) => void;
// Actions
handleConfirmDelete: () => Promise<void>;
resetForm: () => void;
}
/**
* Hook for supplier deletion workflow.
*
* Manages:
* - Selection and confirmation state
* - Deletion submission
* - Error mapping and display
* - Authorization checks
*
* Uses useSupplierSearch for search functionality.
*
* @param onDeleted - Callback when supplier is successfully deleted
* @returns Workflow state and handlers
*/
export const useDeleteSupplierForm = (
onDeleted: () => void
): UseDeleteSupplierFormReturn => {
const { t } = useTranslation(['common', 'suppliers', 'errors']);
const { user } = useAuth();
// Search functionality
const search = useSupplierSearch();
// Selection state
const [selectedSupplier, setSelectedSupplier] = React.useState<SupplierRow | null>(null);
// Confirmation state
const [showConfirmation, setShowConfirmation] = React.useState(false);
const [isDeleting, setIsDeleting] = React.useState(false);
const [error, setError] = React.useState<string | null>(null);
/**
* Handle supplier selection from search results.
* Clears search and shows confirmation step.
*/
const handleSelectSupplier = React.useCallback((supplier: SupplierRow) => {
setSelectedSupplier(supplier);
search.resetSearch();
setError(null);
setShowConfirmation(true);
}, [search]);
/**
* Handle delete confirmation and submission.
* Checks authorization, sends DELETE request, maps errors.
*/
const handleConfirmDelete = React.useCallback(async () => {
const mapServerError = (errorMsg?: string | null): string => {
if (!errorMsg) {
return t('errors:supplier.requests.failedToDeleteSupplier', 'Failed to delete supplier. Please try again.');
}
const msg = errorMsg.toLowerCase();
// Linked items error (409 Conflict)
if (
msg.includes('cannot delete supplier with linked items') ||
msg.includes('linked items') ||
msg.includes('cannot delete supplier') ||
msg.includes('verknüpften') // German: "linked"
) {
return t(
'errors:supplier.businessRules.cannotDeleteWithItems',
'This supplier cannot be deleted because there are still items with stock > 0. Please reduce the stock to 0 before deleting the supplier.'
);
}
// Admin-only error (403 Forbidden)
if (msg.includes('403') || msg.includes('forbidden')) {
return t('errors:supplier.adminOnly', 'Only administrators can perform this action.');
}
// Not found error (404)
if (msg.includes('404') || msg.includes('not found')) {
return t('errors:supplier.businessRules.supplierNotFound', 'Supplier not found');
}
// Generic error
return t('errors:supplier.requests.failedToDeleteSupplier', 'Failed to delete supplier. Please try again.');
};
if (!selectedSupplier) {
setError(t('errors:supplier.selection.noSupplierSelected', 'Please select a supplier'));
return;
}
// Check authorization
if (user?.role !== 'ADMIN') {
setError(t('errors:supplier.adminOnly', 'Only administrators can perform this action.'));
return;
}
setIsDeleting(true);
setError(null);
try {
const response = await deleteSupplier(selectedSupplier.id);
if (response.success) {
// Success: notify parent
onDeleted();
} else {
// Map and display server error
const errorMessage = mapServerError(response.error);
setError(errorMessage);
}
} catch (err) {
const errorMessage = mapServerError(err instanceof Error ? err.message : undefined);
setError(errorMessage);
console.error('Delete failed:', err);
} finally {
setIsDeleting(false);
}
}, [selectedSupplier, user?.role, t, onDeleted]);
/**
* Reset form to initial state.
*/
const resetForm = React.useCallback(() => {
setSelectedSupplier(null);
search.resetSearch();
setError(null);
setShowConfirmation(false);
}, [search]);
return {
// Search state (delegated)
searchQuery: search.searchQuery,
setSearchQuery: search.setSearchQuery,
searchResults: search.searchResults,
searchLoading: search.searchLoading,
handleSearchQueryChange: search.handleSearchQueryChange,
// Selection state
selectedSupplier,
setSelectedSupplier,
handleSelectSupplier,
// Confirmation state
showConfirmation,
setShowConfirmation,
isDeleting,
error,
setError,
// Actions
handleConfirmDelete,
resetForm,
};
};
|