๐Ÿ’ ๋ฆฌ์•กํŠธ์—์„œ ํŽ˜์ด์ง€๋„ค์ด์…˜ ๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•˜๊ธฐ

Somiยท2021๋…„ 8์›” 12์ผ
2
post-thumbnail

๋ฏธ๋ฃจ๊ณ  ๋ฏธ๋ฃจ๋˜ ํŽ˜์ด์ง€๋„ค์ด์…˜

์ง€๋‚œ 1์ฐจ ํ”„๋กœ์ ํŠธ(๋‹ฅํ„ฐ ๋ชจ์งœ)์—์„œ ์‹œ๊ฐ„์˜ ์ œ์•ฝ(๊ทธ๋ฆฌ๊ณ  ๋Šฅ๋ ฅ์˜ ์ œ์•ฝ)์œผ๋กœ ๊ตฌํ˜„ํ•˜์ง€ ๋ชปํ–ˆ๋˜ ํŽ˜์ด์ง€๋„ค์ด์…˜ ๊ธฐ๋Šฅ์„ ์ด๋ฒˆ์—๋Š” ๊ผญ ๊ตฌํ˜„ํ•˜๋ฆฌ๋ผ ์ƒ๊ฐํ–ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์˜ˆ์ƒ๋Œ€๋กœ ํŽ˜์ด์ง€๋„ค์ด์…˜ ๊ตฌํ˜„์€ ์‰ฝ์ง€ ์•Š์•˜๋‹ค...๐Ÿ™ˆ

์ผ๋‹จ ๋ฐฐ์—ด๋กœ ๋ฐ›์€ ํŽ˜์ด์ง€์— ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์œผ๋กœ fetch๋ฅผ ํ•˜๋Š” ๊ฒƒ๊นŒ์ง€๋Š” ๊ตฌํ˜„ํ•ด์ค€ ์ƒํƒœ์˜€๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋ฐ์ดํ„ฐ๊ฐ€ ๋Š˜์–ด๋‚˜๋ฉด์„œ 99ํŽ˜์ด์ง€๊นŒ์ง€ ํŽ˜์ด์ง€๊ฐ€ ๋Š˜์–ด๋‚˜๊ฒŒ ๋˜์—ˆ๊ณ ... ์›นํŽ˜์ด์ง€ ํ•˜๋‹จ ๋ถ€๋ถ„์— 99๊ฐœ์— ๋‹ฌํ•˜๋Š” ํŽ˜์ด์ง€๋ฒ„ํŠผ์ด ๋Š˜์–ด์ ธ์žˆ๋Š” ์ง„๊ท€ํ•œ ํ˜•ํƒœ๋ฅผ ๋ณด์˜€๋‹ค.

์ผ๋‹จ 99๊ฐœ๊นŒ์ง€ ํŽผ์ณ์ง„ ๋ฐฐ์—ด์„ 10๊ฐœ๋กœ ์˜ˆ์˜๊ฒŒ ์ž˜๋ผ์„œ ๋ณด์—ฌ์ค˜์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค๊ธดํ–ˆ์ง€๋งŒ, ๊ทธ ๋‹ค์Œ์— ๋ญ˜ ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ์ง€ ๋ง‰๋ง‰ํ–ˆ๋‹ค. ๊ฒฐ๊ตญ ๋ฉ˜ํ† ๋‹˜๊ป˜ ๋„์›€์„ ์ฒญํ•ด ํ•ด๊ฒฐ์˜ ์‹ค๋งˆ๋ฆฌ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์—ˆ๋‹ค. ์˜ค๋Š˜์€ ๊ทธ ํ•ด๊ฒฐ๊ณผ์ •์„ ๊ณต์œ ํ•˜๊ณ ์ž ํ•œ๋‹ค.

(ํ˜น์‹œ ์•„๋ž˜์˜ ์ฝ”๋“œ๊ฐ€ ์ž˜ ์ดํ•ด๊ฐ€ ์•ˆ๊ฐ„๋‹ค๋ฉด ๊นƒํ—™์„ ์ฐธ์กฐํ•ด์ฃผ์‹œ๊ธธ..)


ํŽ˜์ด์ง€๋„ค์ด์…˜ ๊ตฌํ˜„ ๋ฐฉ๋ฒ•

