Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐Ÿ’ญ ๋ฉ˜ํ† ๋‹˜ ๋ฆฌ๋ทฐ #83

Open
wants to merge 721 commits into
base: review-branch
Choose a base branch
from

Conversation

eunjju2
Copy link
Collaborator

@eunjju2 eunjju2 commented Dec 1, 2024

๐Ÿ› ๏ธ ์„œ๋น„์Šค ์š”์•ฝ

  • ์‚ฌ์šฉ์ž์™€ ์‚ฌ์—…์ž๊ฐ€ ํ•˜๋‚˜์˜ ํ”Œ๋žซํผ์—์„œ ์—ฐ๊ฒฐ๋˜๋Š” ์Šคํ„ฐ๋””๋ฃธ ์˜ˆ์•ฝ ์„œ๋น„์Šค
  • ์‚ฌ์šฉ์ž๋Š” ์œ„์น˜ ๋ฐ ์žฅ์†Œ ๊ฒ€์ƒ‰์„ ํ†ตํ•ด ์Šคํ„ฐ๋””๋ฃธ์„ ์ฐพ๊ณ  ์˜ˆ์•ฝ ๋ฐ ๊ฒฐ์ œ๋ฅผ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์‚ฌ์—…์ž๋Š” ๋ณธ์ธ์˜ ์Šคํ„ฐ๋””๋ฃธ๊ณผ ์˜ˆ์•ฝ์ž๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ“ ์š”๊ตฌ ์‚ฌํ•ญ๊ณผ ๊ตฌํ˜„ ๋‚ด์šฉ

  1. ๋กœ๊ทธ์ธ ๋ฐ ํšŒ์›๊ฐ€์ž…, ์‚ฌ์—…์žฅ ๋“ฑ๋ก ๋ฐ ์Šคํ„ฐ๋””๋ฃธ ์ƒ์„ธ ๋ณด๊ธฐ - ์ตœ์„ฑ๋ น
  2. ์ง€๋„ ๋ฐ ์žฅ์†Œ ๊ฒ€์ƒ‰, ์˜ˆ์•ฝ ๋ฐ ๊ฒฐ์ œ, 1:1 ๋ฌธ์˜ - ์ด์€์ˆ˜
  3. ์‚ฌ์šฉ์ž & ์‚ฌ์—…์ž ํšŒ์› ๊ด€๋ฆฌ, ์‚ฌ์—…์žฅ ๋ฐ ์˜ˆ์•ฝ ๊ด€๋ฆฌ, ์‹ค์‹œ๊ฐ„ ์•Œ๋ฆผ - ์กฐํ˜„์ง„

๐Ÿ“ ์ž‘์—… ํ˜„ํ™ฉ

  • ํ”„๋ก ํŠธ์ธก ํŽ˜์ด์ง€๋ณ„ ๋งˆํฌ์—…์€ ๋งˆ๋ฌด๋ฆฌ๋œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. api ์—ฐ๊ฒฐํ•˜๋ฉด์„œ ์ž์ž˜ํ•œ ๋ถ€๋ถ„๋งŒ ์ˆ˜์ •์ด ์ด๋ฃจ์–ด์ง€๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • ๋ฐฑ์—”๋“œ์ชฝ ์„œ๋ฒ„ ๋ฐฐํฌ๊ฐ€ ๋ผ์„œ ํ˜„์žฌ ๊ฐ€๋Šฅํ•œ api๋ฅผ ์—ฐ๊ฒฐํ•ด์„œ ํ…Œ์ŠคํŠธํ•ด๋ณด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ ์™„๋ฒฝํžˆ ๋ฐฐํฌ๋œ ๊ฒƒ์€ ์•„๋‹ˆ์–ด์„œ ํ…Œ์ŠคํŠธ์— ์‹œ๊ฐ„์ด ์กฐ๊ธˆ ์†Œ์š”๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

    ์ง€๊ธˆ ํ…Œ์ŠคํŠธํ•ด๋ณด๊ณ  ์žˆ๋Š” ํŽ˜์ด์ง€๋Š”

    • ์‚ฌ์—…์ž / ์‚ฌ์šฉ์ž ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€
    • ์‚ฌ์—…์ž / ์‚ฌ์šฉ์ž ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€
    • ์‚ฌ์—…์ž / ์‚ฌ์šฉ์ž ๋งˆ์ดํŽ˜์ด์ง€(๋กœ๊ทธ์•„์›ƒ ํฌํ•จ)
    • ๋ฉ”์ธํŽ˜์ด์ง€
    • ์‚ฌ์—…์žฅ ์ƒ์„ธํŽ˜์ด์ง€

    ์ •๋„์ž…๋‹ˆ๋‹ค. ์ค‘๊ฐ„๋ฐœํ‘œ๋•Œ๊นŒ์ง€ ํ•ด๋‹น ํŽ˜์ด์ง€๋“ค์€ ์ตœ๋Œ€ํ•œ ๊ตฌํ˜„์„ ๋๋‚ผ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.

