settings.tsx 2.7 KB

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