From 6100d8935084d626d9bace649a08c4507e5bf396 Mon Sep 17 00:00:00 2001
From: Guilhem CARRON <gcarron@grandlyon.com>
Date: Fri, 13 May 2022 07:19:36 +0000
Subject: [PATCH] feat(progress): Add global progress bar percentage

---
 src/app/form/form-view/form-view.component.ts | 11 +++++---
 .../progress-bar/progress-bar.component.ts    | 25 ++++++++++++++++++-
 2 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/src/app/form/form-view/form-view.component.ts b/src/app/form/form-view/form-view.component.ts
index d5776889a..5a0f0df01 100644
--- a/src/app/form/form-view/form-view.component.ts
+++ b/src/app/form/form-view/form-view.component.ts
@@ -138,6 +138,10 @@ export class FormViewComponent implements OnInit {
   }
 
   private initPage(): void {
+    const profileFormSteps: number = Object.keys(profileFormStep).length / 2;
+    const personnalOfferFormSteps: number = Object.keys(personalOfferFormStep).length / 2 - 1;
+    const structureFormSteps: number = Object.keys(structureFormStep).length / 2;
+    const totalFormSteps: number = profileFormSteps + personnalOfferFormSteps + structureFormSteps;
     if (formType[this.routeParam] === formType.account) {
       this.nbSteps = 3;
       this.currentPage = accountFormStep.accountInfo;
@@ -146,14 +150,14 @@ export class FormViewComponent implements OnInit {
       this.currentForm = this.accountForm;
     }
     if (formType[this.routeParam] === formType.profile) {
-      this.nbSteps = Object.keys(profileFormStep).length / 2;
+      this.nbSteps = totalFormSteps;
       this.currentPage = profileFormStep.profileBeginningInfo;
       this.currentFormType = formType.profile;
       this.createProfileForm();
       this.currentForm = this.profileForm;
     }
     if (formType[this.routeParam] === formType.structure) {
-      this.nbSteps = 24;
+      this.nbSteps = totalFormSteps;
       this.currentPage = structureFormStep.structureChoice;
       this.currentFormType = formType.structure;
       this.structure = new Structure();
@@ -176,7 +180,7 @@ export class FormViewComponent implements OnInit {
       });
     }
     if (formType[this.routeParam] === formType.personaloffer) {
-      this.nbSteps = 3;
+      this.nbSteps = totalFormSteps;
       this.currentPage = personalOfferFormStep.personalOfferAccompaniment;
       this.currentFormType = formType.personaloffer;
       const newPersonalOffer: PersonalOffer = new PersonalOffer();
@@ -362,7 +366,6 @@ export class FormViewComponent implements OnInit {
   public linkStructureToUser(): void {
     this.structureService.joinStructure(this.structureForm.value._id, this.profile.email).subscribe((data) => {
       this.currentPage = structureFormStep.mailSentInfo;
-      this.nbSteps = structureFormStep.mailSentInfo;
       this.structure._id = this.structureForm.value._id;
       this.structure.structureName = this.structureForm.value.structureName;
     });
diff --git a/src/app/form/form-view/global-components/progress-bar/progress-bar.component.ts b/src/app/form/form-view/global-components/progress-bar/progress-bar.component.ts
index a6c49470c..0f67bb3c7 100644
--- a/src/app/form/form-view/global-components/progress-bar/progress-bar.component.ts
+++ b/src/app/form/form-view/global-components/progress-bar/progress-bar.component.ts
@@ -1,5 +1,8 @@
 import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';
 import { formType } from '../../formType.enum';
+import { personalOfferFormStep } from '../../personal-offer-form/personalOfferFormStep.enum';
+import { profileFormStep } from '../../profile-form/profileFormStep.enum';
+import { structureFormStep } from '../../structure-form/structureFormStep.enum';
 
 @Component({
   selector: 'app-progress-bar',
@@ -13,8 +16,28 @@ export class ProgressBarComponent implements OnChanges {
   @Input() nbSteps: number;
   public progressStatus: number;
   public formTypeEnum = formType;
+  public profileFormSteps: number = Object.keys(profileFormStep).length / 2;
+  public personnalOfferFormSteps: number = Object.keys(personalOfferFormStep).length / 2;
+  public structureFormSteps: number = Object.keys(structureFormStep).length / 2;
 
   ngOnChanges(changes: SimpleChanges): void {
-    if (changes.currentPage) this.progressStatus = ((this.currentPage + 1) / this.nbSteps) * 100;
+    if (changes.currentPage) {
+      switch (this.formType) {
+        case formType.profile: {
+          this.progressStatus = ((this.currentPage + 1) / this.nbSteps) * 100;
+          break;
+        }
+        case formType.structure: {
+          this.progressStatus = ((this.currentPage + this.profileFormSteps + 1) / this.nbSteps) * 100;
+          break;
+        }
+        case formType.personaloffer:
+          this.progressStatus =
+            ((this.currentPage + this.structureFormSteps + this.profileFormSteps + 1) / this.nbSteps) * 100;
+          break;
+        default:
+          this.progressStatus = ((this.currentPage + 1) / this.nbSteps) * 100;
+      }
+    }
   }
 }
-- 
GitLab