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

Sp1/#566 cart page api #576

Merged
merged 21 commits into from
Dec 7, 2023
Merged

Sp1/#566 cart page api #576

merged 21 commits into from
Dec 7, 2023

Conversation

gunom
Copy link
Contributor

@gunom gunom commented Dec 6, 2023

๐Ÿ”ฅ Related Issues

resolved #566

๐Ÿ’œ ์ž‘์—… ๋‚ด์šฉ

  • ์žฅ๋ฐ”๊ตฌ๋‹ˆ item ์‚ญ์ œ ๋ชจ๋‹ฌ API ์—ฐ๊ฒฐ
  • ์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ˆ˜๋Ÿ‰ ๋ณ€๊ฒฝ API ์—ฐ๊ฒฐ
  • ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๊ฒฐ์ œ ํŽ˜์ด์ง€(OrderPage) ์—๋Ÿฌ ์—†์ด ์—ฐ๊ฒฐ๋˜๋„๋ก ์—ฐ๊ฒฐ ๋ฐ Order API ์ˆ˜์ •
  • CompletePage๋กœ ์ด๋™ ์‹œ ํ•„๋“œ๋ช… ์ˆ˜์ •

โœ… PR Point

const CartItem = ({
  id,
  mainImageUrl,
  name,
  price,
  originalPrice,
  count,
  handleClickQuantityButton,
}: {
  id: number;
  mainImageUrl: string;
  name: string;
  price: number;
  originalPrice: number;
  count: number;
  handleClickQuantityButton: (price: number) => void;
}) => {
  const [quantity, setQuantity] = useState(count);
  const [modalOn, setModalOn] = useState(false);
  const navigate = useNavigate();

  const fetchData = async () => {
    await api
      .patch(`/cart`, {
        cartCountReqs: [
          {
            cartId: id,
            count: quantity + 1,
          },
        ],
      })
      .then()
      .catch((err) => {
        navigate('/error');
      });
  };

  const [debouncedFetchData, setDebouncedFetchData] = useState(() => debounce(fetchData, 300));

  useEffect(() => {
    // quantity๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค debouncedFetchData๋ฅผ ์ƒˆ๋กœ ์„ค์ •
    setDebouncedFetchData(() => debounce(fetchData, 300));
    return () => {
      debouncedFetchData.cancel(); // ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์–ธ๋งˆ์šดํŠธ ๋  ๋•Œ debounce๋ฅผ ์ทจ์†Œํ•ฉ๋‹ˆ๋‹ค
    };
  }, [quantity]); // ์˜์กด์„ฑ ๋ฐฐ์—ด์— quantity ์ถ”๊ฐ€

  const handleClickPlusButton = (price: number) => {
    handleClickQuantityButton(price);
    setQuantity((prev) => prev + 1);
    debouncedFetchData();
  };

  const handleClickMinusButton = (price: number) => {
    if (quantity > 1) {
      handleClickQuantityButton(-price);
      setQuantity((prev) => prev - 1);
      debouncedFetchData();
    }
  };

  return (
    <St.Item>
      <St.ItemInformation>
        <St.TattooImage src={mainImageUrl} />
        <St.TitleSection>
          <St.TattooTitle>{name}</St.TattooTitle>
          <St.ItemPriceBox>
            <St.TattooPriceText>{price.toLocaleString()}์›</St.TattooPriceText>
            <St.TattooOriginalPriceText>
              {originalPrice.toLocaleString()}์›
            </St.TattooOriginalPriceText>
          </St.ItemPriceBox>
        </St.TitleSection>
      </St.ItemInformation>
      <St.ButtonSection>
        <IcCancelDark onClick={() => setModalOn(true)} />
        {modalOn && <DeleteCartModal setModalOn={setModalOn} id={id} />}
        <St.Stepper>
          {quantity === 1 ? (
            <IcMinusOneunder />
          ) : (
            <IcMinus onClick={() => handleClickMinusButton(price)} />
          )}
          <span>{quantity}</span>
          <IcPlus onClick={() => handleClickPlusButton(price)} />
        </St.Stepper>
      </St.ButtonSection>
    </St.Item>
  );
};

