[리액트] 타입스크립트 타입 매핑 적용 예시

Jang Seok Woo·2022년 12월 21일
0

리액트

목록 보기
58/58
type DeliveryStatus = 'ITEM_READY'
| 'DELIVERY_READY'
| 'DELIVERY_ING'
| 'DELIVERY_SUCCESS'
| 'DELIVERY_RETURN'
type ItemStatus = 'ITEM_DELIVERY'
| 'ITEM_DELIVERY_SUCCESS'
| 'ITEM_REFUND'
| 'ITEM_EXCHANGE'
| 'ITEM_RETURN'

const statusCodeTranslator = ({
  itemStatus,
  deliveryStatus
} : { itemStatus: ItemStatus, deliveryStatus: DeliveryStatus }) => {
  const itemStatusCode = {
    DELIVERY: 'ITEM_DELIVERY',
    DELIVERY_SUCCESS: 'ITEM_DELIVERY_SUCCESS',
    REFUND: 'ITEM_REFUND',
    EXCHANGE: 'ITEM_EXCHANGE',
    RETURN: 'ITEM_RETURN'
  };
  const itemStatusText: Record<ItemStatus, string> = {
    ITEM_DELIVERY: '배송 진행중',
    ITEM_DELIVERY_SUCCESS: '배송완료',
    ITEM_REFUND: '환불',
    ITEM_EXCHANGE: '교환',
    ITEM_RETURN: '반품'
  };

  const deliveryStatusText: Record<DeliveryStatus, string> = {
    ITEM_READY: '상품 준비중',
    DELIVERY_READY: '배송 준비중',
    DELIVERY_ING: '배송중',
    DELIVERY_SUCCESS: '배송완료',
    DELIVERY_RETURN: '반송(배송실패)',

  };

  if (itemStatus === itemStatusCode.DELIVERY || itemStatus === itemStatusCode.DELIVERY_SUCCESS) {
    return deliveryStatusText[deliveryStatus];
  } else return itemStatusText[itemStatus];
};
profile
https://github.com/jsw4215

0개의 댓글