UserEditForm.tsx 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { SyntheticEvent } from "react"
  2. import { useMutation } from "@apollo/client"
  3. import { useFormHandler, TextInput } from "../form/forms"
  4. import { CURRENT_USER, USER_EDIT } from "./graphql"
  5. import { UserProps } from "./props"
  6. import { userValidation } from "./validation"
  7. const initialData = {
  8. name: '',
  9. email: '',
  10. password: '',
  11. passwordAgain: ''
  12. }
  13. const UserEditForm = ({ user }: UserProps) => {
  14. const [updateUser, userEdit] = useMutation(USER_EDIT)
  15. const { inputProps, values } = useFormHandler({ ...initialData, ...user })
  16. return (
  17. <form onSubmit={async (event: SyntheticEvent) => {
  18. event.preventDefault()
  19. await updateUser({ variables: values, refetchQueries: [{ query: CURRENT_USER }] })
  20. }}>
  21. <TextInput label='Name' {...inputProps('name')} />
  22. <TextInput label='Email' {...inputProps('email')} />
  23. <TextInput label='Password' {...inputProps('password')} />
  24. <TextInput label='Repeat password' {...inputProps('passwordAgain')} />
  25. <button type='submit' disabled={userEdit.loading}>Update</button>
  26. </form>
  27. )
  28. }
  29. export default UserEditForm