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

Asked

Viewed 45 times

0

Strange problem, with $uibModal Angularjs, when using JSON.stringify(object, null, ' '), it does not show the full return in the modal result:

inserir a descrição da imagem aqui

{
 "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 do a console.log() on the object in question, it brings complete what should come on stringify:

"error_message: You have arrived..."

Follows the method that calls the 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 control of the modal:

.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');   
    };    
});

1 answer

0


I solved the problem. The problem I was solving the request within the modal instance (top layer), as the answer was to be in the bottom layer, in this case it was better to use the resolve() in the bottom layer and not within 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;
            }
        }

On 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());
                        });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.