page.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 } 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(() => {
  33. member.summary = newSummary;
  34. setMember(member);
  35. localStorage.setItem('member', JSON.stringify(member));
  36. alert('한마디가 변경되었습니다.');
  37. }).catch(err => {
  38. setError(err.message);
  39. }).finally(() => {
  40. setLoading(false);
  41. setNewSummary('');
  42. });
  43. }
  44. const handleChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
  45. setNewSummary(e.target.value.trim());
  46. }, []);
  47. const handleDelete = async () => {
  48. if (confirm("한마디를 삭제하시겠습니까?")) {
  49. if (!member || !member.summary) {
  50. return;
  51. }
  52. setLoading(true);
  53. fetchApi('/api/mypage/summary', { method: 'DELETE' }).then(() => {
  54. member.summary = null;
  55. setMember(member);
  56. localStorage.setItem('member', JSON.stringify(member));
  57. alert("한마디를 삭제되었습니다.");
  58. }).catch(err => {
  59. setError(err.message);
  60. }).finally(() => {
  61. setLoading(false);
  62. setNewSummary('');
  63. });
  64. }
  65. };
  66. return (
  67. <>
  68. <div id='changeSummary'>
  69. { loading && <Loading /> }
  70. <h1>한마디 변경</h1>
  71. <form method='post' acceptCharset='utf-8' autoComplete='off' onSubmit={handleSubmit}>
  72. <table className='table-auto max-xl:w-full lg:w-[600px]'>
  73. <caption>
  74. 커뮤니티 프로필에 표시되는 한마디를 설정할 수 있습니다.
  75. </caption>
  76. <colgroup>
  77. <col width='30%'/>
  78. <col width='60%'/>
  79. <col width='10%'/>
  80. </colgroup>
  81. <tbody>
  82. <tr>
  83. <th>현재 한마디</th>
  84. <td>{member?.summary || '-'}</td>
  85. <td>&nbsp;</td>
  86. </tr>
  87. <tr>
  88. <th>새 한마디</th>
  89. <td>
  90. <input type='text' name='new_summary' id='newSummary' ref={newSummaryRef} value={newSummary ?? ''} placeholder='변경할 한마디' maxLength={50} autoFocus autoComplete='off' onChange={handleChange} />
  91. </td>
  92. <td>&nbsp;</td>
  93. </tr>
  94. </tbody>
  95. <tfoot>
  96. <tr>
  97. <td colSpan={3}>
  98. <div className='flex justify-center gap-2'>
  99. <button type='submit' className='btn btn-submit'>확인</button>
  100. { member?.summary && (
  101. <button type='button' className='btn btn-delete' onClick={handleDelete}>삭제</button>
  102. )}
  103. <Link href='/profile' className='btn btn-default'>취소</Link>
  104. </div>
  105. </td>
  106. </tr>
  107. </tfoot>
  108. </table>
  109. </form>
  110. <br />
  111. <dl className='max-xl:w-full lg:w-[600px]'>
  112. <dt>등록할 수 없는 한마디</dt>
  113. <dd>
  114. <ol>
  115. <li>한마디는 최대 50자 이내로 입력 가능합니다.</li>
  116. <li>부적절한 내용은 별도의 고지 없이 변경될 수 있습니다.</li>
  117. {(config.account.changeSummaryDay ?? 0) > 0 && <li>자기소개개 변경 주기는 {config.account.changeSummaryDay}일입니다.</li>}
  118. </ol>
  119. </dd>
  120. </dl>
  121. </div>
  122. </>
  123. );
  124. }