From 5258abb8f2322b2c3f1d2751285b57b97b1dc295 Mon Sep 17 00:00:00 2001 From: AlexV Date: Mon, 25 Nov 2024 11:23:16 +0700 Subject: [PATCH 1/3] fix: displaying of validation errors --- src/Controllers/IntakeController.php | 37 ++++++++++++++++++++++------ src/Controllers/OrderController.php | 25 +++++++++++++++++-- 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/src/Controllers/IntakeController.php b/src/Controllers/IntakeController.php index edd4042..38b710c 100644 --- a/src/Controllers/IntakeController.php +++ b/src/Controllers/IntakeController.php @@ -17,7 +17,6 @@ use Cdek\Exceptions\External\InvalidRequestException; use Cdek\Model\Order; use Cdek\Model\Tariff; - use DateTimeImmutable; use JsonException; class IntakeController @@ -33,12 +32,28 @@ public static function create(): void wp_die(-2, 403); } - $id = (int)$_REQUEST['id']; + $id = (int)wp_unslash($_REQUEST['id']); + + try { + $order = new Order($id); + } catch (ExceptionContract $e) { + AdminOrderBox::createOrderMetaBox( + $id, + ['errors' => [$e->getMessage()]], + ); + + wp_die(); + } try { $body = json_decode(file_get_contents('php://input'), true, 512, JSON_THROW_ON_ERROR); } catch (JsonException $e) { - wp_die($e->getMessage()); + AdminOrderBox::createOrderMetaBox( + $order, + ['errors' => [$e->getMessage()]], + ); + + wp_die(); } $val = rest_validate_object_value_from_schema($body, [ @@ -84,10 +99,13 @@ public static function create(): void ], 'intake'); if (is_wp_error($val)) { - wp_die($val); - } + AdminOrderBox::createOrderMetaBox( + $order, + ['errors' => $val->get_error_messages()], + ); - $order = new Order($id); + wp_die(); + } $shipping = $order->getShipping(); $tariff = $shipping !== null ? $shipping->tariff : null; @@ -105,7 +123,12 @@ public static function create(): void ], 'intake'); if (is_wp_error($val)) { - wp_die($val); + AdminOrderBox::createOrderMetaBox( + $order, + ['errors' => $val->get_error_messages()], + ); + + wp_die(); } } diff --git a/src/Controllers/OrderController.php b/src/Controllers/OrderController.php index f6cbd6a..3bc46b6 100644 --- a/src/Controllers/OrderController.php +++ b/src/Controllers/OrderController.php @@ -59,10 +59,26 @@ public static function create(): void $id = (int)wp_unslash($_REQUEST['id']); + try { + $order = new Order($id); + } catch (ExceptionContract $e) { + AdminOrderBox::createOrderMetaBox( + $id, + ['errors' => [$e->getMessage()]], + ); + + wp_die(); + } + try { $body = json_decode(file_get_contents('php://input'), true, 512, JSON_THROW_ON_ERROR); } catch (JsonException $e) { - wp_die($e->getMessage()); + AdminOrderBox::createOrderMetaBox( + $order, + ['errors' => [$e->getMessage()]], + ); + + wp_die(); } $val = rest_validate_array_value_from_schema($body, [ @@ -109,7 +125,12 @@ public static function create(): void ], 'packages'); if (is_wp_error($val)) { - wp_die($val); + AdminOrderBox::createOrderMetaBox( + $order, + ['errors' => $val->get_error_messages()], + ); + + wp_die(); } try { From 89a98ecef74f6154e9545a401ad378a4ff15a982 Mon Sep 17 00:00:00 2001 From: AlexV Date: Mon, 25 Nov 2024 11:58:46 +0700 Subject: [PATCH 2/3] fix: multi-seater nan --- src/Frontend/AdminOrder/index.js | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/Frontend/AdminOrder/index.js b/src/Frontend/AdminOrder/index.js index a2f6df1..8254534 100644 --- a/src/Frontend/AdminOrder/index.js +++ b/src/Frontend/AdminOrder/index.js @@ -65,10 +65,29 @@ $(document).ready(() => { e.preventDefault(); e.stopPropagation(); + const len = metaBox.find('input[name=length]').val(); + const wid = metaBox.find('input[name=width]').val(); + const hei = metaBox.find('input[name=height]').val(); + + if (isNaN(len) || len === '') { + alert(__('Package length not specified', 'cdekdelivery')); + return; + } + + if (isNaN(wid) || wid === '') { + alert(__('Package width not specified', 'cdekdelivery')); + return; + } + + if (isNaN(hei) || hei === '') { + alert(__('Package height not specified', 'cdekdelivery')); + return; + } + const packageData = { - length: parseInt(metaBox.find('input[name=length]').val()), - width: parseInt(metaBox.find('input[name=width]').val()), - height: parseInt(metaBox.find('input[name=height]').val()), + length: parseInt(len), + width: parseInt(wid), + height: parseInt(hei), items: metaBox.find(`.item[data-id][aria-hidden=false]`) .map((i, e) => ({ id: parseInt(e.dataset.id), @@ -78,15 +97,15 @@ $(document).ready(() => { }; if (packageData.length < 1) { - alert(__('Package length not specified', 'cdekdelivery')); + alert(__('Package length should be greater 1', 'cdekdelivery')); return; } if (packageData.width < 1) { - alert(__('Package width not specified', 'cdekdelivery')); + alert(__('Package width be greater 1', 'cdekdelivery')); return; } if (packageData.height < 1) { - alert(__('Package height not specified', 'cdekdelivery')); + alert(__('Package height be greater 1', 'cdekdelivery')); return; } if (packageData.items.length < 1) { From bcf86cf2f6c7014f1edb0f8045922c5fc744774e Mon Sep 17 00:00:00 2001 From: AlexV Date: Mon, 25 Nov 2024 14:07:53 +0700 Subject: [PATCH 3/3] feat: move checkout points to script --- package.json | 3 +- src/Frontend/CheckoutMapShortcode/index.js | 102 ++++++++++----------- src/Transport/HttpClient.php | 2 +- src/UI/CdekWidget.php | 6 +- src/UI/CheckoutMap.php | 16 +--- 5 files changed, 63 insertions(+), 66 deletions(-) diff --git a/package.json b/package.json index 344724f..85be9a8 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,8 @@ "lint:css": "wp-scripts lint-style", "lint:js": "wp-scripts lint-js", "start": "wp-scripts start", - "env": "wp-env start --update" + "env": "wp-env start --update", + "destroy": "wp-env destroy" }, "dependencies": { "@cdek-it/widget": "^3.11.0", diff --git a/src/Frontend/CheckoutMapShortcode/index.js b/src/Frontend/CheckoutMapShortcode/index.js index 47e56f1..949f7a7 100644 --- a/src/Frontend/CheckoutMapShortcode/index.js +++ b/src/Frontend/CheckoutMapShortcode/index.js @@ -7,19 +7,19 @@ import { debounce } from 'lodash'; const billingCityInput = $('#billing_city'); const shippingCityInput = $('#shipping_city'); let widget = null; +let el; - - -if ((billingCityInput.val() || '') !== '' || (shippingCityInput.val() || '') !== '') { +if ((billingCityInput.val() || '') !== '' || (shippingCityInput.val() || '') !== + '') { console.debug('[CDEK-MAP] City has value, initiating checkout update'); $(document.body).trigger('update_checkout'); } -const closeMap = (el, errorMessage = null) => { +const closeMap = (e, errorMessage = null) => { console.debug('[CDEK-MAP] Removing selected office info'); $('.cdek-office-info').remove(); - el.html(__('Choose pick-up', 'cdekdelivery')); + e.find('a').html(__('Choose pick-up', 'cdekdelivery')); $('.cdek-office-code').val(''); if (widget !== null) { @@ -35,19 +35,18 @@ const closeMap = (el, errorMessage = null) => { } }; -let el; - const onChoose = (_type, _tariff, address) => { $('.cdek-office-code').val(address.code); - el.html(__('Re-select pick-up', 'cdekdelivery')); + el.find('a').html(__('Re-select pick-up', 'cdekdelivery')); + const officeInfo = el.parent().children('.cdek-office-info'); if (officeInfo.length === 0) { - el.before( - `
${address.name}
`); + el.before($('
').text(address.name)); } else { - officeInfo.html(`${address.name}`); + officeInfo.text(address.name); } - if ($('.cdek-office-code').data('map-auto-close')) { + + if (window.cdek.close) { widget.close(); } }; @@ -74,48 +73,47 @@ $(document.body) widget.clearSelection(); } }) - .on('change', '.shipping_method', () => { - $(document.body).trigger('update_checkout'); - }) + .on('change', '.shipping_method', + () => $(document.body).trigger('update_checkout')) .on('click', '.open-pvz-btn', null, (e) => { - el = $(e.target); + el = e.target.tagName === 'A' ? $(e.target.parentElement) : $(e.target); closeMap(el); - const points = el.data('points'); - console.debug('[CDEK-MAP] Got points from backend:', points); - - if (typeof points !== 'object') { - console.error('[CDEK_MAP] backend points not object'); - closeMap(el, - __('CDEK was unable to load the list of available pickup points, please select another delivery method', 'cdekdelivery')); - - return; - } else if (!points.length) { - console.warn('[CDEK_MAP] backend points are empty'); - closeMap(el, - __('There are no CDEK pick-up points available in this direction, please select another delivery method') - ); - - return; + try { + const points = JSON.parse(el.find('script').text()); + console.debug('[CDEK-MAP] Got points from backend', points); + + if (!points.length) { + console.warn('[CDEK-MAP] Backend points are empty'); + closeMap(el, __( + 'There are no CDEK pick-up points available in this direction, please select another delivery method')); + + return; + } + + if (widget === null) { + widget = new cdekWidget({ + apiKey: window.cdek.key, + popup: true, + debug: true, + lang: window.cdek.lang, + defaultLocation: el.data('city'), + officesRaw: points, + hideDeliveryOptions: { + door: true, + }, + onChoose, + }); + } else { + widget.updateOfficesRaw(points); + widget.updateLocation(el.data('city')); + } + + widget.open(); + } catch (SyntaxError) { + console.error('[CDEK-MAP] SyntaxError during points parse'); + + closeMap(el, __( + 'There are no CDEK pick-up points available in this direction, please select another delivery method')); } - - if (widget === null) { - widget = new cdekWidget({ - apiKey: window.cdek.apiKey, - popup: true, - debug: true, - lang: el.data('lang'), - defaultLocation: el.data('city'), - officesRaw: points, - hideDeliveryOptions: { - door: true, - }, - onChoose, - }); - } else { - widget.updateOfficesRaw(points); - widget.updateLocation(el.data('city')); - } - - widget.open(); }); diff --git a/src/Transport/HttpClient.php b/src/Transport/HttpClient.php index 46ace72..217e337 100644 --- a/src/Transport/HttpClient.php +++ b/src/Transport/HttpClient.php @@ -64,7 +64,7 @@ public static function sendJsonRequest( } if ($result->isClientError()) { - throw new HttpClientException($result->error()); + throw new HttpClientException($result->error() ?? []); } return $result; diff --git a/src/UI/CdekWidget.php b/src/UI/CdekWidget.php index 1e70ade..615a42d 100644 --- a/src/UI/CdekWidget.php +++ b/src/UI/CdekWidget.php @@ -21,8 +21,12 @@ public static function registerScripts(): void { wp_register_script('cdek-widget', Loader::getPluginUrl('build/cdek-widget.umd.js')); + $shipping = ShippingMethod::factory(); + wp_localize_script('cdek-widget', 'cdek', [ - 'apiKey' => ShippingMethod::factory()->yandex_map_api_key, + 'key' => $shipping->yandex_map_api_key, + 'close' => $shipping->map_auto_close, + 'lang' => mb_strpos(get_user_locale(), 'en') === 0 ? 'eng' : 'rus', ]); } diff --git a/src/UI/CheckoutMap.php b/src/UI/CheckoutMap.php index b78c41f..cfa6f9a 100644 --- a/src/UI/CheckoutMap.php +++ b/src/UI/CheckoutMap.php @@ -13,7 +13,6 @@ use Cdek\Config; use Cdek\Helpers\CheckoutHelper; use Cdek\Model\Tariff; - use Cdek\ShippingMethod; class CheckoutMap { @@ -34,19 +33,14 @@ public function __invoke($shippingMethodCurrent): void $city = $api->cityCodeGet($cityInput, $postcodeInput); - $points = $city !== null ? $api->officeListRaw($city) : '[]'; - - echo '
'. + ''. esc_html__('Choose pick-up', 'cdekdelivery'). - '
'; + ''; } private function isTariffDestinationCdekOffice($shippingMethodCurrent): bool