From 4e7187bba02f466e84d689970f8b5993953e204e Mon Sep 17 00:00:00 2001 From: Samuel Degueldre Date: Mon, 7 Jun 2021 14:54:21 +0200 Subject: [PATCH] [REF] iap: adapt include on CrashManager In iap, we want to handle InsufficientCreditErrors in a special way (present the user with a prompt to buy more credits) instead of the usual error handling that shows a crash dialog. Previously, this was done by doing an include on the crash manager. In wowl, we no longer use the crash manager in the backend, and replaced it with an error service that allows developpers to register error handlers for these kinds of use cases. This commit adapts what was previously an include on the crash manager by instead adding a handler to the error handler registry, the file has also been renamed accordingly. The file iap_credit.js has also been removed, as it adds an action to the action registry that is no longer called by anything, and is dead code. --- addons/iap/static/src/js/crash_manager.js | 72 ------------------- addons/iap/static/src/js/iap_credit.js | 26 ------- .../js/insufficient_credit_error_handler.js | 54 ++++++++++++++ addons/iap/static/src/xml/iap_templates.xml | 19 +++-- 4 files changed, 66 insertions(+), 105 deletions(-) delete mode 100644 addons/iap/static/src/js/crash_manager.js delete mode 100644 addons/iap/static/src/js/iap_credit.js create mode 100644 addons/iap/static/src/js/insufficient_credit_error_handler.js diff --git a/addons/iap/static/src/js/crash_manager.js b/addons/iap/static/src/js/crash_manager.js deleted file mode 100644 index 11324fb68a2..00000000000 --- a/addons/iap/static/src/js/crash_manager.js +++ /dev/null @@ -1,72 +0,0 @@ -odoo.define('iap.CrashManager', function (require) { -"use strict"; - -var ajax = require('web.ajax'); -var core = require('web.core'); -var CrashManager = require('web.CrashManager').CrashManager; -var Dialog = require('web.Dialog'); - -var _t = core._t; -var QWeb = core.qweb; - -CrashManager.include({ - /** - * Change button string depending if it's Enterprise and trial available - * (Only change the text message in the button, doesn't change IAP side validation) - * - * @param {boolean} isTrial - * @returns {string} - * @private - */ - _getButtonMessage: function (isTrial){ - var isEnterprise = _.last(odoo.session_info.server_version_info) === 'e'; - return isTrial && isEnterprise ? _t('Start a Trial at Odoo') : _t('Buy credits'); - }, - /** - * @override - */ - rpc_error: function (error) { - var self = this; - if (error.data.name === "odoo.addons.iap.tools.iap_tools.InsufficientCreditError") { - var error_data = JSON.parse(error.data.message); - ajax.jsonRpc('/web/dataset/call_kw', 'call', { - model: 'iap.account', - method: 'get_credits_url', - args: [], - kwargs: { - base_url: error_data.base_url, - service_name: error_data.service_name, - credit: error_data.credit, - trial: error_data.trial - } - }).then(function (url) { - var content = $(QWeb.render('iap.redirect_to_odoo_credit', { - data: error_data, - })); - if (error_data.body) { - content.css('padding', 0); - } - new Dialog(this, { - size: 'large', - title: error_data.title || _t("Insufficient Balance"), - $content: content, - buttons: [{ - text: self._getButtonMessage(error_data.trial), - classes : "btn-primary", - click: function () { - window.open(url, '_blank'); - }, - close:true, - }, { - text: _t("Cancel"), - close: true, - }], - }).open(); - }); - } else { - this._super.apply(this, arguments); - } - }, -}); - -}); diff --git a/addons/iap/static/src/js/iap_credit.js b/addons/iap/static/src/js/iap_credit.js deleted file mode 100644 index baabd7bd980..00000000000 --- a/addons/iap/static/src/js/iap_credit.js +++ /dev/null @@ -1,26 +0,0 @@ -odoo.define('iap.redirect_odoo_credit_widget', function(require) { -"use strict"; - -var AbstractAction = require('web.AbstractAction'); -var core = require('web.core'); - - -var IapOdooCreditRedirect = AbstractAction.extend({ - template: 'iap.redirect_to_odoo_credit', - events : { - "click .redirect_confirm" : "odoo_redirect", - }, - init: function (parent, action) { - this._super(parent, action); - this.url = action.params.url; - }, - - odoo_redirect: function () { - window.open(this.url, '_blank'); - this.do_action({type: 'ir.actions.act_window_close'}); - // framework.redirect(this.url); - }, - -}); -core.action_registry.add('iap_odoo_credit_redirect', IapOdooCreditRedirect); -}); diff --git a/addons/iap/static/src/js/insufficient_credit_error_handler.js b/addons/iap/static/src/js/insufficient_credit_error_handler.js new file mode 100644 index 00000000000..7a868009099 --- /dev/null +++ b/addons/iap/static/src/js/insufficient_credit_error_handler.js @@ -0,0 +1,54 @@ +/** @odoo-module */ +import { Dialog } from "@web/core/dialog/dialog"; +import { registry } from "@web/core/registry"; +import { useService } from "@web/core/service_hook"; + +class InsufficientCreditDialog extends Dialog { + setup() { + super.setup(...arguments); + this.orm = useService("orm"); + } + + async willStart() { + const { errorData } = this.props; + this.url = await this.orm.call("iap.account", "get_credits_url", [], { + base_url: errorData.base_url, + service_name: errorData.service_name, + credit: errorData.credit, + trial: errorData.trial, + }); + this.style = errorData.body ? "padding:0;" : ""; + const { _t } = this.env; + const isEnterprise = _.last(odoo.info.server_version_info) === "e"; + if (errorData.trial && isEnterprise) { + this.buttonMessage = _t("Start a Trial at Odoo"); + } else { + this.buttonMessage = _t("Buy credits"); + } + } + + buyCredits() { + window.open(this.url, "_blank"); + this.close(); + } +} +InsufficientCreditDialog.bodyTemplate = "iap.redirect_to_odoo_credit"; +InsufficientCreditDialog.footerTemplate = "iap.InsufficientCreditFooter"; + +function insufficientCreditHandler(env, error, originalError) { + if (!originalError) { + return false; + } + const { data } = originalError; + if (data && data.name === "odoo.addons.iap.tools.iap_tools.InsufficientCreditError") { + env.services.dialog.open(InsufficientCreditDialog, { + errorData: JSON.parse(data.message), + }); + return true; + } + return false; +} + +registry + .category("error_handlers") + .add("insufficientCreditHandler", insufficientCreditHandler, { sequence: 0 }); diff --git a/addons/iap/static/src/xml/iap_templates.xml b/addons/iap/static/src/xml/iap_templates.xml index 4b920f283b9..6f9b6b1995f 100644 --- a/addons/iap/static/src/xml/iap_templates.xml +++ b/addons/iap/static/src/xml/iap_templates.xml @@ -2,20 +2,25 @@