page.tsx 4.1 KB

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