|  | 
| 1 |  | -import React, { useState } from "react"; | 
|  | 1 | +import React, { useState, useEffect } from "react"; | 
| 2 | 2 | import styled from "styled-components"; | 
| 3 | 3 | import { Checkbox, Button } from "@kleros/ui-components-library"; | 
| 4 | 4 | import FormEmail from "./FormEmail"; | 
|  | 5 | +import { useAccount } from "wagmi"; | 
|  | 6 | +import { useRelationalDB } from "hooks/useRelationalDB"; | 
|  | 7 | + | 
|  | 8 | +interface UserData { | 
|  | 9 | +  email: string; | 
|  | 10 | +  options: string; | 
|  | 11 | +  walletaddress: string; | 
|  | 12 | +} | 
| 5 | 13 | 
 | 
| 6 | 14 | const FormContainer = styled.div` | 
| 7 | 15 |   position: relative; | 
| @@ -38,19 +46,48 @@ const FormNotifs: React.FC = () => { | 
| 38 | 46 |   const [checkboxStates, setCheckboxStates] = useState<boolean[]>(new Array(OPTIONS.length).fill(false)); | 
| 39 | 47 |   const [emailInput, setEmailInput] = useState<string>(""); | 
| 40 | 48 |   const [emailIsValid, setEmailIsValid] = useState<boolean>(false); | 
|  | 49 | +  const [userExists, setUserExists] = useState<boolean>(false); // TODO: use this to determine whether to use createUser or updateUser | 
|  | 50 | +  const { address } = useAccount(); | 
|  | 51 | +  const { createUser, getUser, updateUser } = useRelationalDB(); | 
| 41 | 52 | 
 | 
| 42 | 53 |   const handleCheckboxChange = (index: number) => (e: React.ChangeEvent<HTMLInputElement>) => { | 
| 43 | 54 |     const newCheckboxStates = [...checkboxStates]; | 
| 44 | 55 |     newCheckboxStates[index] = e.target.checked; | 
| 45 | 56 |     setCheckboxStates(newCheckboxStates); | 
| 46 | 57 |   }; | 
| 47 | 58 | 
 | 
|  | 59 | +  useEffect(() => { | 
|  | 60 | +    const fetchUser = async () => { | 
|  | 61 | +      const data = await getUser(address + ""); | 
|  | 62 | +      if (data[0]) { | 
|  | 63 | +        setUserExists(true); | 
|  | 64 | +        setEmailInput(data[0].email); | 
|  | 65 | +        setCheckboxStates( | 
|  | 66 | +          OPTIONS.map(({ label }) => { | 
|  | 67 | +            return data[0].options[label]; | 
|  | 68 | +          }) | 
|  | 69 | +        ); | 
|  | 70 | +      } | 
|  | 71 | +    }; | 
|  | 72 | +    fetchUser(); | 
|  | 73 | +  }, []); | 
|  | 74 | + | 
| 48 | 75 |   const handleClick = async () => { | 
| 49 | 76 |     const optionsObject: object = {}; | 
| 50 | 77 |     OPTIONS.forEach(({ label }, index: number) => { | 
| 51 | 78 |       optionsObject[label] = checkboxStates[index]; | 
| 52 | 79 |     }); | 
| 53 | 80 |     const jsonData = JSON.stringify(optionsObject, null, 2); | 
|  | 81 | +    const userData: UserData = { | 
|  | 82 | +      email: emailInput, | 
|  | 83 | +      options: jsonData, | 
|  | 84 | +      walletaddress: address + "", | 
|  | 85 | +    }; | 
|  | 86 | +    if (userExists) { | 
|  | 87 | +      await updateUser(address + "", userData); | 
|  | 88 | +    } else { | 
|  | 89 | +      await createUser(userData); | 
|  | 90 | +    } | 
| 54 | 91 |   }; | 
| 55 | 92 | 
 | 
| 56 | 93 |   return ( | 
|  | 
0 commit comments