website refactor
This commit is contained in:
@@ -6,8 +6,7 @@
|
||||
*/
|
||||
|
||||
import { useState, useCallback, useEffect, FormEvent, ChangeEvent, Dispatch, SetStateAction } from 'react';
|
||||
import { parseApiError, formatValidationErrorsForForm, logErrorWithContext, createErrorContext } from '@/lib/utils/errorUtils';
|
||||
import { ApiError } from '@/lib/api/base/ApiError';
|
||||
import { parseApiError, formatValidationErrorsForForm, logErrorWithContext } from '@/lib/utils/errorUtils';
|
||||
|
||||
export interface FormField<T> {
|
||||
value: T;
|
||||
@@ -16,7 +15,7 @@ export interface FormField<T> {
|
||||
validating: boolean;
|
||||
}
|
||||
|
||||
export interface FormState<T extends Record<string, any>> {
|
||||
export interface FormState<T extends Record<string, unknown>> {
|
||||
fields: { [K in keyof T]: FormField<T[K]> };
|
||||
isValid: boolean;
|
||||
isSubmitting: boolean;
|
||||
@@ -24,7 +23,7 @@ export interface FormState<T extends Record<string, any>> {
|
||||
submitCount: number;
|
||||
}
|
||||
|
||||
export interface FormOptions<T extends Record<string, any>> {
|
||||
export interface FormOptions<T extends Record<string, unknown>> {
|
||||
initialValues: T;
|
||||
validate?: (values: T) => Record<string, string> | Promise<Record<string, string>>;
|
||||
onSubmit: (values: T) => Promise<void>;
|
||||
@@ -33,7 +32,7 @@ export interface FormOptions<T extends Record<string, any>> {
|
||||
component?: string;
|
||||
}
|
||||
|
||||
export interface UseEnhancedFormReturn<T extends Record<string, any>> {
|
||||
export interface UseEnhancedFormReturn<T extends Record<string, unknown>> {
|
||||
formState: FormState<T>;
|
||||
setFormState: Dispatch<SetStateAction<FormState<T>>>;
|
||||
handleChange: (e: ChangeEvent<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement>) => void;
|
||||
@@ -49,7 +48,7 @@ export interface UseEnhancedFormReturn<T extends Record<string, any>> {
|
||||
/**
|
||||
* Enhanced form hook with comprehensive error handling
|
||||
*/
|
||||
export function useEnhancedForm<T extends Record<string, any>>(
|
||||
export function useEnhancedForm<T extends Record<string, unknown>>(
|
||||
options: FormOptions<T>
|
||||
): UseEnhancedFormReturn<T> {
|
||||
const [formState, setFormState] = useState<FormState<T>>(() => ({
|
||||
@@ -68,6 +67,13 @@ export function useEnhancedForm<T extends Record<string, any>>(
|
||||
submitCount: 0,
|
||||
}));
|
||||
|
||||
const getValues = useCallback((): T => {
|
||||
return Object.keys(formState.fields).reduce((acc, key) => ({
|
||||
...acc,
|
||||
[key]: formState.fields[key as keyof T].value,
|
||||
}), {} as T);
|
||||
}, [formState.fields]);
|
||||
|
||||
// Validate form on change
|
||||
useEffect(() => {
|
||||
if (options.validate && formState.submitCount > 0) {
|
||||
@@ -91,18 +97,11 @@ export function useEnhancedForm<T extends Record<string, any>>(
|
||||
};
|
||||
validateAsync();
|
||||
}
|
||||
}, [formState.fields, formState.submitCount, options.validate]);
|
||||
|
||||
const getValues = useCallback((): T => {
|
||||
return Object.keys(formState.fields).reduce((acc, key) => ({
|
||||
...acc,
|
||||
[key]: formState.fields[key as keyof T].value,
|
||||
}), {} as T);
|
||||
}, [formState.fields]);
|
||||
}, [formState.fields, formState.submitCount, options.validate, getValues]);
|
||||
|
||||
const handleChange = useCallback((e: ChangeEvent<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement>) => {
|
||||
const { name, value, type } = e.target;
|
||||
const checked = 'checked' in e.target ? e.target.checked : false;
|
||||
const checked = 'checked' in e.target ? (e.target as HTMLInputElement).checked : false;
|
||||
const fieldValue = type === 'checkbox' ? checked : value;
|
||||
|
||||
setFormState(prev => ({
|
||||
@@ -267,7 +266,7 @@ export function useEnhancedForm<T extends Record<string, any>>(
|
||||
}
|
||||
} catch (validationError) {
|
||||
logErrorWithContext(validationError, {
|
||||
timestamp: new Date().toISOString(),
|
||||
timestamp: new globalThis.Date().toISOString(),
|
||||
component: options.component || 'useEnhancedForm',
|
||||
action: 'validate',
|
||||
formData: values,
|
||||
@@ -298,7 +297,7 @@ export function useEnhancedForm<T extends Record<string, any>>(
|
||||
|
||||
// Log for developers
|
||||
logErrorWithContext(error, {
|
||||
timestamp: new Date().toISOString(),
|
||||
timestamp: new globalThis.Date().toISOString(),
|
||||
component: options.component || 'useEnhancedForm',
|
||||
action: 'submit',
|
||||
formData: values,
|
||||
|
||||
Reference in New Issue
Block a user