Skip to content

Commit

Permalink
fix: move flag out of session state so it stays between sessions (#8601)
Browse files Browse the repository at this point in the history
* move flag out of session state so it stays between sessions

* add safe area insets on Android

* set the state in same place we set other state

* add a migration for the push state

* fix test
  • Loading branch information
brainbicycle authored and MounirDhahri committed May 3, 2023
1 parent 8b06b6d commit cd66ccb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
9 changes: 4 additions & 5 deletions src/app/store/AuthModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ export interface AuthPromiseRejectType {
type SessionState = {
isLoading: boolean
isUserIdentified: boolean
requestedPushPermissionsThisSession: boolean
}

export interface AuthModel {
Expand All @@ -195,6 +194,7 @@ export interface AuthModel {
previousSessionUserID: string | null

userHasArtsyEmail: Computed<this, boolean, GlobalStoreModel>
requestedPushPermissionsThisSession: boolean

// Actions
setState: Action<this, Partial<StateMapper<this, "1">>>
Expand Down Expand Up @@ -265,7 +265,6 @@ export const getAuthModel = (): AuthModel => ({
sessionState: {
isLoading: false,
isUserIdentified: false,
requestedPushPermissionsThisSession: false,
},
userID: null,
userAccessToken: null,
Expand All @@ -277,6 +276,7 @@ export const getAuthModel = (): AuthModel => ({
userEmail: null,
previousSessionUserID: null,
userHasArtsyEmail: computed((state) => isArtsyEmail(state.userEmail ?? "")),
requestedPushPermissionsThisSession: false,

setState: action((state, payload) => Object.assign(state, payload)),
setSessionState: action((state, payload) => {
Expand Down Expand Up @@ -408,6 +408,8 @@ export const getAuthModel = (): AuthModel => ({
userID: user.id,
userEmail: email,
onboardingState: onboardingState ?? "complete",
// Make sure we try to get push permission and new tokens on new sessions
requestedPushPermissionsThisSession: false,
})

if (oauthProvider === "email") {
Expand All @@ -429,9 +431,6 @@ export const getAuthModel = (): AuthModel => ({

postEventToProviders(tracks.loggedIn(oauthProvider))

// Make sure we try to get push permission and new tokens on new sessions
GlobalStore.actions.auth.setSessionState({ requestedPushPermissionsThisSession: false })

onSignIn?.()

// Setting up user prefs from gravity after successsfull login.
Expand Down
18 changes: 18 additions & 0 deletions src/app/store/__tests__/migration.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -832,3 +832,21 @@ describe("App version Versions.AddOnboardingArtQuizStateToAuthModel", () => {
expect(migratedState.auth.onboardingArtQuizState).toEqual("none")
})
})

describe("App version Versions.AddPushPromptStateToAuthModel", () => {
const migrationToTest = Versions.AddPushPromptStateToAuthModel

it("adds requestedPushPermissionsThisSession to the auth model", () => {
const previousState = migrate({
state: { version: 0 },
toVersion: migrationToTest - 1,
})

const migratedState = migrate({
state: previousState,
toVersion: migrationToTest,
}) as any

expect(migratedState.auth.requestedPushPermissionsThisSession).toEqual(false)
})
})
6 changes: 5 additions & 1 deletion src/app/store/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ export const Versions = {
MoveEnvironmentToDevicePrefsAndRenameAdminToLocal: 34,
AddCategoryToSubmissionArtworkDetails: 35,
AddOnboardingArtQuizStateToAuthModel: 36,
AddPushPromptStateToAuthModel: 37,
}

export const CURRENT_APP_VERSION = Versions.AddOnboardingArtQuizStateToAuthModel
export const CURRENT_APP_VERSION = Versions.AddPushPromptStateToAuthModel

export type Migrations = Record<number, (oldState: any) => any>
export const artsyAppMigrations: Migrations = {
Expand Down Expand Up @@ -271,6 +272,9 @@ export const artsyAppMigrations: Migrations = {
[Versions.AddOnboardingArtQuizStateToAuthModel]: (state) => {
state.auth.onboardingArtQuizState = "none"
},
[Versions.AddPushPromptStateToAuthModel]: (state) => {
state.auth.requestedPushPermissionsThisSession = false
},
}

export function migrate<State extends { version: number }>({
Expand Down
8 changes: 6 additions & 2 deletions src/app/utils/PushNotification.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import AsyncStorage from "@react-native-async-storage/async-storage"
import { LegacyNativeModules } from "app/NativeModules/LegacyNativeModules"
import { getCurrentEmissionState, GlobalStore } from "app/store/GlobalStore"
import {
getCurrentEmissionState,
GlobalStore,
unsafe__getEnvironment,
unsafe_getUserAccessToken,
} from "app/store/GlobalStore"
import { PendingPushNotification } from "app/store/PendingPushNotificationModel"
import { unsafe__getEnvironment, unsafe_getUserAccessToken } from "app/store/GlobalStore"
import { navigate } from "app/system/navigation/navigate"
import { Alert, Linking, Platform } from "react-native"
import DeviceInfo from "react-native-device-info"
Expand Down

0 comments on commit cd66ccb

Please sign in to comment.