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 / useOnHDWidgetButtonClicked.js

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

89 lines 2.6 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 useOnHDWidgetButtonClicked = (
13 packetaShippingRate,
14 settings,
15 dynamicSettings,
16 setViewState,
17 shippingAddress,
18 ) => {
19 const {
20 carrierConfig,
21 language,
22 packeteryApiKey,
23 appIdentity,
24 nonce,
25 saveValidatedAddressUrl,
26 homeDeliveryAttrs,
27 translations,
28 } = settings;
29
30 const onHDWidgetButtonClicked = useCallback( () => {
31 const rateId = getShippingMethodOptionId( packetaShippingRate.rate_id );
32
33 let widgetOptions = { language, appIdentity };
34 widgetOptions.layout = 'hd';
35 widgetOptions.country = shippingAddress.country.toLowerCase();
36 widgetOptions.street = shippingAddress.address_1;
37 widgetOptions.city = shippingAddress.city;
38 widgetOptions.postcode = shippingAddress.postcode;
39 widgetOptions.carrierId = carrierConfig[ rateId ].id;
40
41 console.log( 'Address widget options: apiKey: ' + packeteryApiKey + ', ' + stringifyOptions( widgetOptions ) );
42
43 // Storage to store settings of all Packeta shipping methods displayed at checkout.
44 let rateAttrValues = {};
45
46 Packeta.Widget.pick(
47 packeteryApiKey,
48 ( result ) => {
49 if ( ! result || ! result.address ) {
50 return;
51 }
52
53 if ( result.address.country !== widgetOptions.country ) {
54 setViewState( { deliveryAddressError: settings.translations.invalidAddressCountrySelected } );
55 return;
56 }
57
58 const deliveryAddressInfo = translations.deliveryAddressNotification + ' ' + result.address.name;
59 setViewState( { deliveryAddressInfo } );
60
61 rateAttrValues = fillRateAttrValues( rateId, homeDeliveryAttrs, result.address, rateAttrValues );
62 let homeDeliveryDataToSave = rateAttrValues[ rateId ];
63 homeDeliveryDataToSave.packetery_rate_id = rateId;
64 homeDeliveryDataToSave.packetery_address_isValidated = 1;
65
66 fetch( saveValidatedAddressUrl, {
67 method: 'POST',
68 headers: {
69 'Content-Type': 'application/x-www-form-urlencoded',
70 'X-WP-Nonce': nonce,
71 },
72 body: new URLSearchParams( homeDeliveryDataToSave ),
73 } )
74 .then( ( response ) => {
75 if ( ! response.ok ) {
76 throw new Error( 'HTTP error ' + response.status );
77 }
78 } )
79 .catch( ( error ) => {
80 console.error( 'Failed to save validated address data:', error );
81 } );
82 },
83 widgetOptions
84 );
85 }, [ packetaShippingRate, dynamicSettings ] );
86
87 return onHDWidgetButtonClicked;
88 };
89