๐Ÿ’ฌ ๋ฆฌ๋ทฐ ์‹œ์— ์ฐธ๊ณ ์‚ฌํ•ญ

  • ๋กœ์ปฌ ๊ตฌ๋™ ์‹œ npm install ์‹คํ–‰ํ•˜์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • .env ํŒŒ์ผ์— ๊ฐ ํ‚ค๊ฐ’์„ ๋„ฃ์–ด์•ผ ํ•˜๋Š”๋ฐ DM์œผ๋กœ ์š”์ฒญํ•ด์ฃผ์‹œ๋ฉด ๋ฐ”๋กœ ์•Œ๋ ค๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.
  • ์ง€๋„ ์ด์šฉ ์‹œ ๋ธŒ๋ผ์šฐ์ €์—์„œ ํ˜„์žฌ ์œ„์น˜ ์ˆ˜์ง‘์— ๊ด€ํ•œ ๊ถŒํ•œ์ด ๋™์˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ๊ฐ ํŽ˜์ด์ง€ ๊ฒฝ๋กœ๋Š” Rounter.tsx ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๐Ÿ’ฌย ์ž‘์—…๋‚ด์—ญ์— ๋Œ€ํ•œ ์„ค๋ช… & ๊ถ๊ธˆํ•œ ์ 

  • typings ํด๋”์—๋Š” ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ํƒ€์ž…์„ ์ •๋ฆฌํ•ด๋‘๊ณ , constants ํด๋”์—๋Š” ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์—๋Ÿฌ ๋ฌธ๊ตฌ๋‚˜ placeholder๋ฅผ ์ •๋ฆฌํ•ด๋‘์—ˆ์Šต๋‹ˆ๋‹ค.
  • ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉ๋ ๋งŒํ•œ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜๋“ค์€ ๋”ฐ๋กœ utils ํด๋”๋กœ ๋นผ์„œ, ์ค‘๋ณต ์ฝ”๋“œ๋ฅผ ์ค„์ด๊ณ  ์ตœ๋Œ€ํ•œ ๊ฐ™์ด ํ™œ์šฉํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค.
  • api๋ฅผ ๊ฐ ํŽ˜์ด์ง€๋ณ„๋กœ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ๊ธฐ๋Šฅ๋ณ„๋กœ ๋ถ„๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค.
  • axios๋ฅผ ๋ชจ๋“ˆํ™”ํ•ด์„œ, ์ค‘๋ณต ์ฝ”๋“œ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ณ ์ž ํ–ˆ์Šต๋‹ˆ๋‹ค.
    • ์ง€๋‚œ ํ”„๋กœ์ ํŠธ์—์„œ axios ๊ด€๋ จ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ, ๋™์ผํ•œ ์—ญํ• ์„ ํ•˜๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ๊ฐ ๋”ฐ๋กœ ๋งŒ๋“ค๊ณ  ์ธ์ฆ์ด ํ•„์š”ํ•œ ์š”์ฒญ์—๋Š” ํ—ค๋”์— ํ† ํฐ ๋„ฃ๋Š” ์ฝ”๋“œ๋ฅผ ๋งค๋ฒˆ ์ž‘์„ฑํ•˜๋Š” ๋“ฑ ์ค‘๋ณต ์ฝ”๋“œ๊ฐ€ ๋งŽ์ด ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด๋ฒˆ์— ๋ชจ๋“ˆํ™”๋ฅผ ์‹œ๋„ํ•ด๋ดค์Šต๋‹ˆ๋‹ค!

    • AxiosInstance๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๊ฐ™์ด ์‚ฌ์šฉ

    • ์ธ์ฆ์ด ํ•„์š” ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” defaultInstance๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์ธ์ฆ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—” authInstance๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

    • authInstance ์‘๋‹ต ์ธํ„ฐ์…‰ํ„ฐ์—์„œ๋Š” 401์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ refresh ํ† ํฐ์„ ๊ฐฑ์‹ ํ•˜๋Š” ์š”์ฒญ์„ ๋ณด๋ƒ…๋‹ˆ๋‹ค.

      POST /reissue ๋Š” ์ฟ ํ‚ค์— ์žˆ๋Š” refresh ํ† ํฐ์œผ๋กœ ๋ฆฌํ”„๋ ˆ์‹œ๊ฐ€ ๋งž๋Š”์ง€, ์œ ํšจํ•œ์ง€, DB์— ์ €์žฅ๋˜์–ด ์žˆ๋Š”์ง€๋ฅผ ๊ฒ€์‚ฌํ•œ ํ›„ accessToken๊ณผ refreshToken์„ ์žฌ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

      refresh ํ† ํฐ์ด ์œ ํšจํ•˜๋ฉด(200) ์ƒˆ๋กœ ๋ฐœ๊ธ‰๋œ accessToken์„ ๋‹ค์‹œ localStorage์— ์ €์žฅํ•˜๊ณ  ๊ธฐ์กด ์š”์ฒญ์„ ์žฌ์‹œ๋„ํ•ฉ๋‹ˆ๋‹ค.

      ์œ ํšจํ•˜์ง€ ์•Š์œผ๋ฉด ๋กœ๊ทธ์•„์›ƒ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค.

    • authInstance ์š”์ฒญ ์ธํ„ฐ์…‰ํ„ฐ์—๋Š” ์š”์ฒญ์ด ์„œ๋ฒ„์— ์ „์†ก๋˜๊ธฐ ์ „์— accessToken์„ ๊ฐ€์ ธ์™€ ํ—ค๋”์— ์ธ์ฆ ๊ถŒํ•œ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ดํ›„ ์ˆ˜์ •๋œ config๋ฅผ ๋ฐ˜ํ™˜ํ•ด์„œ ์ด ์„ค์ •๋Œ€๋กœ ์š”์ฒญ์ด ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค.

    • errorInterceptor ๋ฅผ ํ†ตํ•ด ์—๋Ÿฌ๋ฅผ ์‰ฝ๊ฒŒ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค. api ์š”์ฒญ ํ•จ์ˆ˜์— ์ ํž try-catch๋ฌธ์„ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์–ด api ํ•จ์ˆ˜๋„ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