โ€ป์ฃผ์˜: ํ•ด๋‹น ์ฝ”๋“œ๋Š” ์ฝ”๋“œ๋ฆฌ๋ทฐ๋ฅผ ๋ฐ›์€ ์ ์ด ์—†๋Š” ๋‚ ๊ฒƒ์˜ ์ฝ”๋“œ๋กœ ํ˜น์‹œ ๋” ์ข‹์€ ์ฝ”๋“œ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ํŒ์ด ์žˆ๋‹ค๋ฉด ๊ผญ ์•Œ๋ ค์ฃผ์„ธ์š”!

1. ํŽ˜์ด์ง€๋„ค์ด์…˜์„ ์œ„ํ•œ ๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ

๋จผ์ € state๊ฐ’์œผ๋กœ totalPage๋ฅผ ๋นˆ ๋ฐฐ์—ด๋กœ ์„ค์ •ํ•ด์ค€๋‹ค. ๊ทธ๋ฆฌ๊ณ  makePageArray๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด for๋ฌธ๊ณผ push๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ•„์š”ํ•œ ๋ชจ๋“  ํŽ˜์ด์ง€๊ฐ€ ํ•ด๋‹น ๋ฐฐ์—ด์•ˆ์— ๋“ค์–ด์˜ค๋„๋ก ํ•ด์ค€๋‹ค.

const makePageArray = pages => {
    let pageArray = [];
    for (let i = 1; i <= pages; i++) {
      pageArray.push(i);
    }
    setTotalPage(pageArray);
};

2. ๋ฐ›์•„์˜จ ๋ฐฐ์—ด์„ ์˜ˆ์˜๊ฒŒ ์ž˜๋ผ์„œ ์ค€๋น„ํ•ด๋‘์ž

์ „์ฒด ํŽ˜์ด์ง€ ๋ฐฐ์—ด์ด ์ค€๋น„๋๋‹ค๋ฉด ์›ํ•˜๋Š” ์ˆ˜ ๋งŒํผ slice๋กœ ์ž˜๋ผ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ค€๋น„ํ•ด๋‘”๋‹ค.

  let pageArr = [];
  const pagination = () => {
    for (let i = 0; i < totalPage.length; i += 10) {
      pageArr.push(totalPage.slice(i, i + 10));
    }
    return pageArr;
  };

์œ„์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค.

[
  [ 1, 2, 3,...,10 ],
  [ 11, 12, 13,..., 20 ],
  [ 21 ]
]

2. state ๊ฐ’์œผ๋กœ ์ •๋ณด๋ฅผ ๊ด€๋ฆฌํ•˜์ž

๊ทธ๋Ÿฌ๋‚˜ ์œ„ ์ฝ”๋“œ๋งŒ์œผ๋กœ๋Š” ํŽ˜์ด์ง€๋„ค์ด์…˜์„ ์˜จ์ „ํ•˜๊ฒŒ ์‹คํ–‰ํ•  ์ˆ˜ ์—†๋‹ค. ๋”ฐ๋ผ์„œ state๋กœ ํ•„์š”ํ•œ ์ •๋ณด(์ด์ „ ํŽ˜์ด์ง€ ๋ฐฐ์—ด, ๋‹ค์Œ ํŽ˜์ด์ง€ ๋ฐฐ์—ด, ํ˜„์žฌ ํŽ˜์ด์ง€ ๋ฐฐ์—ด)๋ฅผ ๋งˆ๋ จํ•ด๋‘”๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ „์ฒด ํŽ˜์ด์ง€๊ฐ€ 0๋ณด๋‹ค ํฌ๋ฉด 1๋ฒˆ์˜ ์ฝ”๋“œ์™€ ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ํ•ฉ์นœ makePageInfo๋ฅผ ์‹คํ–‰์‹œ์ผœ์ค€๋‹ค.

const currentGroup =
      pagination(pageArr)[Math.floor((currentPage - 1) / 10)];

let pageInfo = {
      arr: currentGroup.map(el => el),
      perv: prevPage ? prevPage : null,
      next: prevPage + 11 <= totalPage.length ? prevPage + 11 : null,
    };

