Jelajahi Sumber

Improvement in token interceptor, visual adjustments

Oscar José Nuñez Chávez 6 tahun lalu
induk
melakukan
a62135a539

+ 1 - 1
src/app/components/dashboard/dashboard.component.html

@@ -64,7 +64,7 @@
                 <p class='h4'>País: {{selectedPlant.country}}</p>
                 <p class='h4'>Ciudad: {{selectedPlant.city}}</p>
                 <p class='h4'>Dirección: {{selectedPlant.address}}</p>
-                <p class='h4'>Capacidad Instalada: {{selectedPlant.installedCapacity_kW}} kW</p>
+                <p class='h4'>Capacidad Instalada: {{totalInstalled}} kW</p>
 
                 <button class="btn bg-yellow btn-flat" (click)="goToAsset(selectedPlant.id)">Ir a Planta</button>
               </div>

+ 6 - 0
src/app/components/dashboard/dashboard.component.ts

@@ -47,6 +47,7 @@ export class DashboardComponent implements OnInit {
   assetKeys:any;
 
   sumarize:number = 0;
+  totalInstalled:number = 0;
 
   totalMetersInstalled: string;
 
@@ -190,7 +191,12 @@ export class DashboardComponent implements OnInit {
       }
     }
     else {
+      
       this.selectedPlant = this.listAssets.find(e => e.id === this.plantId);
+      let keys = Object.keys(this.selectedPlant.meters_installed.map(item => item["meters_installed"]));
+      for (let prop2 in keys){
+        this.totalInstalled += this.selectedPlant.meters_installed.map(response => response["installedCapacity_kW"])[prop2];
+      }
     }
   }
 

+ 1 - 1
src/app/components/plugins/weather-card/weather-card.component.html

@@ -1,6 +1,6 @@
 <div class="widget">
   <div class="row">
-    <div class="col-lg-7">
+    <div class="col-7">
       <div class="date">
         {{currentDate}}
       </div>

+ 11 - 1
src/app/components/plugins/weather-card/weather-card.component.scss

@@ -12,6 +12,9 @@ div.widget
 div.date
 {
   font-size: .9em;
+  @media screen and (max-width: 680px) {
+    font-size: .75em;
+  }
   font-weight: bold;
   color: rgba(0,0,0,0.5);
 }
@@ -19,6 +22,9 @@ div.date
 div.city
 {
   font-size: 1.3em;
+  @media screen and (max-width: 680px) {
+    font-size: 1em;
+  }
   font-weight: bold;
   text-transform: uppercase;
   padding-top: 5px;
@@ -41,7 +47,11 @@ div.temp
     text-align: right;
     margin-bottom: 20px;
     font-size: 3em;
-    font-weight: bold;
+    @media screen and (max-width: 800px) {
+      font-size: 2.5em;
+      margin-right: 5px;
+    }
+    font-weight: 400;
   }
 }
 

+ 1 - 7
src/app/components/shared/navbar/navbar.component.ts

@@ -155,13 +155,7 @@ export class NavbarComponent implements OnInit {
       });
       Swal.showLoading();
 
-      this.auth.logout().subscribe(success => {
-        if (success) {
-          Swal.close();
-          window.location.href="";
-        }
-      });
-      //window.location.href="";
+      this.auth.logout();
 
       //this.router.navigateByUrl("login");
     }

+ 0 - 6
src/app/components/shared/sidebar/sidebar.component.html

@@ -33,12 +33,6 @@
       <hr>
     </ul>
     <ul *ngIf="adminMenu" class="nav">
-      <li class="nav-item">
-        <a class="nav-link" title="Administración" href="javascript:void(0)" style="cursor: unset;">
-          <i class="material-icons">settings_application</i>
-          <p>Administración</p>
-        </a>
-      </li>
       <li routerLinkActive="active" *ngFor="let menuItem of adminMenuItems" class="{{menuItem.class}} nav-item">
         <a class="nav-link" [routerLink]="[menuItem.path]" *ngIf="menuItem.allowed_roles.indexOf(+role_number)>-1"> 
           <i class="material-icons">{{menuItem.icon}}</i>

+ 9 - 1
src/app/components/shared/sidebar/sidebar.component.ts

@@ -2,6 +2,7 @@ import { Component, OnInit } from '@angular/core';
 import { AuthService } from '../../../services/auth2.service';
 import { Router } from '@angular/router';
 import * as CryptoJS from 'crypto-js';
