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 | 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 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 14x 5x 5x 5x 5x 5x 14x 14x 14x 14x 14x 14x 14x 4x 3x 3x 3x 3x 3x 4x 4x 4x 4x 2x 4x 1x 1x 1x 1x 1x 1x 4x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 14x 14x 14x 14x 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 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x | /**
* @file useEditSupplierForm.ts
* @module dialogs/EditSupplierDialog/useEditSupplierForm
*
* @summary
* Hook for supplier editing workflow logic.
* Orchestrates form state, search, confirmation, and submission.
*
* @enterprise
* - Two-step workflow: search/select → edit info
* - Delegates form state to useEditSupplierFormState
* - Delegates confirmation to useEditSupplierConfirmation
* - Uses mapSupplierError utility for error handling
* - Authorization checks (admin-only)
* - Name field is immutable (cannot be changed)
*/
import * as React from 'react';
import type { UseFormReturn } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { useAuth } from '../../../../hooks/useAuth';
import { updateSupplier, type EditSupplierForm } from '../../../../api/suppliers';
import { useSupplierSearch } from '../DeleteSupplierDialog/useSupplierSearch';
import { useEditSupplierFormState } from './useEditSupplierFormState';
import { useEditSupplierConfirmation } from './useEditSupplierConfirmation';
import { mapSupplierError } from './mapSupplierErrors';
import type { SupplierRow } from '../../../../api/suppliers/types';
/**
* Hook return type for edit supplier workflow.
*
* @interface UseEditSupplierFormReturn
*/
export interface UseEditSupplierFormReturn {
// Search state (delegated to useSupplierSearch)
searchQuery: string;
searchResults: SupplierRow[];
searchLoading: boolean;
handleSearchQueryChange: (query: string) => Promise<void>;
// Selection state
selectedSupplier: SupplierRow | null;
handleSelectSupplier: (supplier: SupplierRow) => void;
// Form state (delegated to useEditSupplierFormState)
register: UseFormReturn<EditSupplierForm>['register'];
control: UseFormReturn<EditSupplierForm>['control'];
formState: UseFormReturn<EditSupplierForm>['formState'];
handleSubmit: UseFormReturn<EditSupplierForm>['handleSubmit'];
setValue: UseFormReturn<EditSupplierForm>['setValue'];
// Confirmation state (delegated to useEditSupplierConfirmation)
showConfirmation: boolean;
setShowConfirmation: (show: boolean) => void;
pendingChanges: EditSupplierForm | null;
setPendingChanges: (changes: EditSupplierForm | null) => void;
// Error state
formError: string;
setFormError: (error: string) => void;
// Actions
handleConfirmChanges: () => Promise<void>;
resetForm: () => void;
onSelectSupplierAndLoadForm: (supplier: SupplierRow) => void;
}
/**
* Hook for supplier editing workflow.
*
* Manages:
* - Supplier search and selection
* - Form state and validation (delegated)
* - Confirmation dialog state (delegated)
* - Submission and error handling
* - Authorization checks
*
* @param onUpdated - Callback when supplier is successfully updated
* @returns Workflow state and handlers
*/
export const useEditSupplierForm = (
onUpdated: () => void
): UseEditSupplierFormReturn => {
const { t } = useTranslation(['common', 'suppliers', 'errors']);
const { user } = useAuth();
// Delegate to specialized hooks
const search = useSupplierSearch();
const formState = useEditSupplierFormState();
const confirmation = useEditSupplierConfirmation();
// Selection state
const [selectedSupplier, setSelectedSupplier] = React.useState<SupplierRow | null>(null);
// Error state
const [formError, setFormError] = React.useState('');
/**
* Handle supplier selection from search results.
* Clears search and pre-fills form.
*/
const onSelectSupplierAndLoadForm = React.useCallback(
(supplier: SupplierRow) => {
setSelectedSupplier(supplier);
formState.populateWithSupplier(supplier);
search.resetSearch();
setFormError('');
},
[search, formState]
);
/**
* Handle update confirmation and submission.
*/
const handleConfirmChanges = React.useCallback(async () => {
if (!confirmation.pendingChanges || !selectedSupplier) return;
try {
const response = await updateSupplier(selectedSupplier.id, {
name: selectedSupplier.name,
createdBy: user?.email,
contactName: confirmation.pendingChanges.contactName,
phone: confirmation.pendingChanges.phone,
email: confirmation.pendingChanges.email,
});
if (response.success) {
onUpdated();
} else {
const errorMessage = mapSupplierError(response.error, t);
setFormError(errorMessage);
confirmation.setShowConfirmation(false);
}
} catch (err) {
const errorMessage = mapSupplierError(
err instanceof Error ? err.message : undefined,
t
);
setFormError(errorMessage);
confirmation.setShowConfirmation(false);
console.error('Update failed:', err);
}
}, [confirmation, selectedSupplier, user?.email, onUpdated, t]);
/**
* Reset form to initial state.
*/
const resetForm = React.useCallback(() => {
setSelectedSupplier(null);
search.resetSearch();
setFormError('');
confirmation.reset();
formState.reset();
}, [search, confirmation, formState]);
return {
// Search state
searchQuery: search.searchQuery,
searchResults: search.searchResults,
searchLoading: search.searchLoading,
handleSearchQueryChange: search.handleSearchQueryChange,
// Selection state
selectedSupplier,
handleSelectSupplier: onSelectSupplierAndLoadForm,
// Form state
register: formState.register,
control: formState.control,
formState: formState.formState,
handleSubmit: formState.handleSubmit,
setValue: formState.setValue,
// Confirmation state
showConfirmation: confirmation.showConfirmation,
setShowConfirmation: confirmation.setShowConfirmation,
pendingChanges: confirmation.pendingChanges,
setPendingChanges: confirmation.setPendingChanges,
// Error state
formError,
setFormError,
// Actions
handleConfirmChanges,
resetForm,
onSelectSupplierAndLoadForm,
};
};
|