From 9a054ccf3cc4c8e3cbdc927d1127fc6f3a24edf3 Mon Sep 17 00:00:00 2001
From: Guilhem CARRON <gcarron@grandlyon.com>
Date: Tue, 13 Dec 2022 08:34:15 +0000
Subject: [PATCH] fix: prevent profile page and edition for uncomplete user to
 crash

---
 src/app/profile/edit/edit.component.ts          | 17 ++++++++++++++---
 .../profile-structure.component.html            |  2 +-
 .../profile-structure.component.ts              |  8 +++-----
 3 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/src/app/profile/edit/edit.component.ts b/src/app/profile/edit/edit.component.ts
index fc39fd088..8e272bcc2 100644
--- a/src/app/profile/edit/edit.component.ts
+++ b/src/app/profile/edit/edit.component.ts
@@ -56,6 +56,7 @@ export class EditComponent implements OnInit {
   public employers: Employer[];
   private selectedEmployer: Employer;
   private isAlreadySearching = false;
+  public isNewUser = false;
   @ViewChild('searchEmployer') searchEmployer: ElementRef;
   @ViewChild('newJobInput') newJobInput: ElementRef;
 
@@ -76,7 +77,9 @@ export class EditComponent implements OnInit {
       this.userProfile = new User(profile);
       this.initialUserProfile = { ...profile };
       this.selectedEmployer = { ...profile.employer };
-
+      if (!profile.employer || !profile.job) {
+        this.isNewUser = true;
+      }
       const otherJob = new Job({ name: 'Autre' });
       this.profileService.getJobs().subscribe((jobs) => {
         this.jobs = [...jobs, otherJob];
@@ -165,10 +168,18 @@ export class EditComponent implements OnInit {
         return this.passwordValid(this.newPassword) && this.newPassword == this.newPasswordConfirm;
       }
     } else if (this.currentTab === tabsEnum.employer) {
-      return !!(
+      if (this.isNewUser) {
+        if (!this.selectedEmployer || !this.selectedJob) {
+          return false;
+        }
+        return true;
+      } else if (
         this.selectedEmployer?.name !== this.userProfile.employer?.name ||
         this.selectedJob?.name !== this.userProfile.job?.name
-      );
+      ) {
+        return true;
+      }
+      return false;
     } else if (this.currentTab === tabsEnum.description) {
       return this.descriptionValid() && this.initialUserProfile.description !== this.userProfile.description;
     }
diff --git a/src/app/profile/profile-structure/profile-structure.component.html b/src/app/profile/profile-structure/profile-structure.component.html
index d151d648c..428635d98 100644
--- a/src/app/profile/profile-structure/profile-structure.component.html
+++ b/src/app/profile/profile-structure/profile-structure.component.html
@@ -130,7 +130,7 @@
         (click)="addMemberModalOpenned = true"
       ></app-button>
     </div>
-    <div class="call-to-action" *ngIf="!isPublic && !this.personalOffer && userProfile.job.hasPersonalOffer">
+    <div class="call-to-action" *ngIf="!isPublic && !this.personalOffer && userProfile.job?.hasPersonalOffer">
       <app-button
         [type]="'button'"
         [iconBtn]="'add'"
diff --git a/src/app/profile/profile-structure/profile-structure.component.ts b/src/app/profile/profile-structure/profile-structure.component.ts
index 3299f2af8..700c8fc30 100644
--- a/src/app/profile/profile-structure/profile-structure.component.ts
+++ b/src/app/profile/profile-structure/profile-structure.component.ts
@@ -68,11 +68,9 @@ export class ProfileStructureComponent implements OnInit {
     // Check if user has personal offers
 
     if (
-      !this.userProfile ||
-      !this.userProfile.job ||
-      !this.userProfile.personalOffers ||
-      !this.userProfile.job.hasPersonalOffer ||
-      this.userProfile.personalOffers.length === 0
+      !this.userProfile?.personalOffers ||
+      !this.userProfile?.job?.hasPersonalOffer ||
+      this.userProfile?.personalOffers?.length === 0
     )
       return null;
 
-- 
GitLab