page.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. 'use client';
  2. import './style.scss';
  3. import Link from 'next/link';
  4. import { useState, useCallback, useRef } from 'react';
  5. import { useMemberContext } from '@/contexts/memberProvider';
  6. import useErrorAlert from '@/hooks/useErrorAlert';
  7. import { useConfigContext } from '@/contexts/configProvider';
  8. import { ChangeEmailRequest } from '@/types/request/account';
  9. import { fetchApi, throwError } from '@/lib/utils/client';
  10. import Loading from '@/app/component/Loading';
  11. export default function ChangeEmail()
  12. {
  13. const config = useConfigContext();
  14. const { member } = useMemberContext();
  15. const { setError } = useErrorAlert();
  16. const [isComplete, setComplete] = useState<boolean>(false);
  17. const [loading, setLoading] = useState<boolean>(false);
  18. const [newEmail, setNewEmail] = useState<string>('');
  19. const newEmailRef = useRef<HTMLInputElement>(null);
  20. // 이메일 변경 요청
  21. const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
  22. e.preventDefault();
  23. if (!member) {
  24. return;
  25. }
  26. if (!newEmail) {
  27. newEmailRef.current?.focus();
  28. return setError('변경하실 이메일을 입력하세요.');
  29. }
  30. setLoading(true);
  31. fetchApi('/api/mypage/email', {
  32. method: 'POST',
  33. body: { NewEmail: newEmail } as ChangeEmailRequest
  34. }).then((res) => {
  35. throwError(res);
  36. setComplete(true);
  37. }).catch(err => {
  38. setError(err.message);
  39. }).finally(() => {
  40. setLoading(false);
  41. });
  42. }
  43. const handleChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
  44. setNewEmail(e.target.value.trim());
  45. }, []);
  46. const refresh = () => location.reload();
  47. return (
  48. <>
  49. <div id="changeEmail">
  50. { loading && <Loading /> }
  51. {!isComplete ?
  52. <>
  53. <h1>이메일 변경</h1>
  54. <form method="post" acceptCharset="utf-8" autoComplete="off" onSubmit={handleSubmit}>
  55. <table className="table-auto max-xl:w-full lg:w-[600px]">
  56. <caption>
  57. 새 이메일 주소를 입력하고 &quot;확인&quot; 버튼을 누릅니다.<br />
  58. 인증 메일이 도착하시면 내용을 확인하신 후 본문에 있는 링크를 클릭해주세요.
  59. </caption>
  60. <colgroup>
  61. <col width="30%"/>
  62. <col width="60%"/>
  63. <col width="10%"/>
  64. </colgroup>
  65. <tbody>
  66. <tr>
  67. <th>현재 이메일</th>
  68. <td>{member?.email}</td>
  69. <td>&nbsp;</td>
  70. </tr>
  71. <tr>
  72. <th>새 이메일</th>
  73. <td>
  74. <input type="email" name="new_email" id="newEmail" ref={newEmailRef} value={newEmail} placeholder="변경할 이메일 주소" maxLength={60} autoFocus autoComplete="off" onChange={handleChange} />
  75. </td>
  76. <td>&nbsp;</td>
  77. </tr>
  78. </tbody>
  79. <tfoot>
  80. <tr>
  81. <td colSpan={3}>
  82. <div className="flex justify-center gap-2">
  83. <button type="submit" className="btn btn-submit">확인</button>
  84. <Link href="/profile" className="btn btn-default">취소</Link>
  85. </div>
  86. </td>
  87. </tr>
  88. </tfoot>
  89. </table>
  90. </form>
  91. <br />
  92. <dl className="max-xl:w-full lg:w-[600px]">
  93. <dt>등록할 수 없는 이메일 주소</dt>
  94. <dd>
  95. <ol>
  96. <li>전 세계 인터넷 통신 표준 RFC(Request for Comments)를 준수하지 않는 전자 메일 주소</li>
  97. <li>회사에서 지정한 사용할 수 없는 문자(공백 및 더블바이트 문자)</li>
  98. <li>전자 메일 계정 부분에서 반자 영숫자, 반자 기호 _(밑줄), . (점) 및 -(하이픈) 이외의 문자가 사용됩니다.</li>
  99. {config.account.changeEmailDay > 0 && <li>이메일 변경 주기는 {config.account.changeEmailDay}일입니다.</li>}
  100. </ol>
  101. </dd>
  102. </dl>
  103. </>
  104. :
  105. <>
  106. <h1>인증 이메일 발송</h1>
  107. <blockquote>
  108. <strong>{newEmail} 으로 인증 이메일이 발송되었습니다.</strong><br />
  109. 메일이 도착하면 내용을 확인하신 후 본문에 있는 링크를 클릭해 주세요.<br />
  110. 몇 분 이내에 메일이 도착하지 않는 경우 등록 된 메일 주소 및 수신 거부 설정을 확인한 후
  111. 처음부터 다시 시도해 주십시오.
  112. </blockquote>
  113. <br />
  114. <button className="btn btn-default" onClick={refresh}>다시 시도하기</button>
  115. </>
  116. }
  117. </div>
  118. </>
  119. );
  120. }