From 52540c9bce591213a23fe089b2a5cb581c2867d9 Mon Sep 17 00:00:00 2001 From: chaw Date: Thu, 30 Jan 2020 13:01:44 +0800 Subject: [PATCH] [PE-305] show tutorial page at first login --- src/pages/login/login.ts | 27 ++++++++++++++++++++++++--- src/pages/settings/settings.page.ts | 20 ++++++++++++-------- src/shared/cache/cache.service.ts | 10 ++++++++++ 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/pages/login/login.ts b/src/pages/login/login.ts index effd9c4e..109c296e 100644 --- a/src/pages/login/login.ts +++ b/src/pages/login/login.ts @@ -23,6 +23,8 @@ import {FormValidator} from '../../validators/formValidator'; // pages import { TabsPage } from '../../pages/tabs/tabs.page'; import { ForgetPasswordPage } from '../../pages/forget-password/forget-password'; +import { TutorialPage } from '../settings/tutorial/tutorial.page'; + /* This page is for handling user login process */ @Component({ selector: 'page-login', @@ -70,12 +72,25 @@ export class LoginPage { return false; } } + + private popupTutorial() { + // this.navCtrl.push(TutorialPage); + let tutorialModal = this.modalCtrl.create(TutorialPage); + tutorialModal.present(); + } + /** * user login function to authenticate user with email and password */ - userLogin() { + async userLogin() { let self = this; - this.cacheService.clear().then(() => { + const hasLoggedInBefore = await this.cacheService.hasBeenAccessed({ verify: true }); + + this.cacheService.clear().then(async () => { + if (hasLoggedInBefore) { // reset cache + await this.cacheService.hasBeenAccessed(); + } + // add loading effect during login process const loading = this.loadingCtrl.create({ dismissOnPageChange: true, @@ -84,7 +99,13 @@ export class LoginPage { loading.present().then(() => { // This part is calling post_auth() API from backend this.authService.loginAuth(this.email, this.password) - .subscribe(data => { + .subscribe(async (data) => { + if (!await this.cacheService.hasBeenAccessed({ verify: true })) { + // show tutorial + this.popupTutorial(); + await this.cacheService.hasBeenAccessed(); + } + // this.getLogInData(data); self.cacheService.setLocalObject('apikey', data.apikey); if (data.Experience.config) { diff --git a/src/pages/settings/settings.page.ts b/src/pages/settings/settings.page.ts index 523bfe81..97526951 100644 --- a/src/pages/settings/settings.page.ts +++ b/src/pages/settings/settings.page.ts @@ -75,7 +75,7 @@ export class SettingsPage { } // getting user data saved in cashe this.user = this.cache.getLocalObject('user'); - + } public getUserEmail() { return this.cache.getLocalObject('email') || ''; @@ -132,13 +132,17 @@ export class SettingsPage { spinner: 'hide', content: this.logoutMessage }); - loader.present().then(() => { - this.cache.clear().then(() => { - localStorage.clear(); - window.location.reload(); // the reason of doing this is because of we need to refresh page content instead of API data cache issue occurs - loader.dismiss(); - this.navCtrl.push(LoginPage); - }); + loader.present().then(async () => { + const accessedBefore = this.cache.hasBeenAccessed({ verify: true }); + await this.cache.clear(); + localStorage.clear(); + window.location.reload(); // the reason of doing this is because of we need to refresh page content instead of API data cache issue occurs + loader.dismiss(); + this.navCtrl.push(LoginPage); + // reset accessed cache + if (accessedBefore) { + this.cache.hasBeenAccessed(); + } }); } diff --git a/src/shared/cache/cache.service.ts b/src/shared/cache/cache.service.ts index e88ac87c..73f109d8 100755 --- a/src/shared/cache/cache.service.ts +++ b/src/shared/cache/cache.service.ts @@ -5,6 +5,9 @@ import { Cache } from '../app/cache'; import * as _ from 'lodash'; +// marking ID for accessed record +const HAS_ACCESSED = 'hasAccessed'; + @Injectable() export class CacheService { @@ -136,4 +139,11 @@ export class CacheService { const readonly = this.getLocalObject('enrolmentStatus'); return (readonly === 'readonly'); } + + public hasBeenAccessed(option?): Promise { + if (option && option.verify) { + return this.storage.get(HAS_ACCESSED); + } + return this.storage.set(HAS_ACCESSED, true); + } }