PluginProbe
Packeta / 2.1
Packeta v2.1
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / PacketaWidget / useOnWidgetButtonClicked.js

useOnWidgetButtonClicked.js in Packeta 2.1, at src/PacketaWidget/useOnWidgetButtonClicked.js

105 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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