+import Swal from 'sweetalert2';
 
 declare const $: any;
 declare interface RouteInfo {
@@ -67,8 +68,15 @@ export class SidebarComponent implements OnInit {
   };
 
   logout() {
+    Swal.fire({
+      allowOutsideClick: false,
+      type: 'info',
+      text: 'Espere por favor...'
+    });
+    Swal.showLoading();
+
     this.auth.logout();
-    window.location.href="#/login";
+
     //this.router.navigateByUrl("login");
   };
 

+ 1 - 1
src/app/components/users/users.component.html

@@ -44,7 +44,7 @@
                   <!-- Country Column -->
                   <ng-container matColumnDef="last_name">
                     <th mat-header-cell *matHeaderCellDef>Apellido</th>
-                    <td mat-cell *matCellDef="let row">{{row.role}}</td>
+                    <td mat-cell *matCellDef="let row">{{row.last_name}}</td>
                   </ng-container>
                   
                   <!-- Country Column -->

+ 13 - 11
src/app/services/auth2.service.ts

@@ -1,10 +1,11 @@
 import { Injectable } from '@angular/core';
 import { HttpClient } from '@angular/common/http';
-import { of, Observable } from 'rxjs';
-import { catchError, mapTo, tap, map } from 'rxjs/operators';
+import { of, Observable, forkJoin } from 'rxjs';
+import { catchError, mapTo, tap, map, mergeMap } from 'rxjs/operators';
 import { Token } from '@app/models/token';
 import { environment } from '@environments/environment';
 import * as CryptoJS from 'crypto-js';
+import Swal from 'sweetalert2';
 
 @Injectable({
   providedIn: 'root'
@@ -29,16 +30,17 @@ export class AuthService {
         }));
   }
 
+
   logout() {
-    // Agregar endpoint para revocar TOKEN REFRESH
-    return this.http.post<any>(`${environment.productionApiUrl}/auth/logout`, {
-    }).pipe(
-      tap(() => this.doLogoutUser()),
-      mapTo(true),
-      catchError(error => {
-        return of(false);
-      }));
-    }
+    let refreshToken:string = this.getRefreshToken();
+    this.http.post<any>(`${environment.productionApiUrl}/auth/logout`, {})
+    .subscribe( results => {});
+    this.http.post<any>(`${environment.productionApiUrl}/auth/logout2`, {}).subscribe( results => {
+      this.doLogoutUser();
+      Swal.close();  
+      window.location.href = "";
+    });
+  }
 
   isLoggedIn() {
     return !!this.getJwtToken();

+ 18 - 9
src/app/services/token.interceptor.ts

@@ -3,6 +3,7 @@ import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor, HttpErrorResponse
 import { AuthService } from '@app/services/auth2.service';
 import { Observable, throwError, BehaviorSubject } from 'rxjs';
 import { catchError, filter, take, switchMap } from 'rxjs/operators';
+import Swal from 'sweetalert2';
 
 @Injectable()
 export class TokenInterceptor implements HttpInterceptor {
@@ -13,10 +14,10 @@ export class TokenInterceptor implements HttpInterceptor {
   constructor(public authService: AuthService) { }
 
   intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
-    if (request.url.indexOf("auth/refresh")!= -1){
+    if (request.url.indexOf("auth/refresh")!= -1 || request.url.indexOf("auth/logout2") != -1){
       request = this.addToken(request, this.authService.getRefreshToken());
     }
-    else if (request.url.indexOf("openweather")!= -1){
+    else if (request.url.indexOf("openweather")!= -1 ){
       request;
     }
     else {
@@ -26,11 +27,19 @@ export class TokenInterceptor implements HttpInterceptor {
     }
     
     return next.handle(request).pipe(catchError(error => {
-
       // Erase storage if response code from auth refresh gives 401 or 422 error
       if (request.url.indexOf("auth/refresh")!= -1 && error instanceof HttpErrorResponse){
+        Swal.fire({
+          allowOutsideClick: false,
+          type: 'warning',
+          title: 'La sesión ha expirado',
+          showConfirmButton: false,
+        });  
         this.authService.removeTokens();
-        window.location.href="#/login";
+        setTimeout(function(){
+          Swal.close();
+          window.location.href = "#/login";
+        }, 2000);
       }
 
       if (error instanceof HttpErrorResponse && (error.status === 401 || error.status === 422)) {
@@ -53,7 +62,9 @@ export class TokenInterceptor implements HttpInterceptor {
 
   // Cuando el endpoint devuelva 401 debe mandarlo al login y destruir las variables del storage
   private handle4xxError(request: HttpRequest<any>, next: HttpHandler) {
+    console.log("handle error 400");
     if (!this.isRefreshing) {
+      console.log("inside ")
       this.isRefreshing = true;
       this.refreshTokenSubject.next(null);
 
@@ -64,14 +75,12 @@ export class TokenInterceptor implements HttpInterceptor {
           return next.handle(this.addToken(request, token["data"]["access_token"]));
         }));
 
-
-        
-
-
     } else {
+      this.isRefreshing = false;
+
       return this.refreshTokenSubject.pipe(
         filter(token => token != null),
-        take(1),
+        take(2),
         switchMap(jwt => {
           return next.handle(this.addToken(request, jwt));
         }));

+ 2 - 1
src/environments/environment.ts

@@ -5,7 +5,8 @@
 export const environment = {
   production: false,
   apiUrl: 'https://api.inverlec.solar',
-  productionApiUrl: 'http://192.168.98.10:8888/api/v1',
+  //productionApiUrl: 'http://192.168.98.10:8888/api/v1',
+  productionApiUrl: 'http://192.168.98.24:5000/api/v1',
   appID: '55899b9ea53834f2736b65a3582b734b',
   gKey: '',
   config: {