UNPKG

@commercelayer/react-components

Version:
2 lines 1.46 kB
"use client"; import baseReducer from"../utils/baseReducer";import getSdk from"../utils/getSdk";import{canPlaceOrder}from"../utils/canPlaceOrder";export const shipmentInitialState={errors:[]},setShipmentErrors=(errors,dispatch)=>{dispatch&&dispatch({type:"setErrors",payload:{errors}})},getShipments=async({order,dispatch,config})=>{try{const sdk=getSdk(config),shipments=order.shipments;let allDeliveryLeadTimes=[],currentPage=1,totalPages=1;do{const response=await 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)}};export async function setShippingMethod({config,shipmentId,shippingMethodId,getOrder,order}){try{if(order!=null&&!canPlaceOrder(order))return{success:!1,order};if(shippingMethodId){const sdk=getSdk(config);if(await sdk.shipments.update({id:shipmentId,shipping_method:sdk.shipping_methods.relationship(shippingMethodId)}),getOrder!=null&&order!=null)return{success:!0,order:await getOrder(order.id)}}return{success:!1}}catch(error){return console.error(error),{success:!1}}}const type=["setErrors","setShipments","setShippingMethod"],shipmentReducer=(state,reducer)=>baseReducer(state,reducer,type);export default shipmentReducer;