ํ•ด๋‹น ๋ถ€๋ถ„์—์„œ ์ˆ˜๋Ÿ‰์„ ์ฆ๊ฐ€ ์‹œํ‚ฌ ์‹œ debounce๋ฅผ ์ด์šฉํ•˜์—ฌ ์ผ์ • ์‹œ๊ฐ„ ์ดํ›„ ์ž…๋ ฅ์ด ์—†์„ ์‹œ์—๋งŒ ์„œ๋ฒ„์™€ API ํ†ต์‹ ์„ ์ง„ํ–‰ํ•˜๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค. throttle, debounce ์ฐธ๊ณ ํ•˜์„ธ์š”.

// OrderPage
const OrderPage = () => {
  const location = useLocation();
  const state = location.state as { stickerId: number; count: number; shippingFee: number };
  const { response, error, loading } = useGetOrdersheet(
    state ? state : ({ stickerId: 0, count: 0 } as OrderSheetRequest),
  );

์žฅ๋ฐ”๊ตฌ๋‹ˆ์—์„œ ๋„˜์–ด์˜ฌ ๋•Œ๋Š” state๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿฐ ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค.

const useGetOrdersheet = ({ stickerId, count }: OrderSheetRequest) => {
  const navigate = useNavigate();

  const [response, setResponse] = useState<OrderSheetProps>();
  const [error, setError] = useState<AxiosError>();
  const [loading, setLoading] = useState(true);

  const url =
    stickerId && count
      ? `/order/ordersheet?stickerId=${stickerId}&count=${count}`
      : '/order/ordersheet';

  const fetchData = async () => {
    await api
      .get(url)

๋”ฐ๋ผ์„œ url๋„ stickerId์™€ count ์œ ๋ฌด๋กœ ๋‹ค๋ฅด๊ฒŒ ์ง€์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

const CompletePage = () => {
  const renderCompletePageHeader = () => {
    return <Header title='์ฃผ๋ฌธํ•˜๊ธฐ' />;
  };

  const location = useLocation();
  const { orderSheetStickersRes, orderAmountDetailRes } = location.state as {
    orderSheetStickersRes: orderSheetStickersResProps[];
    orderAmountDetailRes: orderAmountDetailResProps;
  };

  return (
    <PageLayout renderHeader={renderCompletePageHeader} footer={<CompleteFooter />}>
      <St.Container>
        <Result
          mainText={'๊ฒฐ์ œ๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ์–ด์š”'}
          description={'7์ผ ๋‚ด์— ๋ฐฐ์†ก์ด ์‹œ์ž‘๋˜๋ฉฐ, ๋ฌธ์ž๋กœ ์•ˆ๋‚ด๋“œ๋ ค์š”'}
        />
        <St.Line />
        <St.Title>์ฃผ๋ฌธ ์ •๋ณด</St.Title>
        {orderSheetStickersRes.map((item) => {
          return <ProductInfo key={item.name} orderSheetSticker={item} />;
        })}
        <St.LightLine />
        <St.PriceContainer>
          <PaymentMini orderAmountDetailRes={orderAmountDetailRes} />
        </St.PriceContainer>
      </St.Container>
    </PageLayout>
  );
};

CompletePage๋„ ๋ฐฐ์—ด๋กœ ์ˆ˜ํ–‰๋˜๋„๋ก ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ˜ก Trouble Shooting

ํ˜„์žฌ ์žฅ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ ์‚ญ์ œํ•˜๊ณ  ํ™”๋ฉด์„ reloadํ•ด์ฃผ๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๊ฒŒ ์‚ฌ์šฉ์„ฑ์ด ์•ˆ์ข‹์•„๋ณด์ด๊ธฐ๋„ ํ•ด์„œ ์žฅ๋ฐ”๊ตฌ๋‹ˆ item๋“ค์„ ์ „๋ถ€ state๋กœ ๊ด€๋ฆฌํ•˜๋Š”๊ฒŒ ๋งž์„์ง€ ๊ณ ๋ฏผ์ž…๋‹ˆ๋‹ค.

const DeleteCartModal = ({ setModalOn, id }: DeleteCartModalProps) => {
  const navigate = useNavigate();

  const handleClickDeleteModal = async () => {
    await api
      .delete(`/cart/${id}`)
      .then(() => {
        setModalOn(false);
        window.location.reload();
      })
      .catch((err) => {
        console.log(err);
        navigate('/error');
      });
  };
  return (
    <ModalPortal>
      <EscapeModalForm
        onClose={() => setModalOn(false)}
        pageName={'CartPage'}
        title={'์„ ํƒํ•œ ์ƒํ’ˆ์„ ์‚ญ์ œํ•˜์‹œ๋‚˜์š”?'}
        subTitle={'์‚ญ์ œํ•˜์‹œ๋ฉด ๊ฒฐ์ œ ๋Œ€์ƒ์—์„œ'}
        subTitle2={'ํ•ด๋‹น ์ƒํ’ˆ์ด ์ œ์™ธ๋ผ์š”'}
        continueBtn={'์‚ญ์ œํ•˜๊ธฐ'}
        stopBtn={'๊ทธ๋งŒํ•˜๊ธฐ'}
        continueBtnFunc={() => handleClickDeleteModal()}
      />
    </ModalPortal>
  );
};
const CartItemSection = ({
  items,
  handleClickQuantityButton,
}: {
  items: CartItemProps[];
  handleClickQuantityButton: (price: number) => void;
}) => {
  return (
    <St.ItemSection>
      {items.map((item) => (
        <CartItem
          key={item.stickerId}
          id={item.cartId}
          name={item.name}
          price={item.discountPrice}
          originalPrice={item.price}
          mainImageUrl={item.mainImageUrl}
          count={item.count}
          handleClickQuantityButton={handleClickQuantityButton}
        />
      ))}
    </St.ItemSection>
  );
};

ํ•ด๋‹น item ์‚ญ์ œ๋ฅผ ํ•ด์•ผ ํ•จ.

โ˜€๏ธ ์Šคํฌ๋ฆฐ์ƒท / GIF / ํ™”๋ฉด ๋…นํ™”

TATTOUR - Chrome 2023-12-06 16-59-39

๐Ÿ“š Reference

  • ๊ตฌํ˜„์— ์ฐธ๊ณ ํ•œ ๋งํฌ (ํ•„์š”ํ•œ ๊ฒฝ์šฐ๋งŒ ์ž‘์„ฑํ•˜๊ณ  ์—†์œผ๋ฉด ์ง€์šฐ๊ธฐ)

gunom added 21 commits December 5, 2023 01:16
Copy link
Member

@Arooming Arooming left a comment

Choose a reason for hiding this comment

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

ํ—‰ ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹น LGTM
ํŠธ๋Ÿฌ๋ธ”์ŠˆํŒ…์— ์ ์–ด์ฃผ์‹  ๋‚ด์šฉ์€ ์ €๋„ ๋” ์ƒ๊ฐํ•ด๋ณผ๊ฒŒ์šฉ

Comment on lines +59 to +61
<>
<EmptyView />
</>
Copy link
Member

Choose a reason for hiding this comment

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

์š”๊ฑฐ ๋นˆํƒœ๊ทธ ์—†์–ด๋„ ๋˜์ง€ ์•Š์„๊นŒ์š”??

Comment on lines +47 to +50
const debouncedFetchData = useCallback(
debounce((newQuantity) => fetchData(newQuantity), 300),
[],
);
Copy link
Member

Choose a reason for hiding this comment

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

์˜ค... ๊ตฟ!

window.location.reload();
})
.catch((err) => {
console.log(err);
Copy link
Member

Choose a reason for hiding this comment

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

์ฝ˜์†” ์ฝ˜์†”

setResponse(data.data);
})
.catch(err => {
setError(err);
Copy link
Member

Choose a reason for hiding this comment

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

์—ฌ๊ธฐ๋„ ์—๋ŸฌํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๊ฒŒ ํ•ด์ค˜์•ผํ•˜์ง€ ์•Š๋‚˜์šฉ?!

Suggested change
setError(err);
setError(err);
navigate('/error')

Copy link
Member

@lydiacho lydiacho left a comment

Choose a reason for hiding this comment

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

ํŠธ๋Ÿฌ๋ธ” ์ŠˆํŒ… ๊ด€๋ จ ์˜๊ฒฌ์€ ํ•ด๋‹น ๋ถ€๋ถ„์— ๋ฆฌ๋ทฐ ๋‚จ๊ฒจ๋‘์—ˆ์Šต๋‹ˆ๋‹ค
์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค ! ๐Ÿ’ฏ

Comment on lines +28 to +30
const { response, error, loading } = useGetOrdersheet(
state ? state : ({ stickerId: 0, count: 0 } as OrderSheetRequest),
);
Copy link
Member

Choose a reason for hiding this comment

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

state๋ฅผ ๋„˜๊ฒจ์ฃผ์ง€ ์•Š๊ณ  ๋ณ„๋„ ์ธ์ž ์—†์ด useGetOrdersheet์„ ํ˜ธ์ถœํ•˜๋ฉด ์—๋Ÿฌ๋‚˜๋Š”๊ฑธ๊นŒ์š”? ์ €๋Š” ์ฒ˜์Œ์— ์ด๋ ‡๊ฒŒ ๊ฐ๊ฐ 0์œผ๋กœ ์ง€์ •ํ•ด์ค„ ํ•„์š” ์—†์ด ์•„์˜ˆ state ์•ˆ๋„˜๊ฒจ์ฃผ๋ฉด ์•Œ์•„์„œ undefined ์ฒ˜๋ฆฌ ๋ ๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ !

Copy link
Contributor Author

Choose a reason for hiding this comment

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

์กฐ๊ฑด ํ•œ๋ฒˆ ์ˆ˜์ •ํ•ด๋ณผ๊ฒŒ์š”~

Comment on lines +43 to +47
const url =
stickerId && count
? `/order/ordersheet?stickerId=${stickerId}&count=${count}`
: '/order/ordersheet';

Copy link
Member

Choose a reason for hiding this comment

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

๐Ÿ‘

Comment on lines +34 to +36
{orderSheetStickersRes.map((item) => {
return <ProductInfo key={item.name} orderSheetSticker={item} />;
})}
Copy link
Member

Choose a reason for hiding this comment

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

์•„ ์ด๋ถ€๋ถ„์„ ๋ฐ˜์˜ ์•ˆํ–ˆ๋„ค์š” ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

Comment on lines +11 to +25
const DeleteCartModal = ({ setModalOn, id }: DeleteCartModalProps) => {
const navigate = useNavigate();

const handleClickDeleteModal = async () => {
await api
.delete(`/cart/${id}`)
.then(() => {
setModalOn(false);
window.location.reload();
})
.catch((err) => {
console.log(err);
navigate('/error');
});
};
Copy link
Member

Choose a reason for hiding this comment

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

ํŠธ๋Ÿฌ๋ธ” ์ŠˆํŒ… ๊ด€๋ จํ•ด์„œ ์ฒจ์–ธํ•˜์ž๋ฉด
๊ตฌํ˜„ ์˜์ƒ ๋ณด๋‹ˆ๊นŒ ์‚ฌ์šฉ์ž ๊ฒฝํ—˜์ด ๋งŽ์ด ๋–จ์–ด์ง€๊ฒŒ ๋˜๋Š” ๊ฒƒ ๊ฐ™์•„์„œ reload๋Š” ์•ˆ์“ฐ๋Š”๊ฒŒ ๋งž๋Š” ๊ฒƒ ๊ฐ™๊ณ  ๊ทธ๋ž˜์„œ ์ผ๋‹จ state๋กœ ๊ด€๋ฆฌํ•˜๋Š”๊ฒŒ ๊ดœ์ฐฎ์•„ ๋ณด์ด๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค (๋ฌผ๋ก  ๋” ์ข‹์€ ๋ฐฉ๋ฒ•์ด ์žˆ์œผ๋ฉด ์ข‹๊ฒ ์ง€๋งŒ)
์–ด์ฐจํ”ผ ์ˆ˜๋Ÿ‰๋„ CartItem์—์„œ state๋กœ ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ์œผ๋‹ˆ๊นŒ ์žฅ๋ฐ”๊ตฌ๋‹ˆ ์•„์ดํ…œ ์ •๋ณด state๋ฅผ ๊ฐ์ฒด๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์„œ ํ•œ state๋กœ ์ˆ˜๋Ÿ‰ ์กฐ์ ˆ ๋ฐ ์‚ญ์ œ ๋‹ค ๊ด€๋ฆฌํ•ด์ฃผ๋Š”๊ฑด ์–ด๋–จ๊นŒ์š”?
quantity๋ž‘ item list ๋”ฐ๋กœ ๊ด€๋ฆฌํ•˜์ง€ ๋ง๊ณ  ์•„์ดํ…œ id๋ž‘ quantity ํ”„๋กœํผํ‹ฐ ๋‘๊ฐœ๋งŒ ๊ฐ€์ ธ๊ฐ€์„œ ์‚ญ์ œ ํ†ต์‹  ์„ฑ๊ณต ์‹œ์— quantity 0์œผ๋กœ ๋งŒ๋“ค๊ณ  0์ธ ์• ๋“ค์€ ๋ Œ๋”๋ง ์•ˆํ•˜๋Š” ์‹์œผ๋กœ ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์ด ์ผ๋‹จ ๋‹น์žฅ ๋– ์˜ค๋ฅด๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ๋ฐฉ๋ฒ•? ์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
(์–ด์ฐจํ”ผ ์žฅ๋ฐ”๊ตฌ๋‹ˆ ํŽ˜์ด์ง€ ๋“ค์–ด์˜ฌ ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋ชฉ๋ก ๋ถˆ๋Ÿฌ์™€์„œ state ์ดˆ๊ธฐํ™” ์‹œ์ผœ์ค„ ํ…Œ๋‹ˆ๊นŒ ์‚ญ์ œํ•œ ์•„์ดํ…œ์ด quantity 0 ์ƒํƒœ๋กœ ์ •๋ณด ๋‚จ์•„์žˆ๋Š”๊ฑด ๋ณ„๋กœ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์•„๋„ ๋  ๊ฒƒ ๊ฐ™์•„์š”!)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

SP2๋กœ ๊ฐ€์ ธ๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค. ํ˜น์€ ๋ฉด์ ‘ ๋๋‚˜๊ณ ,,,

Copy link
Member

@Yeonseo-Jo Yeonseo-Jo left a comment

Choose a reason for hiding this comment

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

์ˆ˜๊ณ ํ•˜์…จ์Šด๋‹ˆ๋‹ค!

Comment on lines +18 to +25
id: number;
mainImageUrl: string;
name: string;
price: number;
originalPrice: number;
count: number;
handleClickQuantityButton: (price: number) => void;
}) => {
Copy link
Member

Choose a reason for hiding this comment

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

interface๋กœ ๋นผ์ฃผ๋Š”๊ฒŒ ๊ฐ€๋…์„ฑ์ด ์ข‹์„๊ฒƒ ๊ฐ™์•„์šฉ

Comment on lines +47 to +49
const debouncedFetchData = useCallback(
debounce((newQuantity) => fetchData(newQuantity), 300),
[],
Copy link
Member

Choose a reason for hiding this comment

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

์˜ค ์ข‹์€ ์ฝ”๋“œ ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค

Comment on lines +8 to +11
}: {
items: CartItemProps[];
handleClickQuantityButton: (price: number) => void;
}) => {
Copy link
Member

Choose a reason for hiding this comment

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

์—ฌ๊ธฐ๋„ ๋ณต์žกํ•œ props type๋“ค์€ interface๋กœ ๋”ฐ๋กœ ๋นผ์ฃผ๋ฉด ๋” ๊ฐ€๋…์„ฑ์ด ์ข‹์„๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹น

Comment on lines +10 to +18
export interface CartItemProps {
stickerId: number;
id: number;
mainImageUrl: string;
name: string;
price: number;
discountPrice: number;
count: number;
}
Copy link
Member

Choose a reason for hiding this comment

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

์š”๊ฑฐ ์œ„์— props๋“ค์˜ type์œผ๋กœ๋„ ์“ฐ์ด๋Š”๊ฒƒ ๊ฐ™์€๋ฐ type ํŒŒ์ผ์— ๋นผ๋‘๊ณ  extends ํ•ด์„œ ์“ฐ๊ฑฐ๋‚˜ ํ•˜๋ฉด ์ข‹์„๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!

Comment on lines +18 to +19
setModalOn(false);
window.location.reload();
Copy link
Member

Choose a reason for hiding this comment

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

์ €๋„ ์Šนํฌ ์˜๊ฒฌ์— ๋™์˜ํ•ฉ๋‹ˆ๋‹ค
reload๋ณด๋‹ค๋Š” state๋กœ ๊ด€๋ฆฌํ•˜๋Š”๊ฒŒ ์ข‹์•„ ๋ณด์—ฌ์š”!

@gunom gunom merged commit 755827e into feature/SP1 Dec 7, 2023
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