settings.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 appearance-none bg-dropdown-arrow bg-no-repeat bg-[right_8px_center]"
  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
  78. type="submit"
  79. className="default-button col-start-2 w-max"
  80. disabled={loading}
  81. >
  82. {loading ? <Spinner /> : "Save"}
  83. </button>
  84. <FormResponse
  85. error={error}
  86. success={success}
  87. loading={loading}
  88. fallbackSuccessMessage="Profile updated correctly"
  89. />
  90. </form>
  91. </Layout>
  92. ) : null;
  93. };
  94. export default Settings;