UserAdmin.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { useUsersQuery, UsersQuery, Permission } from '../../graphql'
  2. import { useFormHandler, InputPropsOptions } from '../form/forms'
  3. import { useEffect } from 'react'
  4. interface UserInputProps {
  5. inputProps: (name: string, options?: InputPropsOptions) => any
  6. subtree: string
  7. }
  8. const UserInput = ({ inputProps, subtree }: UserInputProps) => {
  9. console.log('user input', inputProps('name', { subtree }))
  10. return (
  11. <tr>
  12. <td><input type='checkbox' /></td>
  13. <td><input {...inputProps('name', { subtree })} /></td>
  14. <td><input {...inputProps('email', { subtree, label: 'e-mail' })} /></td>
  15. <td><input type='checkbox' {...inputProps('admin')} /></td>
  16. <td><input type='checkbox' {...inputProps('instructor')} /></td>
  17. <td><input {...inputProps('interests', { subtree })} /></td>
  18. <td><button>X</button></td>
  19. </tr>
  20. )
  21. }
  22. const initialData: UsersQuery = {
  23. users: []
  24. }
  25. const UserAdmin = () => {
  26. const { data, error, loading } = useUsersQuery()
  27. const { inputProps, values, setValues } = useFormHandler(initialData)
  28. useEffect(() => {
  29. if (data) console.log('data', { ...data })
  30. if (data && data.users) setValues({ ...data })
  31. }, [data])
  32. if (error) return <p>Error: {error.message}</p>
  33. if (loading) return <p>Loading...</p>
  34. return (
  35. <form>
  36. <table>
  37. <thead>
  38. <tr>
  39. <th></th>
  40. <th>Name</th>
  41. <th>Email</th>
  42. <th>Admin</th>
  43. <th>Instructor</th>
  44. <th>Interests</th>
  45. <th>Actions</th>
  46. </tr>
  47. </thead>
  48. <tbody>
  49. {
  50. values.users.map((user, index) => (
  51. user && <UserInput key={user.id} inputProps={inputProps} subtree={`users.${index}`} />
  52. ))
  53. }
  54. </tbody>
  55. </table>
  56. </form>
  57. )
  58. }
  59. export default UserAdmin