All files / web/bundles/pimdatagrid/js/datagrid/action delete-action.js

89.66% Statements 26/29
50% Branches 5/10
100% Functions 8/8
89.66% Lines 26/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126  463x                                       463x                         9967x   9967x   9967x             25x             22x 22x       4x 4x   4x 4x 4x 4x       4x     18x 18x 18x   18x                 24x           24x                 4x   4x 4x                 4x                   4x          
/* global define */
define([
        'underscore',
        'oro/messenger',
        'oro/translator',
        'pim/dialog',
        'oro/datagrid/model-action',
        'oro/mediator',
        'pim/user-context',
        'oro/datagrid/delete-confirm'
    ],
    function(_, messenger, __, Dialog, ModelAction, mediator, userContext, DeleteConfirm) {
        'use strict';
 
        /**
         * Delete action with confirm dialog, triggers REST DELETE request
         *
         * @export  oro/datagrid/delete-action
         * @class   oro.datagrid.DeleteAction
         * @extends oro.datagrid.ModelAction
         */
        return ModelAction.extend({
            errorModal: undefined,
 
            confirmModal: undefined,
 
            /**
             * Initialize view
             *
             * @param {Object} options
             * @param {Backbone.Model} options.model Optional parameter
             * @throws {TypeError} If model is undefined
             */
            initialize: function(options) {
                options = options || {};
 
                this.gridName = options.datagrid.name;
 
                ModelAction.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * Execute delete model
             */
            execute: function() {
                this.getConfirmDialog();
            },
 
            /**
             * Confirm delete item
             */
            doDelete: function() {
                this.model.id = true;
                this.model.destroy({
                    url: this.getLink(),
                    wait: true,
                    error: function(element, response) {
                        let contentType = response.getResponseHeader('content-type');
                        let message = '';
                        //Need to check if it is a json because the backend can return an error
                        Eif (contentType.indexOf("application/json") !== -1) {
                            const decodedResponse = JSON.parse(response.responseText);
                            Eif (undefined !== decodedResponse.message) {
                                message = decodedResponse.message
                            }
                        }
 
                        this.getErrorDialog(message).open();
                    }.bind(this),
                    success: function() {
                        var messageText = __('pim_enrich.entity.' + this.getEntityCode() + '.flash.delete.success');
                        messenger.notify('success', messageText);
                        userContext.initialize();
 
                        mediator.trigger('datagrid:doRefresh:' + this.gridName);
                    }.bind(this)
                });
            },
 
            /**
             * Get view for confirm modal
             */
            getConfirmDialog: function() {
                this.confirmModal = DeleteConfirm.getConfirmDialog(
                    this.getEntityCode(),
                    this.doDelete.bind(this),
                    this.getEntityHint(true)
                );
 
                return this.confirmModal;
            },
 
            /**
             * Get view for error modal
             *
             * @return {oro.Modal}
             */
            getErrorDialog: function(response) {
                let message = '';
 
                Eif (typeof response === 'string') {
                    message = response;
                } else {
                    try {
                        message = JSON.parse(response).message;
                    } catch(e) {
                        message = __('pim_enrich.entity.' + this.getEntityHint() + '.flash.delete.fail');
                    }
                }
 
                this.errorModal = new Backbone.BootstrapModal({
                    title: __('pim_datagrid.delete_error.title'),
                    content: '' === message ?
                        __('pim_enrich.entity.' + this.getEntityHint() + '.flash.delete.fail') :
                        message,
                    buttonClass: 'AknButton--important',
                    illustrationClass: 'delete',
                    cancelText: '',
                });
 
                return this.errorModal;
            }
        });
    }
);