From d8dc51074289c0da9a2b1eae1bb66901edc854b7 Mon Sep 17 00:00:00 2001
From: Fuzi_fauzia 
Date: Tue, 9 Jul 2024 11:28:30 +0700
Subject: [PATCH] edit profil refresh token
---
 src/app/_layout/footer/footer.component.html  |   2 +-
 .../hemat-app/service/login.service.ts        |  11 ++++
 .../profil-information.component.ts           |  54 +++++++++++++-----
 src/app/login/login.component.html            |   2 +-
 src/app/login/login.component.ts              |   2 +
 src/assets/images/bg1.png                     | Bin 0 -> 489774 bytes
 6 files changed, 56 insertions(+), 15 deletions(-)
 create mode 100644 src/assets/images/bg1.png
diff --git a/src/app/_layout/footer/footer.component.html b/src/app/_layout/footer/footer.component.html
index c8218e8..ae7c7dc 100644
--- a/src/app/_layout/footer/footer.component.html
+++ b/src/app/_layout/footer/footer.component.html
@@ -13,7 +13,7 @@
         href="https://allbestsistem.com/"
         target="_blank"
         style="background-color: #000000 !important;"
-        >Smart Building Management Systems (V@2024-07-8.03)
+        >Smart Building Management Systems (V@2024-07-9.01)
       
   
diff --git a/src/app/content/hemat-app/service/login.service.ts b/src/app/content/hemat-app/service/login.service.ts
index 6e3d42a..1e01c0a 100644
--- a/src/app/content/hemat-app/service/login.service.ts
+++ b/src/app/content/hemat-app/service/login.service.ts
@@ -40,4 +40,15 @@ export class LoginService {
     return this.http.post(`${url}`, data, { headers });
   }
 
+  updateUserProfile(data: any): Observable {
+    const body = { refresh_token: data };
+    const endpoint = `/users`;
+    const url = `${BASE_URL}${endpoint}/refresh-token`;
+    const headers = new HttpHeaders({
+      "x-api-key": "j2yaYvPSQcsEEmHh3NEobfiXyyXmmnHT",
+    });
+
+    return this.http.post(`${url}`, body, { headers });
+  }
+
 }
diff --git a/src/app/content/hemat-app/user-profile/profil-information/profil-information.component.ts b/src/app/content/hemat-app/user-profile/profil-information/profil-information.component.ts
index 3a7abfc..e1eb5de 100644
--- a/src/app/content/hemat-app/user-profile/profil-information/profil-information.component.ts
+++ b/src/app/content/hemat-app/user-profile/profil-information/profil-information.component.ts
@@ -2,7 +2,18 @@ import { Component, ViewChild } from "@angular/core";
 import { FormBuilder, FormGroup, NgForm, Validators } from "@angular/forms";
 import { LoginService } from "../../service/login.service";
 import { ToastrService } from "ngx-toastr";
-
+import { jwtDecode } from "jwt-decode";
+interface CustomJwtPayload {
+  exp: number;
+  scope: string;
+  iat: number;
+  preferred_username: string;
+  name: string;
+  email: string;
+  family_name: string;
+  given_name: string;
+  sub: string;
+}
 @Component({
   selector: "app-profil-information",
   templateUrl: "./profil-information.component.html",
@@ -15,9 +26,10 @@ export class ProfilInformationComponent {
   activeTab: string = "profile-info2";
   submitted = false;
   storedData: any;
+  currentUser: any;
   disableButton: boolean = false;
 
-  url: any = 'https://www.w3schools.com/howto/img_avatar.png';
+  url: any = "https://www.w3schools.com/howto/img_avatar.png";
   fileSelected: any = null;
 
   constructor(
@@ -28,12 +40,13 @@ export class ProfilInformationComponent {
 
   ngOnInit(): void {
     this.storedData = JSON.parse(localStorage.getItem("account_info"));
+    this.currentUser = JSON.parse(localStorage.getItem("currentUser"));
     this.profilInfo = this.formBuilder.group({
       firstName: ["", Validators.required],
       lastName: ["", Validators.required],
       email: ["", [Validators.required, Validators.email]],
       phone: ["", [Validators.required, Validators.pattern(/^[0-9]{10,15}$/)]],
-      image: [null]
+      image: [null],
     });
     this.dataProfil(this.storedData.sub);
     this.profilInfo.valueChanges.subscribe(() => {
@@ -42,15 +55,17 @@ export class ProfilInformationComponent {
   }
 
   dataProfil(userId) {
-    this.authService.getDataProfil(userId).subscribe(data => {
+    this.authService.getDataProfil(userId).subscribe((data) => {
       this.profilInfo.patchValue({
         firstName: data.data.firstname,
         lastName: data.data.lastname,
         email: data.data.email,
         phone: data.data.phone,
-        image: [null]
+        image: [null],
       });
-      if (data.data.image_path !== "https://kapi.absys.ninja/hemat/image/null") {
+      if (
+        data.data.image_path !== "https://kapi.absys.ninja/hemat/image/null"
+      ) {
         this.url = data.data.image_path;
       }
     });
@@ -60,13 +75,13 @@ export class ProfilInformationComponent {
     if (event.target.files && event.target.files[0]) {
       const file = event.target.files[0];
       this.fileSelected = file;
-      if (!file.type.startsWith('image')) {
+      if (!file.type.startsWith("image")) {
         this.toastr.error("Warning", "Please select an image file.", {
           timeOut: 5000,
           closeButton: true,
         });
         this.profilInfo.patchValue({
-          image: null
+          image: null,
         });
         return;
       }
@@ -119,11 +134,24 @@ export class ProfilInformationComponent {
     this.authService.updateProfile(formData, this.storedData.sub).subscribe(
       (response) => {
         console.log("Profile updated successfully:", response);
-        this.toastr.success("success", "Profile updated successfully.", {
-          timeOut: 5000,
-          closeButton: true,
-        });
-        // Handle success response
+        this.authService
+          .updateUserProfile(this.currentUser.refresh_token)
+          .subscribe((resp) => {
+            const decodedToken = jwtDecode(resp.access_token);
+            localStorage.setItem("account_info", JSON.stringify(decodedToken));
+            const userProfile = {
+              access_token: resp.access_token,
+              refresh_token: resp.refresh_token,
+              displayName: decodedToken.name,
+              buildingId: 4,
+            };
+            localStorage.setItem("currentUser", JSON.stringify(userProfile));
+            window.location.reload();
+            this.toastr.success("success", "Profile updated successfully.", {
+              timeOut: 5000,
+              closeButton: true,
+            });
+          });
       },
       (error) => {
         console.error("Profile update failed:", error);
diff --git a/src/app/login/login.component.html b/src/app/login/login.component.html
index 9ecc955..a351d69 100644
--- a/src/app/login/login.component.html
+++ b/src/app/login/login.component.html
@@ -138,7 +138,7 @@
                 
-                  (v@2024.07.8.03)
+                  (v@2024.07.9.01)