Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nathan Woulfe 447 posts 1665 karma points MVP 5x hq c-trib
    May 05, 2016 @ 03:01
    Nathan Woulfe
    0

    Cancelling an unpublish request client-side

    Trying to intercept requests to unpublish a node and display a confirmation dialog before continuing, largely to avoid authors unknowingly unpublishing large sections of the site.

    Previously have done this through a core modification, but don't want to do that in future.

    I've written an httpinterceptor, which is working fine, but rejecting the request throws an error in Umbraco's securityInterceptor factory.

    function contentUnpublish($q) {
        return {
            request: function (request) {
                try {
                    if (request.url.toLowerCase().indexOf('content/postunpublish') !== -1) {
                        var text = 'This will remove the page and all of its child pages from the site. Are you sure?';
                        return confirm(text) ? request : $q.reject(request);
                    } else {
                        return request;
                    }
                }
                catch (err) {
                    return $q.reject(request);
                }
            }
        };
    }
    

    This works fine if the user accepts the confirmation, the page unpublishes. If they reject, I get an error in umbraco.security, as the factory attempts to parse a failed request.

    angular.min.js:63 TypeError: Cannot read property 'url' of undefined at umbraco.security.js:42
    

    The originalResponse object is undefined, as the request was rejected, so I'm not sure why the Umbraco interceptor would be triggered at all?

    Anyone have any suggestions as to how to achieve this?

  • Luuk Peters 85 posts 331 karma points
    Jan 02, 2020 @ 13:59
    Luuk Peters
    0

    This really is an old topic, but I can confirm that in Umbraco 7.14.3, this issue is still present. I'm trying to almost achieve the same what the topic starter tried to do. The difference is that I want to ask the content editors in certain cases if they are sure if they want to save the changes, giving them the option to cancel. When I reject the request, Umbraco throws an error in umbraco.security (in may case a slightly different error):

    TypeError: Cannot read property 'indexOf' of undefined
    at umbraco.security.js?cdv=1917793421:128
    at underscore.js:173
    at Function.h.some.h.any (underscore.js:223)
    at Function.h.find.h.detect (underscore.js:172)
    at umbraco.security.js?cdv=1917793421:127
    at angular.min.js?cdv=1917793421:81
    at Object.$eval (angular.min.js?cdv=1917793421:92)
    at Object.$digest (angular.min.js?cdv=1917793421:90)
    at Object.$apply (angular.min.js?cdv=1917793421:92)
    at HTMLButtonElement.<anonymous> (angular-mobile.js?cdv=1917793421:269)
    

    My controller:

    angular.module('umbraco.services').config([
    '$httpProvider',
    function ($httpProvider) {
    
        $httpProvider.interceptors.push(function ($q) {
            return {
                'request': function (request) {
                    console.log("Custom request!" + request.url);
                                    if (request.url === "/umbraco/backoffice/UmbracoApi/Content/PostSave") {
                        if (!confirm("Weet je zeker dat je wilt opslaan?")) {
                            return $q.reject(request);
                        }
                    }
                    return request || $q.when(request);
                }
            };
        });
    }]);
    

    Anyone know what to do?

Please Sign in or register to post replies

Write your reply to:

Draft