UI initial implementation.
[validation.git] / ui / src / main / webapp / resources / js / App.js
1 /*
2  * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 var AECBlueprintValidationUIApp = angular.module('BlueprintValidationUIManagement', ['ngDialog', 'ui.router', 'base64','App.config','ngStorage','ui.bootstrap', 'ngResource','ngFileUpload','ngMaterial']);
18
19 AECBlueprintValidationUIApp.config(function($stateProvider, $urlRouterProvider) {
20     $urlRouterProvider.otherwise('/login')
21     $stateProvider
22         .state('common', {
23             templateUrl: 'views/indexMain.html',
24             abstract: true
25         })
26         .state('login', {
27             url: "/login",
28             controller: 'Login',
29             templateUrl: 'views/login.html'
30         })
31         .state('newSubmission', {
32             url: "/newSubmission",
33             parent: "common",
34             views: {
35                 "main": {
36                     controller: 'AECNewSubmissionController',
37                     templateUrl: 'views/newSubmission.html'
38                 }
39             }
40         })
41         .state('committedSubmissions', {
42             url: "/committedSubmissions",
43             parent: "common",
44             views: {
45                 "main": {
46                     controller: 'AECCommittedSubmissionsController',
47                     templateUrl: 'views/committedSubmissions.html'
48                 }
49             }
50         })
51         .state('findBySubmissionId', {
52             url: "/findBySubmissionId",
53             parent: "common",
54             views: {
55                 "main": {
56                     controller: 'AECFindBySubmissionIdController',
57                     templateUrl: 'views/findBySubmissionId.html'
58                 }
59             }
60         })
61 });
62
63 AECBlueprintValidationUIApp.controller('Login',function($scope, $http, $filter, filterFilter, $state, $base64,$rootScope,$controller,appContext) {
64     $rootScope.tokenId ="";
65     $scope.usernameVal = '';
66     $scope.passwordVal = '';
67     $rootScope.message = "Please enter credentials";
68     $scope.$state = $state;
69
70     var baseURL = window.location.protocol + '//' + window.location.host;
71     /* eslint-disable no-console */
72     console.log('Base URL for current frame is: ' + baseURL);
73     /* eslint-enable no-console */
74     $scope.goLogin = function() {
75         var arr = $scope.passwordVal;
76         if ($scope.usernameVal == '' && $scope.passwordVal == '') {
77             $scope.userMessage = 'Please enter a username.';
78             $scope.passwordMessage = 'Please enter a password.';
79         } else if ($scope.usernameVal == '') {
80             $scope.userMessage = 'Please enter a username.';
81             $scope.passwordMessage = '';
82         } else if ($scope.passwordVal == '') {
83             $scope.passwordMessage = 'Please enter a password.';
84             $scope.userMessage = '';
85         } else if (arr.length < 6) {
86             $scope.passwordMessage = 'Please enter a valid password.';
87             $scope.userMessage = '';
88         }
89        else {
90             $scope.passwordMessage = '';
91             $scope.userMessage ='';
92             // var userPwd = $scope.usernameVal + ":" + $scope.passwordVal;
93             // var auth = $base64.encode(userPwd);
94             /*
95              * $http({ method: 'POST', url: appContext+'/login', //url:
96              * 'http://'+hostUrl+'/AECPortalMgmt/login', headers: {
97              * 'Authorization': "Basic " + auth, 'Content-Type':
98              * "application/json", 'Accept': "application/json" }, data: { } }).
99              * then(function(response) { if (response.data.statusCode == 200) {
100              * $rootScope.tokenId = response.data.tokenId;
101              * localStorage.setItem("tokenId",response.data.tokenId);
102              * $state.transitionTo('sites'); } else if (response.data.statusCode ==
103              * 401){ $scope.passwordVal= null; $scope.passwordMessage = 'Invalid
104              * Credentials, please try again...';
105              *
106              * localStorage.removeItem("tokenId"); } }, function(error) { if
107              * (error.status == 401) { $scope.passwordMessage = 'Invalid
108              * Credentials, please try again...'; $scope.passwordVal ="";
109              * localStorage.removeItem("tokenId"); } else if (error.status ==
110              * 400) { $scope.passwordMessage = 'Session Invalid, please login
111              * again...'; $scope.passwordVal ="";
112              * localStorage.removeItem("tokenId"); } else if (error.status ==
113              * 307) { $scope.passwordMessage = 'Session expired,Please try
114              * again...'; $scope.passwordVal ="";
115              * localStorage.removeItem("tokenId"); } });
116              */
117             $state.transitionTo('committedSubmissions');
118         }
119     }
120     $scope.goLogout = function() {
121         $http({
122             method: 'POST',
123             url: appContext+'/logout',
124             headers: {
125                 'Content-Type': "application/json",
126                 'Accept': "application/json",
127                 'tokenId' : $rootScope.tokenId
128             },
129         data:{
130         }
131             /*
132              * data: { 'username': $scope.usernameVal, 'passowrd':
133              * $scope.passwordVal }
134              */
135         }).then(function(response) {
136             if (response.data.statusCode == 200) {
137                 $rootScope.tokenId ="";
138                 localStorage.removeItem("tokenId");
139                 $state.transitionTo('login');
140                 $rootScope.message = 'User logged out, please login again...';
141             }
142         }, function(response) {
143             $scope.message = 'Unknown error,Try again later' + response.status;
144         });
145     }
146 });
147