Update Episerver UI from Dojo Command?

Vote:
 

Hi!

I have added a new option in the epi.cms.publishmenu and that works just fine. When the user clicks the menu option I show a confirm dialog and if the user clicks the Publish button in the dialog I publish the page and try to update the UI.

I found this earlier poat from which I tried to reuse code:
https://world.episerver.com/forum/developer-forum/-Episerver-75-CMS/Thread-Container/2018/3/trigger-refresh-of-ui/

The thing is that it works the second time I click the link. The first time the "Changes to be publish" is still there and if I press my option one more time it disappears and the page looks like it should with nothing to publish.
If I reresh the page after the first time it also looks like it should, no changes to publish.

this is my code:

define([
"dojo/_base/declare",
"dojo/topic",
"dojo/Deferred",
"epi/dependency",
"epi-cms/contentediting/command/_ChangeContentStatus",
"epi/shell/DialogService",
"epi/shell/XhrWrapper",
"epi-cms/contentediting/ContentActionSupport",
"epi-cms/contentediting/ContentViewModel",
"epi/i18n!epi/cms/nls/translations",
"myplugin/LanguageSelector"
],

function (declare, topic, Deferred, dependency, _ChangeContentStatus, dialogService, XhrWrapper, ContentActionSupport, ContentViewModel, resources, LanguageSelector) {

    return declare([_ChangeContentStatus], {

        name: "PublishAndMark",
        label: resources.dojo.publishandmark,
        iconClass: "epi-MyIcon",
        canExecute: true,
        action: ContentActionSupport.saveAction.Publish,
        forceReload: true,

        postscript: function () {
            this.inherited(arguments);

            this._dialogService = this._dialogService || dialogService;
            this._pageDataStore = dependency.resolve("epi.storeregistry").get("epi.cms.contentdata");
            this.projectService = dependency.resolve("epi.cms.ProjectService");
        },

        _execute: function () {

            var self = this;
            //var args = arguments;
            var languageDialog = new LanguageSelector(self.model.contentData.contentLink, self.model.languageContext.language);

            var confirmation = this._dialogService.confirmation({
                description: resources.dojo.confirmation,
                dialogClass: "epi-dialog-smartPublish",
                content: languageDialog,
                title: resources.dojo.chooselanguage,
                cancelActionText: epi.resources.action.cancel,
                confirmActionText: epi.resources.action.publish,
                setFocusOnConfirmButton: true,
            });

            var deferred = new Deferred();
            confirmation.then(function () {

                var contentData = self.model.contentData;

                var viewModel = new ContentViewModel({
                    contentLink: contentData.contentLink,
                    contextTypeName: self.model.contextTypeName
                });

                viewModel.set("contentData", contentData);
                viewModel.set("languageContext", self.model.languageContext);
                viewModel.changeContentStatus(self.action);                              

                if (languageDialog.selectedLanguages.length > 0) {
                    var xhr = new XhrWrapper();
                    xhr.xhrGet({
                        url: "/MyUrl",
                        handleAs: "json",
                        content: {
                            contentId: self.model.contentData.contentLink,
                            sourceLanguage: self.model.languageContext.language,
                            targetLanguages: languageDialog.selectedLanguages,
                            includeBlock: languageDialog.includeBlockInContentAreas
                        },
                        failOk: true,
                        load: function (response) {
                            //self._dialogService.alert(response.responseText);
                        },
                        error: function (response) {
                            console.log(response);
                            //self._dialogService.alert(response.responseText);
                        }
                    });                        
                }

                topic.publish("/epi/shell/context/request", {
                    uri: "epi.cms.contentdata:///" + contentData.contentLink
                }, {
                    sender: self,
                    viewName: self.view,
                    forceContextChange: true,
                    forceReload: true
                });
                    
            });
        },

        _onModelChange: function () {

            this.inherited(arguments);

            var isPublishCommandAvailable = this.model.contentData.transitions.filter(function (transition) {
                return transition.name === "publish";
            }).length !== 0;

            this.projectService.getCurrentProjectId().then(function (isProjectActive) {
                if (isProjectActive || !this.model) {
                    this.set("isAvailable", false);
                } else {
                    var contentData = this.model.contentData || this.model;
                    var hasPublishRights = ContentActionSupport.hasAccess(contentData.accessMask, ContentActionSupport.accessLevel.Publish);
                    this.set("isAvailable", hasPublishRights && isPublishCommandAvailable);
                }
            }.bind(this));
        }
    });
});

What am I missing to get this to work the first time?

Thanks!

/Kristoffer

#246787
Edited, Jan 14, 2021 8:09
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.