UNPKG

@commercelayer/react-components

Version:
2 lines 1.69 kB
"use client"; "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ShippingMethod=ShippingMethod;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),ShippingMethodChildrenContext_1=tslib_1.__importDefault(require("../../context/ShippingMethodChildrenContext")),ShipmentChildrenContext_1=tslib_1.__importDefault(require("../../context/ShipmentChildrenContext")),isEmpty_1=tslib_1.__importDefault(require("lodash/isEmpty"));function ShippingMethod(props){const{children,readonly,emptyText="There are not any shipping method available"}=props,{shippingMethods,currentShippingMethodId,deliveryLeadTimes,shipment}=(0,react_1.useContext)(ShipmentChildrenContext_1.default),[items,setItems]=(0,react_1.useState)([]);(0,react_1.useEffect)(()=>{const methods=shippingMethods?.filter(s=>readonly?s.id===currentShippingMethodId:!0).map((shippingMethod,k)=>{var _a;const[deliveryLeadTimeForShipment]=(_a=deliveryLeadTimes?.filter(delivery=>{var _a2;const deliveryShippingMethodId=(_a2=delivery.shipping_method)===null||_a2===void 0?void 0:_a2.id;return shippingMethod.id===deliveryShippingMethodId}))!==null&&_a!==void 0?_a:[],shippingProps={shipmentId:shipment?.id,shippingMethod,currentShippingMethodId,deliveryLeadTimeForShipment};return(0,jsx_runtime_1.jsx)(ShippingMethodChildrenContext_1.default.Provider,{value:shippingProps,children},k)});return methods&&setItems(methods),()=>{setItems([])}},[currentShippingMethodId,deliveryLeadTimes,shippingMethods]);const components=!(0,isEmpty_1.default)(items)&&items||emptyText;return(0,jsx_runtime_1.jsx)(jsx_runtime_1.Fragment,{children:components})}exports.default=ShippingMethod;