@commercelayer/react-components
Version:
The Official Commerce Layer React Components
2 lines • 1.98 kB
JavaScript
"use client";
;Object.defineProperty(exports,"__esModule",{value:!0}),exports.getShipments=exports.setShipmentErrors=exports.shipmentInitialState=void 0,exports.setShippingMethod=setShippingMethod;const tslib_1=require("tslib"),baseReducer_1=tslib_1.__importDefault(require("../utils/baseReducer")),getSdk_1=tslib_1.__importDefault(require("../utils/getSdk")),canPlaceOrder_1=require("../utils/canPlaceOrder");exports.shipmentInitialState={errors:[]};const setShipmentErrors=(errors,dispatch)=>{dispatch&&dispatch({type:"setErrors",payload:{errors}})};exports.setShipmentErrors=setShipmentErrors;const getShipments=_a=>tslib_1.__awaiter(void 0,[_a],void 0,function*({order,dispatch,config}){try{const sdk=(0,getSdk_1.default)(config),shipments=order.shipments;let allDeliveryLeadTimes=[],currentPage=1,totalPages=1;do{const response=yield sdk.delivery_lead_times.list({include:["shipping_method","stock_location"],pageNumber:currentPage});allDeliveryLeadTimes=allDeliveryLeadTimes.concat(response),totalPages=response.meta.pageCount,currentPage++}while(currentPage<=totalPages);dispatch({type:"setShipments",payload:{shipments,deliveryLeadTimes:allDeliveryLeadTimes}})}catch(error){console.error(error)}});exports.getShipments=getShipments;function setShippingMethod(_a){return tslib_1.__awaiter(this,arguments,void 0,function*({config,shipmentId,shippingMethodId,getOrder,order}){try{if(order!=null&&!(0,canPlaceOrder_1.canPlaceOrder)(order))return{success:!1,order};if(shippingMethodId){const sdk=(0,getSdk_1.default)(config);if(yield sdk.shipments.update({id:shipmentId,shipping_method:sdk.shipping_methods.relationship(shippingMethodId)}),getOrder!=null&&order!=null)return{success:!0,order:yield getOrder(order.id)}}return{success:!1}}catch(error){return console.error(error),{success:!1}}})}const type=["setErrors","setShipments","setShippingMethod"],shipmentReducer=(state,reducer)=>(0,baseReducer_1.default)(state,reducer,type);exports.default=shipmentReducer;