Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PER-9916-exiting-onboarding-on-refresh #501

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 37 additions & 23 deletions src/app/core/guards/auth.guard.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,59 @@
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import {
ActivatedRouteSnapshot,
RouterStateSnapshot,
Router,
} from '@angular/router';
import { Observable } from 'rxjs';
import { AccountService } from '@shared/services/account/account.service';

@Injectable({
providedIn: 'root'
providedIn: 'root',
})
export class AuthGuard {
constructor (private account: AccountService, private router: Router) {}
export class AuthGuard {
constructor(
private account: AccountService,
private router: Router,
) {}

canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Promise<boolean> {
return this.account.checkSession()
.then((isSessionValid: boolean) => {
if (isSessionValid && this.account.isLoggedIn()) {
return true;
} else {
if (isSessionValid !== this.account.isLoggedIn()) {
this.account.clear();
state: RouterStateSnapshot,
): Promise<any> {
return this.account

Check warning on line 23 in src/app/core/guards/auth.guard.ts

View check run for this annotation

Codecov / codecov/patch

src/app/core/guards/auth.guard.ts#L23

Added line #L23 was not covered by tests
.checkSession()
.then((isSessionValid: boolean) => {
if (isSessionValid && this.account.isLoggedIn()) {
return true;

Check warning on line 27 in src/app/core/guards/auth.guard.ts

View check run for this annotation

Codecov / codecov/patch

src/app/core/guards/auth.guard.ts#L27

Added line #L27 was not covered by tests
} else {
if (isSessionValid !== this.account.isLoggedIn()) {
this.account.clear();

Check warning on line 30 in src/app/core/guards/auth.guard.ts

View check run for this annotation

Codecov / codecov/patch

src/app/core/guards/auth.guard.ts#L30

Added line #L30 was not covered by tests
}
this.router.navigate(['/app', 'auth', 'login'], {

Check warning on line 32 in src/app/core/guards/auth.guard.ts

View check run for this annotation

Codecov / codecov/patch

src/app/core/guards/auth.guard.ts#L32

Added line #L32 was not covered by tests
queryParams: next.queryParams,
});
return false;

Check warning on line 35 in src/app/core/guards/auth.guard.ts

View check run for this annotation

Codecov / codecov/patch

src/app/core/guards/auth.guard.ts#L35

Added line #L35 was not covered by tests
}
this.router.navigate(['/app', 'auth', 'login'], { queryParams: next.queryParams });
})
.catch(() => {
this.account.clear();
this.router.navigate(['/app', 'auth', 'login'], {

Check warning on line 40 in src/app/core/guards/auth.guard.ts

View check run for this annotation

Codecov / codecov/patch

src/app/core/guards/auth.guard.ts#L39-L40

Added lines #L39 - L40 were not covered by tests
queryParams: next.queryParams,
});
return false;
}
})
.catch(() => {
this.account.clear();
this.router.navigate(['/app', 'auth', 'login'], { queryParams: next.queryParams });
return false;
});
});
}

canActivateChild(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot
state: RouterStateSnapshot,
): Observable<boolean> | Promise<boolean> | boolean {
if (this.account.isLoggedIn()) {
return true;
} else {
this.router.navigate(['/app', 'auth', 'login'], { queryParams: next.queryParams });
this.router.navigate(['/app', 'onboarding'], {

Check warning on line 54 in src/app/core/guards/auth.guard.ts

View check run for this annotation

Codecov / codecov/patch

src/app/core/guards/auth.guard.ts#L54

Added line #L54 was not covered by tests
queryParams: next.queryParams,
});
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@
if (!this.isGlam) {
this.screen = 'create';
}

const screen = sessionStorage.getItem('onboardingScreen');
if (screen) {
this.screen = screen as NewArchiveScreen;
} else {
sessionStorage.setItem('onboardingScreen', this.screen);
}
}

ngOnInit(): void {
Expand All @@ -119,6 +126,26 @@
entity: 'account',
action: 'start_onboarding',
});

const storageGoals = sessionStorage.getItem('goals');
if (storageGoals) {
this.selectedGoals = JSON.parse(storageGoals);

Check warning on line 132 in src/app/onboarding/components/create-new-archive/create-new-archive.component.ts

View check run for this annotation

Codecov / codecov/patch

src/app/onboarding/components/create-new-archive/create-new-archive.component.ts#L132

Added line #L132 was not covered by tests
}

const storageReasons = sessionStorage.getItem('reasons');
if (storageReasons) {
this.selectedReasons = JSON.parse(storageReasons);

Check warning on line 137 in src/app/onboarding/components/create-new-archive/create-new-archive.component.ts

View check run for this annotation

Codecov / codecov/patch

src/app/onboarding/components/create-new-archive/create-new-archive.component.ts#L137

Added line #L137 was not covered by tests
}

const storageName = sessionStorage.getItem('archiveName');
if (storageName) {
this.name = storageName;

Check warning on line 142 in src/app/onboarding/components/create-new-archive/create-new-archive.component.ts

View check run for this annotation

Codecov / codecov/patch

src/app/onboarding/components/create-new-archive/create-new-archive.component.ts#L142

Added line #L142 was not covered by tests
}

const storageType = sessionStorage.getItem('archiveType');
if (storageType) {
this.archiveType = storageType;

Check warning on line 147 in src/app/onboarding/components/create-new-archive/create-new-archive.component.ts

View check run for this annotation

Codecov / codecov/patch

src/app/onboarding/components/create-new-archive/create-new-archive.component.ts#L147

Added line #L147 was not covered by tests
}
}

public onBackPress(): void {
Expand All @@ -132,6 +159,7 @@
this.screen = 'goals';
this.progress.emit(1);
}
sessionStorage.setItem('onboardingScreen', this.screen);

Check warning on line 162 in src/app/onboarding/components/create-new-archive/create-new-archive.component.ts

View check run for this annotation

Codecov / codecov/patch

src/app/onboarding/components/create-new-archive/create-new-archive.component.ts#L162

Added line #L162 was not covered by tests
}

public setScreen(screen: NewArchiveScreen): void {
Expand All @@ -147,6 +175,7 @@
action: action,
});
this.screen = screen;
sessionStorage.setItem('onboardingScreen', screen);
if (screen === 'reasons') {
this.progress.emit(2);
this.chartPathClicked.emit();
Expand Down Expand Up @@ -250,10 +279,12 @@
if (this.screen === 'goals') {
this.screen = 'reasons';
this.progress.emit(2);
sessionStorage.setItem('onboardingScreen', this.screen);

Check warning on line 282 in src/app/onboarding/components/create-new-archive/create-new-archive.component.ts

View check run for this annotation

Codecov / codecov/patch

src/app/onboarding/components/create-new-archive/create-new-archive.component.ts#L282

Added line #L282 was not covered by tests
this.selectedGoals = [];
} else if (this.screen === 'reasons') {
this.selectedReasons = [];
this.onSubmit();
sessionStorage.removeItem('onboardingScreen');

Check warning on line 287 in src/app/onboarding/components/create-new-archive/create-new-archive.component.ts

View check run for this annotation

Codecov / codecov/patch

src/app/onboarding/components/create-new-archive/create-new-archive.component.ts#L287

Added line #L287 was not covered by tests
}
}

