From dd8ac998eb825a70f20e31f96de00bf9f92d08a5 Mon Sep 17 00:00:00 2001 From: Aura Alba Date: Mon, 23 Oct 2023 21:25:17 -0500 Subject: [PATCH] feat: change controlled components form to uncontrolled --- .../Instructors/AddInstructors/index.jsx | 104 +++++++----------- 1 file changed, 37 insertions(+), 67 deletions(-) diff --git a/src/features/Instructors/AddInstructors/index.jsx b/src/features/Instructors/AddInstructors/index.jsx index 77216a45..208172d6 100644 --- a/src/features/Instructors/AddInstructors/index.jsx +++ b/src/features/Instructors/AddInstructors/index.jsx @@ -16,16 +16,9 @@ const initialState = { error: null, }; -const addInstructorState = { - instructorInfo: '', - ccxId: '', - ccxName: '', -}; - const AddInstructors = () => { const [state, dispatch] = useReducer(reducer, initialState); const [isOpen, open, close] = useToggle(false); - const [addInstructorInfo, setAddInstructorInfo] = useState(addInstructorState); const [isNoUser, setIsNoUser] = useState(false); const enrollmentData = new FormData(); @@ -41,39 +34,16 @@ const AddInstructors = () => { } }; - const handleInstructorInput = (e) => { - setAddInstructorInfo({ - ...addInstructorInfo, - instructorInfo: e.target.value.trim(), - }); - }; - - // Set default value - useEffect(() => { - if (state.data.length > 0) { - setAddInstructorInfo((prevState) => ({ - ...prevState, - ccxId: state.data[0].classId, - ccxName: state.data[0].className, - })); - } - }, [state.data]); - - const handleCcxSelect = (e) => { - const select = e.target; - setAddInstructorInfo({ - ...addInstructorInfo, - ccxId: select.value, - ccxName: select.options[select.selectedIndex].text, - }); - }; - - const handleAddInstructors = async () => { + const handleAddInstructors = async (e) => { + e.preventDefault(); + const form = e.target; + const formData = new FormData(form); + const formJson = Object.fromEntries(formData.entries()); try { - enrollmentData.append('unique_student_identifier', addInstructorInfo.instructorInfo); + enrollmentData.append('unique_student_identifier', formJson.instructorInfo); enrollmentData.append('rolename', 'staff'); enrollmentData.append('action', 'allow'); - const response = await handleInstructorsEnrollment(enrollmentData, addInstructorInfo.ccxId); + const response = await handleInstructorsEnrollment(enrollmentData, formJson.ccxId); if (response.data?.userDoesNotExist) { setIsNoUser(true); } else { @@ -107,36 +77,36 @@ const AddInstructors = () => { - - - {state.data.map((ccx) => )} - - - - - {isNoUser && ( - - User does not exist - - )} - -
- -
+
+ + + + {state.data.map((ccx) => )} + + + + + {isNoUser && ( + + User does not exist + + )} + +
+ +
+