settings.tsx 2.7 KB

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