Expand All @@ -264,8 +295,9 @@
private setName(archiveTypeTag: OnboardingTypes): void {
switch (archiveTypeTag) {
case OnboardingTypes.unsure:
const name = this.accountService.getAccount().fullName;
const name = this.accountService.getAccount()?.fullName;

Check warning on line 298 in src/app/onboarding/components/create-new-archive/create-new-archive.component.ts

View check run for this annotation

Codecov / codecov/patch

src/app/onboarding/components/create-new-archive/create-new-archive.component.ts#L298

Added line #L298 was not covered by tests
this.name = name;
sessionStorage.setItem('archiveName', name);

Check warning on line 300 in src/app/onboarding/components/create-new-archive/create-new-archive.component.ts

View check run for this annotation

Codecov / codecov/patch

src/app/onboarding/components/create-new-archive/create-new-archive.component.ts#L300

Added line #L300 was not covered by tests
break;
default:
this.name = '';
Expand All @@ -284,8 +316,10 @@
public handleCreationScreenEvents(event: Record<string, string>): void {
this.archiveTypeTag = event.tag as OnboardingTypes;
this.archiveType = event.type;
sessionStorage.setItem('archiveType', this.archiveType);
sessionStorage.setItem('archiveTypeTag', this.archiveTypeTag);

Check warning on line 320 in src/app/onboarding/components/create-new-archive/create-new-archive.component.ts

View check run for this annotation

Codecov / codecov/patch

src/app/onboarding/components/create-new-archive/create-new-archive.component.ts#L319-L320

Added lines #L319 - L320 were not covered by tests
this.headerText = event.headerText;
this.screen = event.screen as NewArchiveScreen;
this.setScreen(event.screen as NewArchiveScreen);

Check warning on line 322 in src/app/onboarding/components/create-new-archive/create-new-archive.component.ts

View check run for this annotation

Codecov / codecov/patch

src/app/onboarding/components/create-new-archive/create-new-archive.component.ts#L322

Added line #L322 was not covered by tests
}

public onValueChange(value: {
Expand All @@ -300,6 +334,7 @@

public navigateToGoals(event: string) {
this.name = event;
sessionStorage.setItem('archiveName', this.name);

Check warning on line 337 in src/app/onboarding/components/create-new-archive/create-new-archive.component.ts

View check run for this annotation

Codecov / codecov/patch

src/app/onboarding/components/create-new-archive/create-new-archive.component.ts#L337

Added line #L337 was not covered by tests
this.setScreen('goals');
}

Expand All @@ -319,4 +354,12 @@
this.selectedReasons = event.reasons;
this.setScreen(event.screen as NewArchiveScreen);
}

private clearSessionStorage(): void {
sessionStorage.removeItem('goals');
sessionStorage.removeItem('reasons');
sessionStorage.removeItem('archiveName');
sessionStorage.removeItem('archiveType');
sessionStorage.removeItem('archiveTypeTag');

Check warning on line 363 in src/app/onboarding/components/create-new-archive/create-new-archive.component.ts

View check run for this annotation

Codecov / codecov/patch

src/app/onboarding/components/create-new-archive/create-new-archive.component.ts#L359-L363

Added lines #L359 - L363 were not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export class CreateArchiveForMeScreenComponent {
this.onboardingService.registerArchive(
new ArchiveVO({ fullName: this.name, accessRole: 'access.role.owner' }),
);
sessionStorage.setItem('archiveName', this.name);
sessionStorage.setItem('archiveType', this.TYPE);
this.continueOutput.emit({
screen: 'goals',
type: this.TYPE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ describe('FinalizeArchiveCreationScreenComponent', () => {
});

it('should display the archive name correctly', async () => {
const name = 'Test Archive';
const name = 'John Doe';
onboardingService.registerArchive(new ArchiveVO({ fullName: name }));
const { find } = await shallow.render();
const { fixture, find } = await shallow.render();
const archiveNameElement = find('.archive-info p');

expect(archiveNameElement.nativeElement.textContent).toContain(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ describe('GlamGoalsScreenComponent', () => {

beforeEach(async () => {
shallow = new Shallow(GlamGoalsScreenComponent, OnboardingModule);

spyOn(sessionStorage, 'getItem').and.callFake((key) => {
const store = {
goals: JSON.stringify(['Mock Goal']),
};
return store[key] || null;
});

spyOn(sessionStorage, 'setItem').and.callFake((key, value) => {});
});

it('should create', async () => {
Expand All @@ -23,6 +32,25 @@ describe('GlamGoalsScreenComponent', () => {
expect(instance.goals).toEqual(goals);
});

it('should initialize selectedGoals from sessionStorage', async () => {
const { instance } = await shallow.render();

expect(sessionStorage.getItem).toHaveBeenCalledWith('goals');
expect(instance.selectedGoals).toEqual(['Mock Goal']);
});

it('should update sessionStorage when addGoal is called', async () => {
const { instance } = await shallow.render();
const goal = 'Test Goal';

instance.addGoal(goal);

expect(sessionStorage.setItem).toHaveBeenCalledWith(
'goals',
JSON.stringify(['Mock Goal', 'Test Goal']),
);
});

it('should add goal to selectedGoals when addGoal is called', async () => {
const { instance } = await shallow.render();
const goal = 'Test Goal';
Expand All @@ -46,7 +74,7 @@ describe('GlamGoalsScreenComponent', () => {

expect(outputs.goalsOutput.emit).toHaveBeenCalledWith({
screen: 'name-archive',
goals: [],
goals: ['Mock Goal'],
});
});

Expand All @@ -58,7 +86,7 @@ describe('GlamGoalsScreenComponent', () => {

expect(outputs.goalsOutput.emit).toHaveBeenCalledWith({
screen: 'reasons',
goals: [goal],
goals: ['Mock Goal', goal],
});
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* @format */
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { goals } from '../../../shared/onboarding-screen';

interface OutputModel {
Expand All @@ -12,7 +12,7 @@ interface OutputModel {
templateUrl: './glam-goals-screen.component.html',
styleUrl: './glam-goals-screen.component.scss',
})
export class GlamGoalsScreenComponent {
export class GlamGoalsScreenComponent implements OnInit {
public goals = [];
@Input() selectedGoals: string[] = [];
@Output() goalsOutput = new EventEmitter<OutputModel>();
Expand All @@ -21,6 +21,13 @@ export class GlamGoalsScreenComponent {
this.goals = goals;
}

ngOnInit(): void {
const storageGoals = sessionStorage.getItem('goals');
if (storageGoals) {
this.selectedGoals = JSON.parse(storageGoals);
}
}

backToNameArchive() {
this.goalsOutput.emit({
screen: 'name-archive',
Expand All @@ -40,5 +47,6 @@ export class GlamGoalsScreenComponent {
} else {
this.selectedGoals.push(goal);
}
sessionStorage.setItem('goals', JSON.stringify(this.selectedGoals));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ describe('GlamReasonsScreenComponent', () => {

beforeEach(async () => {
shallow = new Shallow(GlamReasonsScreenComponent, OnboardingModule);

spyOn(sessionStorage, 'getItem').and.callFake((key) => {
const store = {
reasons: JSON.stringify(['Mock Reason']),
};
return store[key] || null;
});

spyOn(sessionStorage, 'setItem').and.callFake((key, value) => {});
});

it('should create', async () => {
Expand All @@ -23,6 +32,25 @@ describe('GlamReasonsScreenComponent', () => {
expect(instance.reasons).toEqual(reasons);
});

it('should initialize selectedReasons from sessionStorage', async () => {
const { instance } = await shallow.render();

expect(sessionStorage.getItem).toHaveBeenCalledWith('reasons');
expect(instance.selectedReasons).toEqual(['Mock Reason']);
});

it('should update sessionStorage when addReason is called', async () => {
const { instance } = await shallow.render();
const reason = 'Test Reason';

instance.addReason(reason);

expect(sessionStorage.setItem).toHaveBeenCalledWith(
'reasons',
JSON.stringify(['Mock Reason', 'Test Reason']),
);
});

it('should add reason to selectedReasons when addReason is called', async () => {
const { instance } = await shallow.render();
const reason = 'Test Reason';
Expand All @@ -48,7 +76,7 @@ describe('GlamReasonsScreenComponent', () => {

expect(outputs.reasonsEmit.emit).toHaveBeenCalledWith({
screen: 'finalize',
reasons: [reason],
reasons: ['Mock Reason', reason],
});
});

Expand All @@ -60,7 +88,7 @@ describe('GlamReasonsScreenComponent', () => {

expect(outputs.reasonsEmit.emit).toHaveBeenCalledWith({
screen: 'goals',
reasons: [reason],
reasons: ['Mock Reason', reason],
});
});
});
Loading
Loading