Modal does not return updated validation error result when submitting a request $ http ()

0

{"permission": true,
 "title": "ALUNO: aDSadaD",
 "question": "Deseja ativar este Aluno?",
 "usuario": {
  "id": "42247",
  "usuario": "alunoguten3360",
  "role": "student",
  "role_formatted": "Aluno",
  "nome": "aDSadaD",
  "options_roles": [
   {
    "value": "student",
    "title": "Aluno"
   },
   {
    "value": "teacher",
    "title": "Professor"
   },
   {
    "value": "coordinator",
    "title": "Coordenador"
   }
  ],
  "public_password": "kk8744",
  "user_type": "student",
  "status": 1,
  "coordinators": [],
  "turmas": [
   {
    "turma": "Year 4 A",
    "value": "131"
   }
  ],
  "$$hashKey": "object:393"
 },
 "id": "42247",
 "role": "student",
 "status": true,
 "new_status": 1,
 "message": "Usuário desativado com sucesso."
}

However, when I make a console.log () on the object in question, it does complete what should come in stringify :

  

"error_message: You have arrived ..."

Here is the method that calls Modal Control:

$scope.editarStatusUsuario = function (size) {
                    $scope.hideTooltip();
                    if (isPermission()) {
                        if(size == 'desativar') {
                            $scope.statusUsuario = {
                                permission: isPermission(),
                                title: this.usuario.role_formatted.toUpperCase() + ': ' + this.usuario.nome,
                                question: 'Deseja desativar este ' + this.usuario.role_formatted + '?',
                                usuario: this.usuario,
                                id: this.usuario.id,
                                role: this.usuario.role,
                                status: false,
                                new_status:2,
                                message: 'Usuário ativado com sucesso.'
                            }
                        } else {
                            $scope.statusUsuario = {
                                permission: isPermission(),
                                title: this.usuario.role_formatted.toUpperCase() + ': ' + this.usuario.nome,
                                question: 'Deseja ativar este ' + this.usuario.role_formatted + '?',
                                usuario: this.usuario,
                                id: this.usuario.id,
                                role: this.usuario.role,
                                status: false,
                                new_status:1,
                                message:'Usuário desativado com sucesso.',
                            }
                        }

                        var modalInstance = $uibModal.open({
                            animation: true,
                            templateUrl: 'statusUsuario.html',
                            controller: 'ModalController',
                            size: size,
                            resolve: {
                                validate: function () {
                                    return 'status-usuario';
                                },
                                dados: function () {
                                    return $scope.statusUsuario;
                                },
                                statusModal: function () {
                                    return ' modal-open';
                                }
                            }

                        });

                        modalInstance.result.then(function (selectedItemResult) {

                             $scope.hideTooltip();
                            if (selectedItemResult.status == true) {
                                var indexUser = $filter('FilterData')(selectedItemResult.id, $scope.dataUsuarios, 'id');
                                if (angular.isDefined($scope.dataUsuarios)) {
                                    $scope.dataUsuarios[indexUser].status = selectedItemResult.new_status;
                                    $scope.classMessages = 'show';
                                    console.log(selectedItemResult);
                                    console.log(JSON.stringify(selectedItemResult, null, ' '));
                                    if (selectedItemResult.result == false) {
                                        $scope.messageError = AlertMessagesService.getMessages([selectedItemResult.message], 'danger', '');
                                    } else {
                                        $scope.messageError = AlertMessagesService.getMessages([selectedItemResult.message], 'success', '');
                                    }

                                }

                            } else {
                                $scope.classMessages = 'show';
                                $scope.messageError = AlertMessagesService.getMessages(['O status não foi alterado.'], 'danger', '');

                                loadingOff('');
                            }
                            //caso ok, faz...
                            // $scope.deletedTurma = selectedItem.data_result;
                            //$log.info(selectedItem)
                            $scope.blurClassElement = '';

                        }, function () {
                            //console.log(cancel);
                            $scope.blurClassElement = '';
                            //$log.info('Modal dismissed at: ' + new Date());
                        });
                        $scope.blurClassElement = 'is-blured-content';
                    } else {
                        $scope.classMessages = 'show';
                        $scope.messageError = AlertMessagesService.getMessages(['Você não tem permissão para desativar usuários.'],'danger', 'Ocorreu um problema de permissão');

                    }

And this is the modal control:

.controller("ModalController",
    function($scope, $sce, $rootScope, $filter, $uibModalInstance, $uibModal, $log, $timeout, $window, dados, validate, ModalService, AlertMessagesService, statusModal) {
    'use strict';
        $scope.submitForm = function() {
            $scope.statusUsuario = [];
            
            var validateForm = { status:true, messages:null };   
                  
           if (validate == 'status-usuario') {    
               validateForm = ValidateFilter.validateForm($scope, validate);           
               if (validateForm.status == true) {   
                   ModalService.status(dados, validate)    
                       .then(function (rtn) {   
                   if (rtn.status == true) {    
                       loadingOff('Status alterado!');   
                       $scope.statusUsuario = rtn.data;    
                       dados.result = rtn.data;    
                       dados.message = 'OK';
                   } else {  
                       loadingOff('');   
                       dados.status = false;   
                       dados.message = rtn.error;   
                   }   
               });    
           } else {
           $scope.messages = validateForm.messages;
           }
       }           
            
       $scope.ok = function() {   
       $scope.submitForm();   
       if ($scope.selected.dados.status == true) {   
           $uibModalInstance.close($scope.selected.dados);   
           $scope.messageError = null;   
       } else {    
           $scope.classMessages = 'show';   
           $scope.messageError = AlertMessagesService
              .getMessages($scope.messages, 'danger', 'Corrija os erros abaixo');     
           return;   
       }   
    };    
            
    $scope.cancel = function() {    
               $uibModalInstance.dismiss('cancel');   
    };    
});
    
asked by anonymous 26.07.2017 / 15:29

1 answer

0

I solved the problem. The problem that I was solving the request inside the modal instance (top layer), as the answer was to be in the lower layer, in this case it was better to use the resolver () in the lower layer and not inside the modal.

In Modal Control, do this:

$scope.selected.dados.result = {dados:dados, type:'desativar-aluno'};

 $scope.returnScope = function() {
            if ($scope.selected.dados.status == true) {
                $uibModalInstance.close($scope.selected.dados);
                $scope.messageError = null;
            } else {
                $rootScope.body_modal_open = ' modal-open';
                $scope.classMessages = 'show';
                $scope.messageError = AlertMessagesService.getMessages($scope.messages, 'danger', 'Corrija os erros abaixo');
                $('body').addClass('modal-open');
                return;
            }
        }

At the bottom layer (main control):

modalInstance.result.then(function (selectedItemResult) {
                            $scope.hideTooltip();
                            if (selectedItemResult.status == true) {
                                var indexUser = $filter('FilterData')(selectedItemResult.id, $scope.dataUsuarios, 'id');
                                if (angular.isDefined($scope.dataUsuarios)) {
                                    UsuariosService.statusUsuario(selectedItemResult)
                                    .then(function (rtn) {
                                        $scope.classMessages = 'show';

                                        if (rtn.status == true) {
                                            $scope.dataUsuarios[indexUser].status = selectedItemResult.new_status;
                                            loadingOff('Status alterado!');
                                            $scope.statusUsuario = rtn.data;
                                            var message = selectedItemResult.message_delete;
                                            if (selectedItemResult.type == 'desativar-aluno') {
                                                 message = selectedItemResult.message_enable;
                                            } else {
                                                $scope.dataUsuarios.splice(indexUser, 1);
                                            }
                                            $scope.messageError = AlertMessagesService.getMessages([message], 'success', '');
                                        } else {
                                            loadingOff('');
                                            $scope.statusUsuario = rtn.error;
                                            $scope.messageError = AlertMessagesService.getMessages([rtn.error], 'danger', 'Observação');
                                        }
                                    });
                                }

                            } else {
                                $scope.classMessages = 'show';
                                $scope.messageError = AlertMessagesService.getMessages(['O status não foi alterado.'], 'danger', '');
                                loadingOff('');
                            }
                            //caso ok, faz...
                            // $scope.deletedTurma = selectedItem.data_result;
                            //$log.info(selectedItem)
                            $scope.blurClassElement = '';

                        }, function () {
                            //console.log(cancel);
                            $scope.blurClassElement = '';
                            //$log.info('Modal dismissed at: ' + new Date());
                        });
    
08.08.2017 / 16:17