Skip to content

Commit

Permalink
Opening training cards once
Browse files Browse the repository at this point in the history
  • Loading branch information
latin-panda committed Oct 7, 2024
1 parent 9e74aeb commit 6f64f8d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
5 changes: 1 addition & 4 deletions webapp/src/ts/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,10 +501,7 @@ export class AppComponent implements OnInit, AfterViewInit {
}

private displayTrainingCards() {
if (this.showPrivacyPolicy && !this.privacyPolicyAccepted) {
return;
}
if (!this.trainingCardFormId) {
if (!this.trainingCardFormId || (this.showPrivacyPolicy && !this.privacyPolicyAccepted)) {
return;
}

Expand Down
34 changes: 31 additions & 3 deletions webapp/src/ts/services/training-cards.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable } from '@angular/core';
import { Store } from '@ngrx/store';
import * as moment from 'moment';
import { v4 as uuid } from 'uuid';
import { first } from 'rxjs/operators';

import { XmlFormsService } from '@mm-services/xml-forms.service';
import { TrainingCardsComponent } from '@mm-modals/training-cards/training-cards.component';
Expand All @@ -17,7 +18,8 @@ export const TRAINING_PREFIX: string = 'training:';
providedIn: 'root'
})
export class TrainingCardsService {
private globalActions;
private globalActions: GlobalActions;
private readonly STORAGE_KEY_LAST_VIEWED_DATE = 'training-cards-last-viewed-date';

constructor(
private store: Store,
Expand All @@ -27,7 +29,7 @@ export class TrainingCardsService {
private sessionService: SessionService,
private routeSnapshotService: RouteSnapshotService,
) {
this.globalActions = new GlobalActions(store);
this.globalActions = new GlobalActions(this.store);
}

private getAvailableTrainingCards(xForms, userCtx) {
Expand Down Expand Up @@ -98,11 +100,22 @@ export class TrainingCardsService {
}

displayTrainingCards() {
if (this.hasBeenDisplayed()) {
return;
}

const routeSnapshot = this.routeSnapshotService.get();
if (routeSnapshot?.data?.hideTraining) {
return;
}
this.modalService.show(TrainingCardsComponent);

this.modalService
.show(TrainingCardsComponent)
.afterOpened()
.pipe(first())
.subscribe(() => {
window.localStorage.setItem(this.STORAGE_KEY_LAST_VIEWED_DATE, new Date().toISOString());
});
}

private async getFirstChronologicalForm(xForms) {
Expand Down Expand Up @@ -138,4 +151,19 @@ export class TrainingCardsService {
}
return `${TRAINING_PREFIX}${userName}:${uuid()}`;
}

private hasBeenDisplayed() {
const dateString = window.localStorage.getItem(this.STORAGE_KEY_LAST_VIEWED_DATE) || '';
const lastViewedDate = new Date(dateString);

if (isNaN(lastViewedDate.getTime())) {
return false;
}

lastViewedDate.setHours(0, 0, 0, 0);
const today = new Date();
today.setHours(0, 0, 0, 0);

return lastViewedDate >= today;
}
}

0 comments on commit 6f64f8d

Please sign in to comment.