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

refactor : 전체 점검 후 수정 #145

Open
ruddnjs3769 opened this issue Jun 28, 2023 · 3 comments
Open

refactor : 전체 점검 후 수정 #145

ruddnjs3769 opened this issue Jun 28, 2023 · 3 comments
Assignees
Labels
완료 완료했을때

Comments

@ruddnjs3769
Copy link
Contributor

ㅜㅜ

@ruddnjs3769 ruddnjs3769 self-assigned this Jun 28, 2023
@ruddnjs3769
Copy link
Contributor Author

ruddnjs3769 commented Jun 28, 2023

orderComplete 페이지에서, 주문한 상품 내역이 안나오는 오류를 수정했습니다.
전체 코드

import React, { useEffect, useState } from 'react'
import styles from './index.module.scss'
import PayProcessFlow from '@/components/payment/PayProcessFlow'
import Btn from '@/components/payment/Btn'
import { UserTransactionDetails } from '@/types/product'
import { getTransactionDetails } from '@/apis/payment/product'
import Loading from '@/components/payment/Loading'
import useUserInfo from '@/hooks/useUserInfo'
import useCartItems from '@/hooks/useCartItems'

export default function OrderComplete() {
  const [transactionDetails, setTransactionDetails] = useState<UserTransactionDetails>([])
  const [isLoading, setIsLoading] = useState<boolean>(false)

  const accessToken = localStorage.getItem('token') || ''
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  const [userInfo, _isLoggedIn, _logout] = useUserInfo()
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  const [cartItems, _addcartItems, removeCartItemsByUser, _removeOneCartItemByUser] = useCartItems(userInfo)

  useEffect(() => {
    // 랜딩 시 details 저장
    matchedTransactionDetails().then(() => {
      removeCartItemsByUser(userInfo)
    })
  }, [userInfo])

  const matchedTransactionDetails = async (): Promise<void> => {
    // 제품 전체 거래(구매) 내역 API 호출 또는 dummy 데이터 사용
    try {
      setIsLoading(true)
      const transactionDetails: UserTransactionDetails = await getTransactionDetails(accessToken)

      // 장바구니에 있는 제품과 비교하여 매칭된 거래 내역 필터링
      const matchedDetails = transactionDetails.filter((detail) =>
        cartItems.some((item) => item.id === detail.product.productId)
      )
      setTransactionDetails(matchedDetails)
      setIsLoading(false)
    } catch (error) {
      console.error(error)
    }
  }

  return (
    <>
      <PayProcessFlow />
      {isLoading ? (
        <Loading color={'#666666'} />
      ) : (
        <div className={styles.orderer}>
          <div className={styles.orderInfo}>주문 완료</div>
          {transactionDetails.map((detail, index) => (
            <div key={index} className={styles.info}>
              <div className={styles.titleContainer}>
                <div className={styles.title}>
                  <span>주문 번호</span>
                </div>
                <div className={styles.title}>
                  <span>상품명</span>
                </div>
              </div>
              <div className={styles.contentContainer}>
                <div className={styles.content}>
                  <span>{detail.detailId}</span>
                </div>
                <div className={styles.content}>
                  <span>{detail.product.title}</span>
                </div>
              </div>
            </div>
          ))}
        </div>
      )}
      <div className={styles.btnContainer}>
        <Btn text="계속 쇼핑하기" targetURL="/" />
        <Btn text="주문내역 확인하기" targetURL="/user/:username" />
      </div>
    </>
  )
}

수정 이전

  useEffect(() => {
    // 랜딩 시 details 저장
    matchedTransactionDetails()
    // 장바구니에 담겨있던 제품들을 삭제
    removeCartItemsByUser(userInfo)
  }, [])

기존 코드는 비동기 완료 이전에 cartItems가 삭제되면서 matchedTransactionDetails 호출 중에 비교할 cartItems가 빈 배열로 나타남. matchedTransactionDetails 완료 이후에 removeCartItemsByUser가 실행되도록 then메서드 사용.
또한, cartItems는 customHooks인 useCartItems에서 userInfo를 받아오게 되면 활성화되는데, 따라서 useEffect 감시 데이터에도 userInfo를 추가해줌.

수정 이후

  useEffect(() => {
  // 랜딩 시 details 저장
  matchedTransactionDetails().then(() => {
    removeCartItemsByUser(userInfo)
  })
}, [userInfo])

@kse-seong-eun
Copy link
Contributor

오....빠르게 잡아내셨네여...! 고생하셨습니닷!!

@ruddnjs3769
Copy link
Contributor Author

orderComplete 페이지에서, 주문내역 확인하기 버튼의
<Btn text="주문내역 확인하기" targetURL="/user/:username" />
<Btn text="주문내역 확인하기" targetURL={/user/${userInfo.displayName}/getItemAll} />
로 바로 getItemAll로 이동하도록 수정했습니다.

@ruddnjs3769 ruddnjs3769 linked a pull request Jun 28, 2023 that will close this issue
@ruddnjs3769 ruddnjs3769 added the 완료 완료했을때 label Jun 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
완료 완료했을때
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants