source: frontend/src/Components/useFormNested.js@ ac19a0c

Last change on this file since ac19a0c was ac19a0c, checked in by darsov2 <62809499+darsov2@…>, 10 months ago

authContext impl, admin panel impl, search bar fixes, reservations listings impl

  • Property mode set to 100644
File size: 1.2 KB
RevLine 
[e9b4ba9]1import {useState} from 'react';
2
3const useFormNested = (editData) => {
4 const [formData, setData] = useState({...editData});
5
6 const onFormChange = (e) => {
7 console.log(e)
8
[ac19a0c]9 const dependantRoutes = e.target.name === 'freeSpace' ? e.target.parentElement.getAttribute('dependantRoutes').split(',') : undefined;
[e9b4ba9]10 console.log(dependantRoutes)
11
12 let updatedData = {
13 ...formData,
14 [e.target.parentElement.attributes.routeId.value]: {
15 ...formData[e.target.parentElement.attributes.routeId.value],
16 [e.target.name]: e.target.value
17 },
18 }
19
20 if (e.target.name === 'freeSpace')
21 dependantRoutes.forEach(x => {
22 updatedData = {
23 ...updatedData,
24 [x]: {
25 ...formData[x],
26 [e.target.name]: e.target.value
27 },
28 }
29 })
30 setData(updatedData);
31 console.log(updatedData)
32 };
33
34 const setFormData = (newData) => {
35 setData({...newData});
36 }
37 return {
38 formData,
39 onFormChange,
40 setFormData
41 };
42};
43
44export default useFormNested;
Note: See TracBrowser for help on using the repository browser.