๊ฐœ์ธ์ ์œผ๋กœ ๊ถ๊ธˆํ•œ ์ 

  • ์กฐํ˜„์ง„

data๊ฐ€ undefined์ผ ์ˆ˜ ์žˆ๋‹ค๋Š” ์—๋Ÿฌ๊ฐ€ ๋œฐ ๋•Œ ๊ทธ๊ฑธ ์–ด๋–ค ์‹์œผ๋กœ ํ•ด๊ฒฐํ•ด์•ผ ์ข‹์„์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค.

// ์ œ๊ฐ€ ์‚ฌ์šฉํ•œ ๋ฐฉ์‹
{
  data?.businessName || (
    <p className="text-2xl">๋‹‰๋„ค์ž„์„ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.</p>
  );
}

// ์กฐ๊ฑด๋ฌธ ์‚ฌ์šฉ
if (!data) {
  return <p>์กฐํšŒ ์‹คํŒจ</p>;
}

// ๋…ผ๋ฆฌ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ
{
	data && ~~
}

์ €๋Š” HostInfo.tsx, UserInfo.tsx ๋“ฑ์˜ ํŒŒ์ผ์—์„œ undefined์ผ ์ˆ˜ ์žˆ๋‹ค๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ฒซ ๋ฒˆ์งธ ๋ฐฉ์‹์ด๋‚˜ ์„ธ ๋ฒˆ์งธ ๋ฐฉ์‹์„ ์ฃผ๋กœ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.
undefined๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์—ฌ๋Ÿฌ ๊ฐœ๊ฐ€ ์žˆ๋Š”๋ฐ ์–ด๋–ค ๋ฐฉ์‹์ด ๊ฐ€์žฅ ์ ์ ˆํ•œ์ง€,
๊ทธ๋ฆฌ๊ณ  undefined๋ฅผ ๋ณดํ†ต ์ปดํฌ๋„ŒํŠธ์ชฝ์—์„œ ๊ฑธ๋Ÿฌ๋‚ด๋Š”์ง€, ์•„๋‹ˆ๋ฉด api ํ•จ์ˆ˜๋‚˜ hook์—์„œ ๋จผ์ € ๊ฑธ๋Ÿฌ๋‚ธ ๋ฐ์ดํ„ฐ๋ฅผ ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉํ•˜๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!

  • ์ด์€์ˆ˜

