From fd54179ceba290ca06e2a607d798a19713e43ef0 Mon Sep 17 00:00:00 2001 From: Romain Derie Date: Wed, 4 Aug 2021 13:11:40 +0000 Subject: [PATCH] [FIX] website_mass_mailing: improve configuration usability 1. The option to change newsletter becomes a select instead of a button that open a modal 2. If no mailing list exists, we show a warning and user can be redirected to the backend to create one if he wants. 3. Add name to the snippet so it is not "Block" in the right panel Community: https://github.com/odoo/odoo/pull/74667 Upgrade: https://github.com/odoo/upgrade/pull/2742 task-2246975 closes odoo/odoo#74667 Related: odoo/upgrade#2742 Signed-off-by: Romain Derie --- .../tests/tours/snippets_all_drag_and_drop.js | 13 +-- .../src/js/website_mass_mailing.editor.js | 94 +++++++++---------- .../static/tests/tours/newsletter_popup.js | 4 - .../views/snippets_templates.xml | 4 +- 4 files changed, 49 insertions(+), 66 deletions(-) diff --git a/addons/website/static/tests/tours/snippets_all_drag_and_drop.js b/addons/website/static/tests/tours/snippets_all_drag_and_drop.js index 22e06e61a1e..1d8226b2df4 100644 --- a/addons/website/static/tests/tours/snippets_all_drag_and_drop.js +++ b/addons/website/static/tests/tours/snippets_all_drag_and_drop.js @@ -33,23 +33,12 @@ for (const snippet of snippetsNames) { content: 'Close API Key popup', trigger: ".modal-footer .btn-secondary", }); - } else if (snippet === 's_popup') { + } else if (['s_popup', 's_newsletter_subscribe_popup'].includes(snippet)) { snippetSteps[2]['in_modal'] = false; snippetSteps.splice(3, 2, { content: `Hide the ${snippet} popup`, trigger: ".s_popup_close", }); - } else if (['s_newsletter_block', 's_newsletter_subscribe_form', 's_newsletter_subscribe_popup'].includes(snippet)) { - snippetSteps.splice(1, 0, { - content: `Confirm the ${snippet} popup`, - trigger: `.modal-footer button.btn-primary`, - }); - } - if (snippet === 's_newsletter_subscribe_popup') { - snippetSteps.splice(3, 2, { - content: `Hide the ${snippet} popup`, - trigger: "button.close", - }); } steps = steps.concat(snippetSteps); } diff --git a/addons/website_mass_mailing/static/src/js/website_mass_mailing.editor.js b/addons/website_mass_mailing/static/src/js/website_mass_mailing.editor.js index 1b344472a2c..d065f485e6a 100644 --- a/addons/website_mass_mailing/static/src/js/website_mass_mailing.editor.js +++ b/addons/website_mass_mailing/static/src/js/website_mass_mailing.editor.js @@ -2,65 +2,63 @@ odoo.define('website_mass_mailing.editor', function (require) { 'use strict'; var core = require('web.core'); -var rpc = require('web.rpc'); +const Dialog = require('web.Dialog'); var options = require('web_editor.snippets.options'); -var wUtils = require('website.utils'); const qweb = core.qweb; var _t = core._t; options.registry.mailing_list_subscribe = options.Class.extend({ - popup_template_id: "editor_new_mailing_list_subscribe_button", - popup_title: _t("Add a Newsletter Subscribe Button"), - - //-------------------------------------------------------------------------- - // Options - //-------------------------------------------------------------------------- - - /** - * Allows to select mailing list. - * - * @see this.selectClass for parameters - */ - select_mailing_list: function (previewMode, value) { - var self = this; - var def = wUtils.prompt({ - 'id': this.popup_template_id, - 'window_title': this.popup_title, - 'select': _t("Newsletter"), - 'init': function (field, dialog) { - return rpc.query({ - model: 'mailing.list', - method: 'name_search', - args: ['', [['is_public', '=', true]]], - context: self.options.recordInfo.context, - }).then(function (data) { - $(dialog).find('.btn-primary').prop('disabled', !data.length); - var list_id = self.$target.attr("data-list-id"); - $(dialog).on('show.bs.modal', function () { - if (list_id !== "0"){ - $(dialog).find('select').val(list_id); - }; - }); - return data; - }); - }, - }); - def.then(function (result) { - self.$target.attr("data-list-id", result.val); - }); - return def; - }, /** * @override */ - onBuilt: function () { - var self = this; - this._super(); - this.select_mailing_list('click').guardedCatch(function () { - self.getParent()._onRemoveClick($.Event( "click" )); + onBuilt() { + this._super(...arguments); + if (this.mailingLists.length) { + this.$target.attr("data-list-id", this.mailingLists[0][0]); + } else { + Dialog.confirm(this, _t("No mailing list found, do you want to create a new one? This will save all your changes, are you sure you want to proceed?"), { + confirm_callback: () => { + this.trigger_up('request_save', { + reload: false, + onSuccess: () => { + window.location.href = '/web#action=mass_mailing.action_view_mass_mailing_lists'; + }, + }); + }, + cancel_callback: () => { + this.trigger_up('remove_snippet', { + $snippet: this.$target, + }); + }, + }); + } + }, + + //-------------------------------------------------------------------------- + // Private + //-------------------------------------------------------------------------- + + /** + * @override + */ + async _renderCustomXML(uiFragment) { + this.mailingLists = await this._rpc({ + model: 'mailing.list', + method: 'name_search', + args: ['', [['is_public', '=', true]]], + context: this.options.recordInfo.context, }); + if (this.mailingLists.length) { + const selectEl = uiFragment.querySelector('we-select[data-attribute-name="listId"]'); + for (const mailingList of this.mailingLists) { + const button = document.createElement('we-button'); + button.dataset.selectDataAttribute = mailingList[0]; + button.textContent = mailingList[1]; + selectEl.appendChild(button); + } + } }, }); diff --git a/addons/website_mass_mailing/static/tests/tours/newsletter_popup.js b/addons/website_mass_mailing/static/tests/tours/newsletter_popup.js index 44023b00f3a..10663c1e53a 100644 --- a/addons/website_mass_mailing/static/tests/tours/newsletter_popup.js +++ b/addons/website_mass_mailing/static/tests/tours/newsletter_popup.js @@ -12,10 +12,6 @@ tour.register('newsletter_popup_edition', { id: 's_newsletter_subscribe_popup', name: 'Newsletter Popup', }), - { - content: "Confirm newsletter choice", - trigger: '.modal-footer .btn-primary', - }, { content: "Check the modal is opened for edition", trigger: '.o_newsletter_popup .modal:visible', diff --git a/addons/website_mass_mailing/views/snippets_templates.xml b/addons/website_mass_mailing/views/snippets_templates.xml index 5741a3b88f2..6a24e891340 100644 --- a/addons/website_mass_mailing/views/snippets_templates.xml +++ b/addons/website_mass_mailing/views/snippets_templates.xml @@ -25,7 +25,7 @@