3. ํŽ˜์ด์ง€ ๋ฒ„ํŠผ ํด๋ฆญ์‹œ ํŽ˜์ด์ง€ ์ „ํ™˜/๋‹ค์Œ ๋˜๋Š” ์ด์ „ ํŽ˜์ด์ง€ ๋ฐฐ์—ด ์ด๋™

ํŽ˜์ด์ง€ ๋ฐฐ์—ด์„ ์ „ํ™˜ํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ๋งŒ๋“ค์–ด์ค€๋‹ค.

const pageClick = e => {
    setCurrentPage(+e.target.id);
  };

const changePage = e => {
    e.target.id === 'prev'
      ? setCurrentPage(makePageInfo()?.perv)
      : setCurrentPage(makePageInfo()?.next);
  };

์œ„์˜ ๋ชจ๋“  ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œํ‚ฌ ํŽ˜์ด์ง€๋„ค์ด์…˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ๋„ฃ์–ด์ค€๋‹ค.

{totalPage.length && (
   <Pagination
    pages={makePageInfo()?.arr}
 	pageClick={pageClick}
  	totalPage={totalPage}
    currentPage={currentPage}
    changePage={changePage}
   />
)}

ํŽ˜์ด์ง€๋„ค์ด์…˜ ์ปดํฌ๋„ŒํŠธ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ƒ๊ฒผ๋‹ค!

export default function Pagination({
  pages,
  pageClick,
  currentPage,
  changePage,
}) {
  return (
    <PageWrapper>
      <ChangeBtn onClick={changePage} id="prev">
        <i className="fas fa-arrow-left" />
      </ChangeBtn>
      <PageListBox>
        {pages.map(page => {
          return (
            <PageBtn
              isSelectedPage={currentPage === page}
              id={page}
              key={page}
              onClick={pageClick}
            >
              {page}
            </PageBtn>
          );
        })}
      </PageListBox>

      <ChangeBtn onClick={changePage} name="next">
        <i className="fas fa-arrow-right" />
      </ChangeBtn>
    </PageWrapper>
  );
}

4. ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์œผ๋กœ fetch ๋ฐ›๊ธฐ

๋งˆ์ง€๋ง‰์œผ๋กœ makePageArray ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ fetchํ•˜๋ฉด ๋์ด๋‹ค!

useEffect(() => {
    const offset = currentPage - 1;
    const requestOptions = {
      method: 'GET',
      headers: {
        Authorization: localStorage.getItem('accessToken'),
      },
    };

    if (currentStatus > 0) {
      fetch(
        `${HISTORY_API}?search=${search}&status=${currentStatus}&offset=${
          offset * LIMIT
        }&limit=${LIMIT}`,
        requestOptions
      )
        .then(res => res.json())
        .then(res => {
          setInvestLog(res);
          makePageArray(Math.ceil(res.count[currentStatus] / LIMIT));
        });
    } else {
      fetch(
        `${HISTORY_API}?search=${search}&offset=${
          offset * LIMIT
        }&limit=${LIMIT}`,
        requestOptions
      )
        .then(res => res.json())
        .then(res => {
          setInvestLog(res);
          makePageArray(Math.ceil(res.count.all / LIMIT));
        });
    }
  }, [currentPage, currentStatus, search]);

๋ฐฑ์—์„œ ์ค€ ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์„ ๊ฐ€์ง€๊ณ  ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค์—ˆ๋‹ค. currentStatus๋Š” ์˜ต์…˜ ์„ ํƒ ๊ฐ’์ด๋ผ ๋ฌด์‹œํ•ด๋„ ๋œ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ์•Œ๊ณ ๋ณด๋‹ˆ ์•„๋ž˜์™€ ๊ฐ™์ด ์“ธ ์ˆ˜ ์žˆ๋‹ค๊ณ ... ์•„์ง ์ดํ•ด๊ฐ€ ์•ˆ๊ฐ€์ง€๋งŒ ๋‚˜์ค‘์— ๋ฆฌํŒฉํ† ๋ง ํ•  ๋•Œ ์‚ฌ์šฉํ•ด๋ณด๊ณ  ์‹ถ๋‹ค!

 const opt = {
     search,
     ...(currentStatus > 0 && { status: currentStatus }),
 };

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