API ํ†ต์‹  ์‹œ, ์„œ๋ฒ„์—์„œ ๋ณด๋‚ด์ฃผ๋Š” ์—๋Ÿฌ status code๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ errorInterceptor์—์„œ ๋ถ„๋ฆฌํ•ด ์ฒ˜๋ฆฌํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ๊ฐ ์—๋Ÿฌ๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•ด์•ผ ํ• ์ง€ ์•„์ง ๊ฐ์ด ์•ˆ ์˜ต๋‹ˆ๋‹ค. ๊ณตํ†ต์œผ๋กœ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•œ ๋ฒˆ์— ํ•˜๋Š” ๋ฐฉ์‹์ด ๋‚˜์„์ง€, ์•„๋‹ˆ๋ฉด ๊ฐ API ์š”์ฒญ ๋กœ์ง์—์„œ ์—๋Ÿฌ๋ฅผ ๊ฐœ๋ณ„์ ์œผ๋กœ ํ•ธ๋“ค๋งํ•˜๋Š” ๊ฒŒ ๋” ๋‚˜์€ ๋ฐฉ๋ฒ•์ธ์ง€ ๊ณ ๋ฏผ์ž…๋‹ˆ๋‹ค!

@eunjju2 eunjju2 self-assigned this Dec 1, 2024
Copy link

@devpang20 devpang20 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๐Ÿ‘ ์ž˜๋œ ์ 

  • ๊ทœ๋ชจ ๋งž์ถฐ์„œ ์ ์ ˆํžˆ ๋‚˜๋ˆˆ ํด๋” ๊ตฌ์กฐ
  • axios ์ธํ„ฐ์…‰ํ„ฐ๋ฅผ ํ†ตํ•œ ๋ชจ๋“ˆํ™”
  • ๊ณตํ†ต ์ปดํฌ๋„ŒํŠธ ๋ถ„๋ฆฌ, ์œ ํ‹ธ ํ•จ์ˆ˜ ์žฌ์‚ฌ์šฉ

