page.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. 'use client';
  2. import './style.scss';
  3. import Link from 'next/link';
  4. import { useState, useCallback, useRef } from 'react';
  5. import { useConfigContext } from '@/contexts/configProvider';
  6. import { useMemberContext } from '@/contexts/memberProvider';
  7. import useErrorAlert from '@/hooks/useErrorAlert';
  8. import { ChangeSummaryRequest } from '@/types/request/account';
  9. import { fetchApi, throwError } from '@/lib/utils/client';
  10. import Loading from '@/app/component/Loading';
  11. export default function ChangeSummary()
  12. {
  13. const config = useConfigContext();
  14. const { member, setMember } = useMemberContext();
  15. const { setError } = useErrorAlert();
  16. const [loading, setLoading] = useState<boolean>(false);
  17. const [newSummary, setNewSummary] = useState<string|null>('');
  18. const newSummaryRef = useRef<HTMLInputElement>(null);
  19. const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
  20. e.preventDefault();
  21. if (!member) {
  22. return;
  23. }
  24. if (!newSummary) {
  25. newSummaryRef.current?.focus();
  26. return setError('한마디를 입력하세요.');
  27. }
  28. setLoading(true);
  29. fetchApi('/api/mypage/summary', {
  30. method: 'POST',
  31. body: { Summary: newSummary } as ChangeSummaryRequest
  32. }).then((res) => {
  33. throwError(res);
  34. member.summary = newSummary;
  35. setMember(member);
  36. localStorage.setItem('member', JSON.stringify(member));
  37. alert('한마디가 변경되었습니다.');
  38. }).catch(err => {
  39. setError(err.message);
  40. }).finally(() => {
  41. setLoading(false);
  42. setNewSummary('');
  43. });
  44. }
  45. const handleChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
  46. setNewSummary(e.target.value.trim());
  47. }, []);
  48. const handleDelete = async () => {
  49. if (confirm("한마디를 삭제하시겠습니까?")) {
  50. if (!member || !member.summary) {
  51. return;
  52. }
  53. setLoading(true);
  54. fetchApi('/api/mypage/summary', { method: 'DELETE' }).then((res) => {
  55. throwError(res);
  56. member.summary = null;
  57. setMember(member);
  58. localStorage.setItem('member', JSON.stringify(member));
  59. alert("한마디를 삭제되었습니다.");
  60. }).catch(err => {
  61. setError(err.message);
  62. }).finally(() => {
  63. setLoading(false);
  64. setNewSummary('');
  65. });
  66. }
  67. };
  68. return (
  69. <>
  70. <div id='changeSummary'>
  71. { loading && <Loading /> }
  72. <h1>한마디 변경</h1>
  73. <form method='post' acceptCharset='utf-8' autoComplete='off' onSubmit={handleSubmit}>
  74. <table className='table-auto max-xl:w-full lg:w-[600px]'>
  75. <caption>
  76. 커뮤니티 프로필에 표시되는 한마디를 설정할 수 있습니다.
  77. </caption>
  78. <colgroup>
  79. <col width='30%'/>
  80. <col width='60%'/>
  81. <col width='10%'/>
  82. </colgroup>
  83. <tbody>
  84. <tr>
  85. <th>현재 한마디</th>
  86. <td>{member?.summary || '-'}</td>
  87. <td>&nbsp;</td>
  88. </tr>
  89. <tr>
  90. <th>새 한마디</th>
  91. <td>
  92. <input type='text' name='new_summary' id='newSummary' ref={newSummaryRef} value={newSummary ?? ''} placeholder='변경할 한마디' maxLength={50} autoFocus autoComplete='off' onChange={handleChange} />
  93. </td>
  94. <td>&nbsp;</td>
  95. </tr>
  96. </tbody>
  97. <tfoot>
  98. <tr>
  99. <td colSpan={3}>
  100. <div className='flex justify-center gap-2'>
  101. <button type='submit' className='btn btn-submit'>확인</button>
  102. { member?.summary && (
  103. <button type='button' className='btn btn-delete' onClick={handleDelete}>삭제</button>
  104. )}
  105. <Link href='/profile' className='btn btn-default'>취소</Link>
  106. </div>
  107. </td>
  108. </tr>
  109. </tfoot>
  110. </table>
  111. </form>
  112. <br />
  113. <dl className='max-xl:w-full lg:w-[600px]'>
  114. <dt>등록할 수 없는 한마디</dt>
  115. <dd>
  116. <ol>
  117. <li>한마디는 최대 50자 이내로 입력 가능합니다.</li>
  118. <li>부적절한 내용은 별도의 고지 없이 변경될 수 있습니다.</li>
  119. {config.account.changeSummaryDay > 0 && <li>자기소개개 변경 주기는 {config.account.changeSummaryDay}일입니다.</li>}
  120. </ol>
  121. </dd>
  122. </dl>
  123. </div>
  124. </>
  125. );
  126. }