| 1 |
/** |
| 2 |
* Widget button callback. |
| 3 |
* |
| 4 |
* @package Packetery |
| 5 |
*/ |
| 6 |
|
| 7 |
import { useCallback } from 'react'; |
| 8 |
import { fillRateAttrValues } from './fillRateAttrValues'; |
| 9 |
import { stringifyOptions } from "./stringifyOptions"; |
| 10 |
import { getShippingMethodOptionId } from "./getShippingMethodOptionId"; |
| 11 |
|
| 12 |
export const useOnWidgetButtonClicked = ( |
| 13 |
packetaShippingRate, |
| 14 |
settings, |
| 15 |
dynamicSettings, |
| 16 |
setViewState, |
| 17 |
shippingAddress, |
| 18 |
cartItemsWeight, |
| 19 |
) => { |
| 20 |
const { |
| 21 |
carrierConfig, |
| 22 |
language, |
| 23 |
packeteryApiKey, |
| 24 |
appIdentity, |
| 25 |
nonce, |
| 26 |
saveSelectedPickupPointUrl, |
| 27 |
pickupPointAttrs, |
| 28 |
} = settings; |
| 29 |
|
| 30 |
const onWidgetButtonClicked = useCallback( () => { |
| 31 |
const rateId = getShippingMethodOptionId( packetaShippingRate.rate_id ); |
| 32 |
|
| 33 |
let weight = +( cartItemsWeight / 1000 ).toFixed( 2 ); |
| 34 |
let widgetOptions = { language, appIdentity, weight }; |
| 35 |
widgetOptions.country = shippingAddress.country.toLowerCase(); |
| 36 |
if ( carrierConfig[ rateId ].carriers ) { |
| 37 |
widgetOptions.carriers = carrierConfig[ rateId ].carriers; |
| 38 |
} |
| 39 |
if ( carrierConfig[ rateId ].vendors ) { |
| 40 |
widgetOptions.vendors = carrierConfig[ rateId ].vendors; |
| 41 |
} |
| 42 |
|
| 43 |
if ( dynamicSettings ) { |
| 44 |
if ( dynamicSettings.isAgeVerificationRequired ) { |
| 45 |
widgetOptions.livePickupPoint = true; // Pickup points with real person only. |
| 46 |
} |
| 47 |
|
| 48 |
if ( dynamicSettings.biggestProductSize ) { |
| 49 |
if ( dynamicSettings.biggestProductSize.length ) { |
| 50 |
widgetOptions.length = dynamicSettings.biggestProductSize.length; |
| 51 |
} |
| 52 |
if ( dynamicSettings.biggestProductSize.width ) { |
| 53 |
widgetOptions.width = dynamicSettings.biggestProductSize.width; |
| 54 |
} |
| 55 |
if ( dynamicSettings.biggestProductSize.depth ) { |
| 56 |
widgetOptions.depth = dynamicSettings.biggestProductSize.depth; |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
console.log( 'Pickup point widget options: apiKey: ' + packeteryApiKey + ', ' + stringifyOptions( widgetOptions ) ); |
| 62 |
|
| 63 |
// Storage to store settings of all Packeta shipping methods displayed at checkout. |
| 64 |
let rateAttrValues = {}; |
| 65 |
|
| 66 |
Packeta.Widget.pick( |
| 67 |
packeteryApiKey, |
| 68 |
( pickupPoint ) => { |
| 69 |
if ( ! pickupPoint ) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
setViewState( { pickupPoint } ); |
| 74 |
|
| 75 |
rateAttrValues = fillRateAttrValues( rateId, pickupPointAttrs, pickupPoint, rateAttrValues ); |
| 76 |
let pickupPointDataToSave = rateAttrValues[ rateId ]; |
| 77 |
pickupPointDataToSave.packetery_rate_id = rateId; |
| 78 |
|
| 79 |
fetch( saveSelectedPickupPointUrl, { |
| 80 |
method: 'POST', |
| 81 |
headers: { |
| 82 |
'Content-Type': 'application/x-www-form-urlencoded', |
| 83 |
'X-WP-Nonce': nonce, |
| 84 |
}, |
| 85 |
body: new URLSearchParams( pickupPointDataToSave ), |
| 86 |
} ) |
| 87 |
.then( ( response ) => { |
| 88 |
if ( ! response.ok ) { |
| 89 |
throw new Error( 'HTTP error ' + response.status ); |
| 90 |
} |
| 91 |
} ) |
| 92 |
.catch( ( error ) => { |
| 93 |
console.error( |
| 94 |
'Failed to save pickup point data:', |
| 95 |
error |
| 96 |
); |
| 97 |
} ); |
| 98 |
}, |
| 99 |
widgetOptions |
| 100 |
); |
| 101 |
}, [ packetaShippingRate, dynamicSettings ] ); |
| 102 |
|
| 103 |
return onWidgetButtonClicked; |
| 104 |
}; |
| 105 |
|