[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.
This commit is contained in:
Samuel Degueldre
2021-06-18 21:31:31 +02:00
committed by Aaron Bohy
parent 9c05de391e
commit 4e7187bba0
4 changed files with 66 additions and 105 deletions
-72
View File
@@ -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);
}
},
});
});
-26
View File
@@ -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);
});
@@ -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 });
+12 -7
View File
@@ -2,20 +2,25 @@
<template id="template" xml:space="preserve">
<!-- LAYOUT TEMPLATES -->
<div t-name="iap.redirect_to_odoo_credit">
<t t-if="data.body">
<div t-raw="data.body"/>
<div t-name="iap.redirect_to_odoo_credit" t-att-style="style" owl="1">
<t t-if="props.errorData.body">
<div t-raw="props.errorData.body"/>
</t>
<t t-if="!data.body">
<t t-if="data.message">
<span t-esc="data.message"/>
<t t-if="!props.errorData.body">
<t t-if="props.errorData.message">
<span t-esc="props.errorData.message"/>
</t>
<t t-if="!data.message">
<t t-if="!props.errorData.message">
<span>Insufficient credit to perform this service.</span>
</t>
</t>
</div>
<div t-name="iap.InsufficientCreditFooter" owl="1">
<button class="btn btn-primary" t-on-click="buyCredits" t-esc="buttonMessage"/>
<button class="btn" t-on-click="close">Cancel</button>
</div>
<t t-extend="DashboardMain">
<t t-jquery=".o_web_settings_apps" t-operation="after">
<div class="o_web_settings_iap"></div>