๐Ÿ› ๏ธ ๊ฐœ์„ ํ•  ์ 

  1. ์—๋Ÿฌ์ฒ˜๋ฆฌ ๊ฐ•ํ™”, ๊ณตํ†ต ์—๋Ÿฌ์™€ ๊ฐœ๋ณ„ ์—๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ๋กœ ๋ณ€๊ฒฝ(์ฝ”๋“œ ์ฝ”๋ฉ˜ํŠธ 1-n ์ฐธ๊ณ )
  • ๋Œ€๋ถ€๋ถ„์˜ ์—๋Ÿฌ๋Š” ๊ณตํ†ต ์ฒ˜๋ฆฌ๋กœ ํ•ด๊ฒฐ
  • ํŠน๋ณ„ํ•œ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ๋งŒ ๊ฐœ๋ณ„ API์—์„œ ์ฒ˜๋ฆฌ
  • ์ฝ”๋“œ ์ค‘๋ณต์„ ์ตœ์†Œํ™”ํ•˜๋ฉด์„œ๋„ ์œ ์—ฐํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ
  1. ์ปค์Šคํ…€ ํ›…์„ ํ†ตํ•œ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๋กœ์ง ๋ถ„๋ฆฌ(์ฝ”๋“œ ์ฝ”๋ฉ˜ํŠธ 2-n ์ฐธ๊ณ )

๋ฐ์ดํ„ฐ ํ๋ฆ„์ด ๋ช…ํ™•ํ•ด์ง

  • API โ†’ ์ปค์Šคํ…€ ํ›… โ†’ ์ปดํฌ๋„ŒํŠธ
  • undefined ์ฒ˜๋ฆฌ๊ฐ€ ์ปค์Šคํ…€ ํ›…์—์„œ ์ด๋ฃจ์–ด์ง

์ปดํฌ๋„ŒํŠธ ์ฑ…์ž„์ด ๋ถ„๋ฆฌ๋จ

  • ๋ฐ์ดํ„ฐ fetching์€ ์ปค์Šคํ…€ ํ›…์—์„œ
  • UI ๋ Œ๋”๋ง์€ ์ปดํฌ๋„ŒํŠธ์—์„œ

์—๋Ÿฌ ์ฒ˜๋ฆฌ๊ฐ€ ์ฒด๊ณ„์ 

  • ๋กœ๋”ฉ/์—๋Ÿฌ/๋นˆ ์ƒํƒœ ์ฒ˜๋ฆฌ๊ฐ€ ๋ช…ํ™•ํ•จ
  • ์‚ฌ์šฉ์ž์—๊ฒŒ ์ ์ ˆํ•œ ํ”ผ๋“œ๋ฐฑ ์ œ๊ณต

ํƒ€์ž… ์•ˆ์ •์„ฑ ํ™•๋ณด

  • ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” ์ด๋ฏธ ํƒ€์ž…์ด ๋ณด์žฅ๋œ ๋ฐ์ดํ„ฐ ์‚ฌ์šฉ
  • undefined ์ฒดํฌ ๋ถˆํ•„์š”
    ์ด๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ๋” ์•ˆ์ •์ ์ด๊ณ  ์œ ์ง€๋ณด์ˆ˜ํ•˜๊ธฐ ์‰ฌ์›Œ์งˆ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ถ”๊ฐ€์ ์ธ ์ถ”์ฒœ

export interface ApiResponse<T> {
  data: T;
  status: number;
  message: string;
}

export interface ApiError {
  code: string;
  message: string;
  status: number;
}

// API ์ƒํƒœ ์ฝ”๋“œ ์ƒ์ˆ˜ํ™”
export const API_STATUS = {
  SUCCESS: 200,
  UNAUTHORIZED: 401,
  FORBIDDEN: 403,
  NOT_FOUND: 404,
  SERVER_ERROR: 500,
} as const;

// ์—๋Ÿฌ์ฒ˜๋ฆฌ ์ƒ์ˆ˜ํ‚ค์›Œ๋“œ ์ด์šฉ
switch (status) {
  case API_STATUS.UNAUTHORIZED:
    return '๋กœ๊ทธ์ธ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.';
  case API_STATUS.FORBIDDEN:
    return '์ ‘๊ทผ ๊ถŒํ•œ์ด ์—†์Šต๋‹ˆ๋‹ค.';
  case API_STATUS.NOT_FOUND:
    return '์š”์ฒญํ•˜์‹  ์ •๋ณด๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.';
  case API_STATUS.SERVER_ERROR:
    return '์„œ๋ฒ„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.';
  default:
    return '์•Œ ์ˆ˜ ์—†๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.';

ํด๋”๊ตฌ์กฐ ์ฐธ๊ณ  ์ž๋ฃŒ

  • FSD ๋ผ๋Š” ๊ตฌ์กฐ๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ๋„ ์ฐธ๊ณ , ํ˜„์žฌ ์œ ๋ช…ํ•œ ๋””๋ ‰ํ† ๋ฆฌ ๋ถ„๋ฅ˜ ๊ธฐ์ค€์ด๋ผ ์ฐธ๊ณ ์ •๋„ ํ•ด๋ณด๋ฉด ์ข‹์Œ. ์˜์ƒ์—๋„ ๋‚˜์˜ค์ง€๋งŒ ๋””๋ ‰ํ† ๋ฆฌ ๊ตฌ์กฐ ๋ถ„๋ฅ˜์— ์ •๋‹ต์€ ์—†์Œ. ๋ถˆํŽธํ• ๋•Œ ๊ฐœ์„ ์„ ์œ„ํ•ด ๋‚˜๋ˆ„๋Š”๊ฒŒ ์ •๋‹ต.
    https://www.youtube.com/watch?v=64Fx5Y1gEOA

src/apis/index.ts Show resolved Hide resolved
return response.data;
};

// ๋ฆฌ๋ทฐ ์ž‘์„ฑ

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 1-2 ๊ฐœ๋ณ„ ์—๋Ÿฌ ์ฒ˜๋ฆฌ์˜ ๊ฒฝ์šฐ ์•„๋ž˜์™€ ๊ฐ™์ด ์ฒ˜๋ฆฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
export const postReview = async (data: ReviewData): Promise<Review> => {
  try {
    const response = await authInstance.post('/api/v1/review', data);
    return response.data;
  } catch (error) {
    // ๋ฆฌ๋ทฐ ์ž‘์„ฑ ์‹œ ํŠน๋ณ„ํžˆ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋Š” ์—๋Ÿฌ๋งŒ ์—ฌ๊ธฐ์„œ ์ฒ˜๋ฆฌ
    if (error.response?.status === 400) {
      throw new Error('๋ฆฌ๋ทฐ ๋‚ด์šฉ์„ ํ™•์ธํ•ด์ฃผ์„ธ์š”.');
    }
    // ๋‚˜๋จธ์ง€๋Š” ๊ณตํ†ต ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋กœ ์ „ํŒŒ
    throw error;
  }
};

createdAt: '2024-11-12T14:00:00',
},
];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. data undefined ์—๋Ÿฌ ์ฒ˜๋ฆฌ
    2-1 ์ปค์Šคํ…€ hooks ๋„์ž…
  • src/hooks/useWorkplaceReview.ts ์ƒ์„ฑ
import { useQuery } from '@tanstack/react-query';
import { ReviewData } from '../types';

export const useWorkplaceReview = (workplaceId: string) => {
  const { data, isLoading, error } = useQuery({
    queryKey: ['workplaceReview', workplaceId],
    queryFn: () => getWorkplaceReview(workplaceId),
    retry: 1,
  });

  return {
    reviews: data ?? [], // undefined์ผ ๊ฒฝ์šฐ ๋นˆ ๋ฐฐ์—ด ๋ฐ˜ํ™˜
    isLoading,
    error: error as Error | null,
  };
};
  • 2-2 hooks ๋„์ž… ํ›„ WorkPlaceReview ์ˆ˜์ •
import { useWorkplaceReview } from '@/hooks/useWorkplaceReview';

const WorkPlaceReview = ({ workplaceId }: { workplaceId: string }) => {
  const { reviews, isLoading, error } = useWorkplaceReview(workplaceId);

  if (isLoading) {
    return (
      <div className='w-custom flex justify-center'>
        <p>๋ฆฌ๋ทฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์ค‘...</p>
      </div>
    );
  }

  if (error) {
    return (
      <div className='w-custom flex justify-center'>
        <p className='text-subfont'>๋ฆฌ๋ทฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.</p>
      </div>
    );
  }

  if (reviews.length === 0) {
    return (
      <div className='w-custom flex justify-center'>
        <p className='text-subfont'>์ž‘์„ฑ๋œ ๋ฆฌ๋ทฐ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.</p>
      </div>
    );
  }

  return (
    <div className='w-custom'>
      {reviews.map((item) => (
        <div
          key={item.reviewId}
          className='border-b border-subfont py-[16px] last:border-none'
        >
          <ReviewComponent review={item} />
        </div>
      ))}
    </div>
  );
};

createdAt: string;
};
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 2-3 ReviewComponent์—์„œ์˜ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ
interface ReviewComponentProps {
  review: ReviewData;
}

const ReviewComponent = ({ review }: ReviewComponentProps) => {
  // review๋Š” ์ด๋ฏธ ํƒ€์ž…์ด ๋ณด์žฅ๋œ ์ƒํƒœ
  const { userName, reviewRating, content, createdAt } = review;

  return (
    <div>
      <h3>{userName}</h3>
      <div>{reviewRating}</div>
      <p>{content}</p>
      <span>{createdAt}</span>
    </div>
  );
};

JOEIH and others added 28 commits December 5, 2024 21:06
์‚ฌ์ง„ ๋ˆŒ๋Ÿฌ์„œ ์ƒ์„ธํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๋Š” ๊ฑด ์ถ”๊ฐ€ ์•ˆ ํ•˜๊ธฐ๋กœ
โœจ Feat : ์‚ฌ์—…์ž ์˜ˆ์•ฝ์ž ์กฐํšŒ, ๋ฆฌ๋ทฐ ์ž‘์„ฑ ์™„๋ฃŒ๋œ ์˜ˆ์•ฝ ๋ฌธ๊ตฌ ํ‘œ์‹œ ๋ฐ ๋ฆฌ๋ทฐ ํŽ˜์ด์ง€๋กœ ์ด๋™
โœจ Feat : ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€ ๋ฐ ์ƒ์„ธ ํŽ˜์ด์ง€ ๊ตฌํ˜„ ์™„๋ฃŒ
โœจ Feat, โ™ป๏ธ Refactor : ๊ฒ€์ƒ‰ ํŽ˜์ด์ง€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ๋ฐ ์˜ˆ์•ฝ ๊ฒฐ์ œ ํŽ˜์ด์ง€ ๊ฐœ์„ 
โœจ Feat : ์˜ˆ์•ฝ ๋‚ด์—ญ ํŽ˜์ด์ง€ ๊ฒฐ์ œ ์ทจ์†Œ ๊ธฐ๋Šฅ ๊ตฌํ˜„
JOEIH and others added 30 commits January 9, 2025 15:01
โ™ป๏ธ Refactor : ์˜ˆ์•ฝ ๊ธฐ๋Šฅ ๊ฐœ์„ 
โœจ Feat : ๋ฆฌ๋ทฐ ์ˆ˜์ • ๋ฐ ์‚ญ์ œ ๊ธฐ๋Šฅ ์ถ”๊ฐ€
โœจ Feat, ๐Ÿ”ง Fix, โ™ป๏ธ Refactor : ๊ฒฐ์ œ ํŽ˜์ด์ง€ ๋ฐ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ๊ฐœ์„ 
โ™ป๏ธ Refactor : ์ตœ๊ทผ 7์ผ ์ด๋‚ด ์•Œ๋ฆผ ๊ตฌ๋ถ„์„  ์ถ”๊ฐ€
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants