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 @@