Debian Codegraph @

addons/delivery_mondialrelay/static/src/components/mondialrelay_field.js

raw
1 import { registry } from "@web/core/registry";
2 import { loadJS } from "@web/core/assets";
3
4 // temporary for OnNoResultReturned bug
5 import { ThirdPartyScriptError } from "@web/core/errors/error_service";
6 const errorHandlerRegistry = registry.category("error_handlers");
7 import { Component, onWillRender, useEffect, useRef, useState, xml } from "@odoo/owl";
8 import { standardFieldProps } from "@web/views/fields/standard_field_props";
9
10 const MONDIALRELAY_SCRIPT_URL = "https://widget.mondialrelay.com/parcelshop-picker/jquery.plugin.mondialrelay.parcelshoppicker.min.js"
11
12 function corsIgnoredErrorHandler(env, error) {
13 if (error instanceof ThirdPartyScriptError) {
14 return true;
15 }
16 }
17
18 export class MondialRelayField extends Component {
19 static template = xml`<div t-if="enabled" t-ref="root"/>`;
20 static props = {...standardFieldProps};
21 setup() {
22 this.root = useRef("root");
23 this.state = useState({
24 libLoaded: false, // Whether the library is loaded or not
25 });
26 onWillRender(() => {
27 // Do nothing if the record is not of type mondial_relay
28 if (!this.enabled || this.state.libLoaded) {
29 return;
30 }
31 loadJS(MONDIALRELAY_SCRIPT_URL).then(() => {this.state.libLoaded = true});
32 });
33
34 useEffect(
35 (el) => {
36 if (!el) {
37 return;
38 }
39 this.insertWidget($(el));
40 },
41 () => [this.state.libLoaded && this.root.el],
42 )
43 }
44
45 get enabled() {
46 return this.props.record.data.is_mondialrelay;
47 }
48
49 insertWidget($el) {
50 const params = {
51 Target: "", // required but handled by OnParcelShopSelected
52 Brand: this.props.record.data.mondialrelay_brand,
53 ColLivMod: this.props.record.data.mondial_realy_colLivMod,
54 AllowedCountries: this.props.record.data.mondialrelay_allowed_countries,
55 PostCode: this.props.record.data.shipping_zip || '',
56 Country: this.props.record.data.shipping_country_code || '',
57 Responsive: true,
58 ShowResultsOnMap: true,
59 AutoSelect: this.props.record.data.mondialrelay_last_selected_id,
60 OnParcelShopSelected: (RelaySelected) => {
61 const values = JSON.stringify({
62 'id': RelaySelected.ID,
63 'name': RelaySelected.Nom,
64 'street': RelaySelected.Adresse1,
65 'street2': RelaySelected.Adresse2,
66 'zip': RelaySelected.CP,
67 'city': RelaySelected.Ville,
68 'country': RelaySelected.Pays,
69 });
70 this.props.record.update({ [this.props.name]: values });
71 },
72 OnNoResultReturned: () => {
73 // HACK while Mondial Relay fix his bug
74 // disable corsErrorHandler for 10 seconds
75 // If code postal not valid, it will crash with Cors Error:
76 // Cannot read property 'on' of undefined at u.MR_FitBounds
77 const randInt = Math.floor(Math.random() * 100);
78 errorHandlerRegistry.add("corsIgnoredErrorHandler" + randInt, corsIgnoredErrorHandler, {sequence: 10});
79 setTimeout(function () {
80 errorHandlerRegistry.remove("corsIgnoredErrorHandler" + randInt);
81 }, 10000);
82 },
83 };
84 $el.show();
85 $el.MR_ParcelShopPicker(params);
86 $el.trigger("MR_RebindMap");
87 }
88 }
89
90 export const mondialRelayField = {
91 component: MondialRelayField,
92 };
93
94 registry.category("fields").add("mondialrelay_relay", mondialRelayField);