page.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 } 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(() => {
  35. setComplete(true);
  36. }).catch(err => {
  37. setError(err.message);
  38. }).finally(() => {
  39. setLoading(false);
  40. });
  41. }
  42. const handleChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
  43. setNewEmail(e.target.value.trim());
  44. }, []);
  45. const refresh = () => location.reload();
  46. return (
  47. <>
  48. <div id="changeEmail">
  49. { loading && <Loading /> }
  50. {!isComplete ?
  51. <>
  52. <h1>이메일 변경</h1>
  53. <form method="post" acceptCharset="utf-8" autoComplete="off" onSubmit={handleSubmit}>
  54. <table className="table-auto max-xl:w-full lg:w-[600px]">
  55. <caption>
  56. 새 이메일 주소를 입력하고 &quot;확인&quot; 버튼을 누릅니다.<br />
  57. 인증 메일이 도착하시면 내용을 확인하신 후 본문에 있는 링크를 클릭해주세요.
  58. </caption>
  59. <colgroup>
  60. <col width="30%"/>
  61. <col width="60%"/>
  62. <col width="10%"/>
  63. </colgroup>
  64. <tbody>
  65. <tr>
  66. <th>현재 이메일</th>
  67. <td>{member?.email}</td>
  68. <td>&nbsp;</td>
  69. </tr>
  70. <tr>
  71. <th>새 이메일</th>
  72. <td>
  73. <input type="email" name="new_email" id="newEmail" ref={newEmailRef} value={newEmail} placeholder="변경할 이메일 주소" maxLength={60} autoFocus autoComplete="off" onChange={handleChange} />
  74. </td>
  75. <td>&nbsp;</td>
  76. </tr>
  77. </tbody>
  78. <tfoot>
  79. <tr>
  80. <td colSpan={3}>
  81. <div className="flex justify-center gap-2">
  82. <button type="submit" className="btn btn-submit">확인</button>
  83. <Link href="/profile" className="btn btn-default">취소</Link>
  84. </div>
  85. </td>
  86. </tr>
  87. </tfoot>
  88. </table>
  89. </form>
  90. <br />
  91. <dl className="max-xl:w-full lg:w-[600px]">
  92. <dt>등록할 수 없는 이메일 주소</dt>
  93. <dd>
  94. <ol>
  95. <li>전 세계 인터넷 통신 표준 RFC(Request for Comments)를 준수하지 않는 전자 메일 주소</li>
  96. <li>회사에서 지정한 사용할 수 없는 문자(공백 및 더블바이트 문자)</li>
  97. <li>전자 메일 계정 부분에서 반자 영숫자, 반자 기호 _(밑줄), . (점) 및 -(하이픈) 이외의 문자가 사용됩니다.</li>
  98. {(config.account.changeEmailDay ?? 0) > 0 && <li>이메일 변경 주기는 {config.account.changeEmailDay}일입니다.</li>}
  99. </ol>
  100. </dd>
  101. </dl>
  102. </>
  103. :
  104. <>
  105. <h1>인증 이메일 발송</h1>
  106. <blockquote>
  107. <strong>{newEmail} 으로 인증 이메일이 발송되었습니다.</strong><br />
  108. 메일이 도착하면 내용을 확인하신 후 본문에 있는 링크를 클릭해 주세요.<br />
  109. 몇 분 이내에 메일이 도착하지 않는 경우 등록 된 메일 주소 및 수신 거부 설정을 확인한 후
  110. 처음부터 다시 시도해 주십시오.
  111. </blockquote>
  112. <br />
  113. <button type="button" className="btn btn-default" onClick={refresh}>다시 시도하기</button>
  114. </>
  115. }
  116. </div>
  117. </>
  118. );
  119. }