From 08fe7fb08451d4d286f5013d9ad14b4e32afa50b Mon Sep 17 00:00:00 2001 From: Ansh Nanda Date: Fri, 25 Aug 2023 14:36:10 -0700 Subject: [PATCH] create onboarding model --- src/state/models/discovery/onboarding.ts | 63 ++++++++++++++++++++++++ src/state/models/root-store.ts | 6 +++ 2 files changed, 69 insertions(+) create mode 100644 src/state/models/discovery/onboarding.ts diff --git a/src/state/models/discovery/onboarding.ts b/src/state/models/discovery/onboarding.ts new file mode 100644 index 00000000..c75c49d0 --- /dev/null +++ b/src/state/models/discovery/onboarding.ts @@ -0,0 +1,63 @@ +import {makeAutoObservable} from 'mobx' +import {RootStoreModel} from '../root-store' +import {NavigationProp} from 'lib/routes/types' +import {hasProp} from 'lib/type-guards' + +enum OnboardingStep { + WELCOME = 'WELCOME', + BROWSE_FEEDS = 'BROWSE_FEEDS', + COMPLETE = 'COMPLETE', +} + +export class OnboardingModel { + // state + step: OnboardingStep + + constructor(public rootStore: RootStoreModel) { + makeAutoObservable(this, {rootStore: false}) + this.step = OnboardingStep.WELCOME + } + + serialize() { + return { + step: this.step, + } + } + + hydrate(v: unknown) { + if (typeof v === 'object' && v !== null) { + if (hasProp(v, 'step') && typeof v.step === 'string') { + this.step = v.step as OnboardingStep + } + } + } + + nextStep(navigation?: NavigationProp) { + switch (this.step) { + case OnboardingStep.WELCOME: + this.step = OnboardingStep.COMPLETE + break + case OnboardingStep.BROWSE_FEEDS: + this.step = OnboardingStep.COMPLETE + break + case OnboardingStep.COMPLETE: + if (!navigation) { + throw new Error('Navigation prop required to complete onboarding') + } + this.complete(navigation) + break + } + } + + complete(navigation: NavigationProp) { + navigation.navigate('Home') + } + + reset() { + this.step = OnboardingStep.WELCOME + } + + get isComplete() { + return this.step === OnboardingStep.COMPLETE + } +} diff --git a/src/state/models/root-store.ts b/src/state/models/root-store.ts index 1d6d3a0d..6204e0d1 100644 --- a/src/state/models/root-store.ts +++ b/src/state/models/root-store.ts @@ -27,6 +27,7 @@ import {reset as resetNavigation} from '../../Navigation' // remove after backend testing finishes // -prf import {applyDebugHeader} from 'lib/api/debug-appview-proxy-header' +import {OnboardingModel} from './discovery/onboarding' export const appInfo = z.object({ build: z.string(), @@ -44,6 +45,7 @@ export class RootStoreModel { shell = new ShellUiModel(this) preferences = new PreferencesModel(this) me = new MeModel(this) + onboarding = new OnboardingModel(this) invitedUsers = new InvitedUsers(this) handleResolutions = new HandleResolutionsCache() profiles = new ProfilesCache(this) @@ -70,6 +72,7 @@ export class RootStoreModel { appInfo: this.appInfo, session: this.session.serialize(), me: this.me.serialize(), + onboarding: this.onboarding.serialize(), shell: this.shell.serialize(), preferences: this.preferences.serialize(), invitedUsers: this.invitedUsers.serialize(), @@ -88,6 +91,9 @@ export class RootStoreModel { if (hasProp(v, 'me')) { this.me.hydrate(v.me) } + if (hasProp(v, 'onboarding')) { + this.onboarding.hydrate(v.onboarding) + } if (hasProp(v, 'session')) { this.session.hydrate(v.session) }