-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GEN-1822]: create a "useGenericForm" hook for form data & error mana…
…gement (#1891) This pull request introduces a new custom hook, `useGenericForm`, and refactors several existing hooks to utilize it for managing form state and errors. The main changes include the creation of the `useGenericForm` hook, updates to existing hooks to use this new hook, and relevant import adjustments. ### Introduction of `useGenericForm` hook: * [`frontend/webapp/hooks/common/useGenericForm.ts`](diffhunk://#diff-33107d1d28564b132423ff2c52fc5c1863e97ff8322a634b67aaee63647f387bR1-R44): Added a new custom hook `useGenericForm` to handle form state and errors generically. ### Refactoring existing hooks to use `useGenericForm`: * [`frontend/webapp/hooks/actions/useActionFormData.ts`](diffhunk://#diff-034891eac1659418ed1ceccc6d28858abafa1f6f5da688a2550123de5ee99841L1-R3): Refactored to use `useGenericForm` for managing form state and errors, replacing local state management. [[1]](diffhunk://#diff-034891eac1659418ed1ceccc6d28858abafa1f6f5da688a2550123de5ee99841L1-R3) [[2]](diffhunk://#diff-034891eac1659418ed1ceccc6d28858abafa1f6f5da688a2550123de5ee99841L19-R18) [[3]](diffhunk://#diff-034891eac1659418ed1ceccc6d28858abafa1f6f5da688a2550123de5ee99841L63-R48) [[4]](diffhunk://#diff-034891eac1659418ed1ceccc6d28858abafa1f6f5da688a2550123de5ee99841L101-R86) * [`frontend/webapp/hooks/destinations/useDestinationFormData.ts`](diffhunk://#diff-74e478b5171cbecdf7abf376e9bc1428c38d585bc6252dc463216f9869bf9f77L5-R5): Updated to utilize `useGenericForm` for form state and error handling, removing redundant local state management. [[1]](diffhunk://#diff-74e478b5171cbecdf7abf376e9bc1428c38d585bc6252dc463216f9869bf9f77L5-R5) [[2]](diffhunk://#diff-74e478b5171cbecdf7abf376e9bc1428c38d585bc6252dc463216f9869bf9f77L32-R34) [[3]](diffhunk://#diff-74e478b5171cbecdf7abf376e9bc1428c38d585bc6252dc463216f9869bf9f77L90-L114) [[4]](diffhunk://#diff-74e478b5171cbecdf7abf376e9bc1428c38d585bc6252dc463216f9869bf9f77L134-R108) [[5]](diffhunk://#diff-74e478b5171cbecdf7abf376e9bc1428c38d585bc6252dc463216f9869bf9f77L155-R129) * [`frontend/webapp/hooks/instrumentation-rules/useInstrumentationRuleFormData.ts`](diffhunk://#diff-ac4b5a705bbef372ded4616918885fb1bb8cd2cb8468b7226c89574e709a22f5L1-R3): Modified to adopt `useGenericForm` for form state and error management, simplifying the code. [[1]](diffhunk://#diff-ac4b5a705bbef372ded4616918885fb1bb8cd2cb8468b7226c89574e709a22f5L1-R3) [[2]](diffhunk://#diff-ac4b5a705bbef372ded4616918885fb1bb8cd2cb8468b7226c89574e709a22f5L23-R22) [[3]](diffhunk://#diff-ac4b5a705bbef372ded4616918885fb1bb8cd2cb8468b7226c89574e709a22f5L66-R51) [[4]](diffhunk://#diff-ac4b5a705bbef372ded4616918885fb1bb8cd2cb8468b7226c89574e709a22f5L90-R75) ### Import adjustments: * [`frontend/webapp/hooks/common/index.ts`](diffhunk://#diff-e91dfaad8acf2180d9c3e07b98c4f7d5bdab76542b7ab14d5e84202a42aeba29R2): Exported the new `useGenericForm` hook for use in other parts of the application.
- Loading branch information
1 parent
21098d7
commit e6bcd4c
Showing
6 changed files
with
61 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { useState } from 'react'; | ||
|
||
export const useGenericForm = <Form = Record<string, any>>(initialFormData: Form) => { | ||
const [formData, setFormData] = useState<Form>({ ...initialFormData }); | ||
const [formErrors, setFormErrors] = useState<Partial<Record<keyof Form, string>>>({}); | ||
|
||
const handleFormChange = (key?: keyof typeof formData, val?: any, obj?: typeof formData) => { | ||
if (!!key) { | ||
// this is for cases where the form contains objects such as "exportedSignals", | ||
// the object's child is targeted with a ".dot" for example: "exportedSignals.logs" | ||
|
||
const [parentKey, childKey] = (key as string).split('.'); | ||
|
||
if (!!childKey) { | ||
setFormData((prev) => ({ ...prev, [parentKey]: { ...prev[parentKey], [childKey]: val } })); | ||
} else { | ||
setFormData((prev) => ({ ...prev, [parentKey]: val })); | ||
} | ||
} else if (!!obj) { | ||
setFormData({ ...obj }); | ||
} | ||
}; | ||
|
||
const handleErrorChange = (key?: keyof typeof formErrors, val?: string, obj?: typeof formErrors) => { | ||
if (!!key) { | ||
setFormErrors((prev) => ({ ...prev, [key]: val })); | ||
} else if (!!obj) { | ||
setFormErrors({ ...obj }); | ||
} | ||
}; | ||
|
||
const resetFormData = () => { | ||
setFormData({ ...initialFormData }); | ||
setFormErrors({}); | ||
}; | ||
|
||
return { | ||
formData, | ||
formErrors, | ||
handleFormChange, | ||
handleErrorChange, | ||
resetFormData, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters