๐Ÿ“šReadyBooks?!๐Ÿ“š 2์ฃผ์ฐจ ๊ณ ๋ฏผํ•œ ์ฝ”๋“œ์˜ ํ”์ ๋“ค

Maria Kimยท2021๋…„ 12์›” 26์ผ
0
post-custom-banner

RestfulApi ์—์„œ method์— ๋”ฐ๋ฅธ body ์˜ ์œ ๋ฌด

  • POST , PATCH -> body ์žˆ์Œ
  • DELETE, GET -> body ์—†์Œ
    (DELETE ๋Š” body ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋Š” ์žˆ์ง€๋งŒ body ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํšจ์œจ์•„ ๋–จ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ,
    query ๋ฅผ ๋Œ€์‹  ์‚ฌ์šฉํ•จ)

status ๊ฐ€ 204์ธ ๊ฒฝ์šฐ

  • ์‚ญ์ œ ์„ฑ๊ณต ์‹œ ๋ฐฑ์€ ํ”„๋ก ํŠธ์—๊ฒŒ status๋ฅผ 204๋กœ ์ฃผ๋ฉฐ ์„ฑ๊ณตํ–ˆ๋‹ค๋Š” ์˜๋ฏธ๋ฅผ ๋ณด๋‚ธ๋‹ค.
  • 204๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ reponse๋ฅผ ๋ณด๋‚ผ ์ˆ˜ ์—†๋‹ค.
  • status, statusText ๋กœ ํ”„๋ก ํŠธ๋Š” ์‚ญ์ œ ์„ฑ๊ณต์„ ํ™•์ธํ•˜๊ณ  ๋‹ค์Œ ํ–‰๋™์„ ํ•ด์•ผ ํ•œ๋‹ค.

์—๋Ÿฌ ์ฒ˜๋ฆฌ

  • ๋ณ€๊ฒฝ ์ „
switch (res.message) {
  case 'SUCCESS':
    alert('์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค');
    break;
  case 'BOOK_NOT_EXIST':
    alert('์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ฑ…์ž…๋‹ˆ๋‹ค.');
    break;
  case 'BOOK_ALREADY_EXIST':
    alert('์ด๋ฏธ ๊ตฌ๋งคํ•œ ์ฑ…์ž…๋‹ˆ๋‹ค.');
    break;
  case 'INVALID_CART':
  case 'KEY_ERROR':
    alert('์—๋Ÿฌ์ž…๋‹ˆ๋‹ค.');
    break;
  default:
    break;
}
  • ๋ณ€๊ฒฝ ํ›„ 1์ฐจ
const ERRORS = {
  SUCCESS: '์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค.',
  BOOK_NOT_EXIST: '์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ฑ…์ž…๋‹ˆ๋‹ค.',
  BOOK_ALREADY_EXIST: '์ด๋ฏธ ๊ตฌ๋งคํ•œ ์ฑ…์ž…๋‹ˆ๋‹ค.',
};

// ...

if(ERROR[res.message]) {
	return alert(ERRORS[res.message])
}
alert('์—๋Ÿฌ์ž…๋‹ˆ๋‹ค');
  • ๋ณ€๊ฒฝ 2์ฐจ
    -> ์—๋Ÿฌ ๋ถ€๋ถ„๋งŒ์„ ์ฒ˜๋ฆฌํ•˜๋Š” js ํŒŒ์ผ ์ƒ์„ฑ

modal ์Šคํฌ๋กค ์—†์• ๊ธฐ(+์ตœ์ƒ๋‹จ์œผ๋กœ ์˜ฌ๋ผ๊ฐ€์ง€ ์•Š๊ฒŒ ํ•˜๊ธฐ)

  useEffect(() => {
    const scrollY = window.scrollY;
    document.body.style.overflow = 'hidden';

    return () => {
      document.body.style.cssText = '';
      window.scrollTo(0, parseInt(scrollY * -1 || '0', 10) * -1);
    };
  }, []);
  • ์ด ๋ฐฉ๋ฒ•์€ ์‚ฌ์‹ค ๋งค๋„๋Ÿฝ์ง€๋Š” ์•Š๋‹ค ๊ทธ๋ž˜์„œ ๋” ๋งค๋„๋Ÿฌ์šด ๋ฐฉ์‹์„ ์ฐพ์•„๋ณผ ์˜ˆ์ •
profile
Frontend Developer, who has business in mind.
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€