settings.tsx 2.6 KB

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