settings.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { NextPage } from "next/types";
  2. import { useEffect } from "react";
  3. import FormResponse from "../components/FormResponse";
  4. import Layout from "../components/Layout";
  5. import Spinner from "../components/Spinner";
  6. import { useAuth } from "../context/UserContext";
  7. import useForm from "../hooks/useForm";
  8. import api from "../utils/api";
  9. const Settings: NextPage = () => {
  10. const { updateUser, user } = useAuth();
  11. const { error, submitHandler, success, loading } = useForm(api.updateUser);
  12. useEffect(() => {
  13. if (success) {
  14. updateUser(success.result);
  15. }
  16. }, [success, updateUser]);
  17. return user ? (
  18. <Layout>
  19. <form className="form" onSubmit={submitHandler}>
  20. <div className="form-group">
  21. <label htmlFor="name" className="form-label">
  22. Name
  23. </label>
  24. <input
  25. type="text"
  26. name="name"
  27. id="name"
  28. className="form-input"
  29. defaultValue={user.name}
  30. required
  31. />
  32. </div>
  33. <div className="form-group">
  34. <label htmlFor="email" className="form-label">
  35. Email
  36. </label>
  37. <input
  38. type="email"
  39. name="email"
  40. id="email"
  41. className="form-input"
  42. defaultValue={user.email}
  43. required
  44. />
  45. </div>
  46. <div className="form-group">
  47. <label htmlFor="password" className="form-label">
  48. Password
  49. </label>
  50. <input
  51. type="password"
  52. id="password"
  53. name="password"
  54. className="form-input"
  55. defaultValue={user.password}
  56. minLength={6}
  57. required
  58. />
  59. </div>
  60. <div className="form-group">
  61. <label htmlFor="team" className="form-label">
  62. Team
  63. </label>
  64. <select
  65. className="form-input"
  66. id="team"
  67. name="team"
  68. defaultValue={user.team}
  69. required
  70. >
  71. <option value="Admins">Admins</option>
  72. <option value="Users">Users</option>
  73. <option value="Viewers">Viewers</option>
  74. </select>
  75. </div>
  76. <input type="hidden" id="id" name="id" value={user.id} />
  77. <button type="submit" className="form-button" disabled={loading}>
  78. {loading ? <Spinner /> : "Save"}
  79. </button>
  80. <FormResponse
  81. error={error}
  82. success={success}
  83. loading={loading}
  84. fallbackSuccessMessage="Profile updated correctly"
  85. />
  86. </form>
  87. </Layout>
  88. ) : null;
  89. };
  90. export default Settings;