diff options
Diffstat (limited to 'assets/js/cepis-woocommerce-blocks.js')
| -rw-r--r-- | assets/js/cepis-woocommerce-blocks.js | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/assets/js/cepis-woocommerce-blocks.js b/assets/js/cepis-woocommerce-blocks.js index bcbab93..4f4e2e3 100644 --- a/assets/js/cepis-woocommerce-blocks.js +++ b/assets/js/cepis-woocommerce-blocks.js @@ -131,6 +131,37 @@ } /** + * Say which municipality a CEP that does not exist would have been in. + * + * Replaces the notice already on screen — same id, so the block swaps the + * text rather than stacking a second error. The failure is reported + * immediately and refined a round trip later, because making the shopper wait + * on a second request before being told anything at all is worse than the + * message improving under them. + * + * Nothing here goes near `apply()`. A CEP the Correios do not have has no + * address, and the municipality in a 404 is a guess about which digit went + * wrong, not a place to ship to. + * + * @param {string} name Section name, `shipping` or `billing`. + * @param {string} digits The CEP that was not found. + * @param {string} shown The message already in the notice area. + */ + function explain(name, digits, shown) { + var helper = window.cepisWooCommerceFaixa; + + if (!helper) { + return; + } + + helper.message(digits, shown, function (message) { + if (message !== shown && progress[name].cep === digits) { + announce(message); + } + }); + } + + /** * Write a resolved address into the store. * * @param {string} name Section name, `shipping` or `billing`. @@ -185,7 +216,13 @@ if (progress[name].cep !== digits || error.code === "aborted") { return; } - announce(config.messages[error.code] || error.message); + + var shown = config.messages[error.code] || error.message; + announce(shown); + + if (error.code === "not_found") { + explain(name, digits, shown); + } }, ); } |