[FIX] website: share code between new content and page list

The 'addPage' code on website pages list view was duplicated from
'new_content.js'. The goal of this commit is to move this code to the
'AddPageDialog' component.

task-2889981

X-original-commit: 58698f848703395ff680bc7586ecc8eec4fb61ae
Part-of: odoo/odoo#101215
This commit is contained in:
xO-Tx
2022-09-27 11:41:52 +02:00
committed by qsm-odoo
parent f451840941
commit 9d9fe23c33
3 changed files with 31 additions and 44 deletions
@@ -4,7 +4,7 @@ import { useAutofocus, useService } from '@web/core/utils/hooks';
import { Dialog } from '@web/core/dialog/dialog';
import { Switch } from '@website/components/switch/switch';
import {unslugHtmlDataObject} from '../../services/website_service';
import { _t } from 'web.core';
import {csrf_token, _t} from 'web.core';
const { xml, useState, Component, onWillStart } = owl;
@@ -84,6 +84,7 @@ export class AddPageDialog extends Component {
this.switchLabel = this.env._t("Add to menu");
this.website = useService('website');
this.orm = useService('orm');
this.http = useService('http');
this.state = useState({
addMenu: true,
@@ -105,17 +106,42 @@ export class AddPageDialog extends Component {
}
async addPage() {
await this.props.addPage(this.state);
const params = {'add_menu': this.state.addMenu || '', csrf_token};
const url = `/website/add/${encodeURIComponent(this.state.name)}`;
const websiteId = parseInt(this.state.websiteId);
if (this.props.selectWebsite) {
params['website_id'] = websiteId;
}
const data = await this.http.post(url, params);
if (data.view_id) {
this.action.doAction({
'res_model': 'ir.ui.view',
'res_id': data.view_id,
'views': [[false, 'form']],
'type': 'ir.actions.act_window',
'view_mode': 'form',
});
} else {
this.website.goToWebsite({path: data.url, edition: true, ...(this.props.selectWebsite && {websiteId})});
}
this.props.onAddPage(this.state);
}
}
AddPageDialog.props = {
close: Function,
addPage: Function,
onAddPage: {
type: Function,
optional: true,
},
selectWebsite: {
type: Boolean,
optional: true,
},
};
AddPageDialog.defaultProps = {
onAddPage: NO_OP,
selectWebsite: false,
};
AddPageDialog.components = {
Switch,
WebsiteDialog,
@@ -4,7 +4,6 @@ import {AddPageDialog} from "../dialog/dialog";
import {registry} from '@web/core/registry';
import {listView} from '@web/views/list/list_view';
import {useService} from "@web/core/utils/hooks";
import { csrf_token } from 'web.core';
const {onWillStart, useState} = owl;
@@ -16,7 +15,6 @@ export class PageListController extends listView.Controller {
setup() {
super.setup();
this.website = useService('website');
this.http = useService('http');
this.websiteSelection = [{id: 0, name: this.env._t("All Websites")}];
@@ -35,27 +33,7 @@ export class PageListController extends listView.Controller {
*/
onClickCreate() {
if (this.props.resModel === 'website.page') {
return this.dialogService.add(AddPageDialog, {
selectWebsite: true,
addPage: async (state) => {
// TODO this is duplicated code from new_content.js, this
// should be shared somehow.
const websiteId = parseInt(state.websiteId);
const url = `/website/add/${encodeURIComponent(state.name)}`;
const data = await this.http.post(url, { 'add_menu': state.addMenu || '', 'website_id': websiteId, csrf_token });
if (data.view_id) {
this.actionService.doAction({
'res_model': 'ir.ui.view',
'res_id': data.view_id,
'views': [[false, 'form']],
'type': 'ir.actions.act_window',
'view_mode': 'form',
});
} else {
this.website.goToWebsite({ path: data.url, edition: true, websiteId });
}
},
});
return this.dialogService.add(AddPageDialog, {selectWebsite: true});
}
const action = this.props.context.create_action;
if (action) {
@@ -4,7 +4,6 @@ import { registry } from '@web/core/registry';
import { useService } from '@web/core/utils/hooks';
import { WebsiteDialog, AddPageDialog } from "@website/components/dialog/dialog";
import { useHotkey } from "@web/core/hotkeys/hotkey_hook";
import { csrf_token } from 'web.core';
import { sprintf } from '@web/core/utils/strings';
const { Component, xml, useState, onWillStart } = owl;
@@ -61,7 +60,6 @@ export class NewContentModal extends Component {
this.dialogs = useService('dialog');
this.website = useService('website');
this.action = useService('action');
this.http = useService('http');
this.isSystem = this.user.isSystem;
this.newContentText = {
@@ -146,22 +144,7 @@ export class NewContentModal extends Component {
createNewPage() {
this.dialogs.add(AddPageDialog, {
addPage: async (state) => {
const url = `/website/add/${encodeURIComponent(state.name)}`;
const data = await this.http.post(url, { 'add_menu': state.addMenu || '', csrf_token });
if (data.view_id) {
this.action.doAction({
'res_model': 'ir.ui.view',
'res_id': data.view_id,
'views': [[false, 'form']],
'type': 'ir.actions.act_window',
'view_mode': 'form',
});
} else {
this.website.goToWebsite({ path: data.url, edition: true });
}
this.websiteContext.showNewContentModal = false;
},
onAddPage: () => this.websiteContext.showNewContentModal = false,
});
}