-
Notifications
You must be signed in to change notification settings - Fork 579
/
GlobalStoreModel.ts
174 lines (162 loc) · 5.85 KB
/
GlobalStoreModel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { LegacyNativeModules } from "app/NativeModules/LegacyNativeModules"
import { BottomTabsModel, getBottomTabsModel } from "app/Scenes/BottomTabs/BottomTabsModel"
import {
getMyCollectionModel,
MyCollectionModel,
} from "app/Scenes/MyCollection/State/MyCollectionModel"
import { DevicePrefsModel, getDevicePrefsModel } from "app/Scenes/MyProfile/DevicePrefsModel"
import { getSearchModel, SearchModel } from "app/Scenes/Search/SearchModel"
import { getUserPrefsModel, UserPrefsModel } from "app/Scenes/Search/UserPrefsModel"
import {
getSubmissionModel,
SubmissionModel,
} from "app/Scenes/SellWithArtsy/utils/submissionModelState"
import { unsafe__getEnvironment } from "app/store/GlobalStore"
import {
ProgressiveOnboardingModel,
getProgressiveOnboardingModel,
} from "app/store/ProgressiveOnboardingModel"
import { Action, action, createStore, State, thunkOn, ThunkOn } from "easy-peasy"
import { ArtsyPrefsModel, getArtsyPrefsModel } from "./ArtsyPrefsModel"
import { AuthModel, getAuthModel } from "./AuthModel"
import { getNativeModel, NativeModel } from "./NativeModel"
import {
getPendingPushNotificationModel,
PendingPushNotificationModel,
} from "./PendingPushNotificationModel"
import { getRecentPriceRangesModel, RecentPriceRangesModel } from "./RecentPriceRangesModel"
import {
getRequestedPriceEstimatesModel,
RequestedPriceEstimatesModel,
} from "./RequestedPriceEstimatesModel"
import { getToastModel, ToastModel } from "./ToastModel"
import { getVisualClueModel, VisualClueModel } from "./VisualClueModel"
import { CURRENT_APP_VERSION } from "./migration"
import { assignDeep, sanitize } from "./persistence"
type SessionState = {
isHydrated: boolean
isNavigationReady: boolean
}
interface GlobalStoreStateModel {
version: number
sessionState: SessionState
native: NativeModel
bottomTabs: BottomTabsModel
search: SearchModel
myCollection: MyCollectionModel
auth: AuthModel
toast: ToastModel
pendingPushNotification: PendingPushNotificationModel
artsyPrefs: ArtsyPrefsModel
userPrefs: UserPrefsModel
devicePrefs: DevicePrefsModel
visualClue: VisualClueModel
artworkSubmission: SubmissionModel
requestedPriceEstimates: RequestedPriceEstimatesModel
recentPriceRanges: RecentPriceRangesModel
progressiveOnboarding: ProgressiveOnboardingModel
}
export interface GlobalStoreModel extends GlobalStoreStateModel {
rehydrate: Action<this, DeepPartial<State<GlobalStoreStateModel>>>
reset: Action<this, DeepPartial<State<GlobalStoreStateModel>>>
resetAfterSignOut: ThunkOn<this>
didRehydrate: ThunkOn<this>
setSessionState: Action<this, Partial<SessionState>>
// for dev only.
_setVersion: Action<this, number>
// for testing only. noop otherwise.
__inject: Action<this, DeepPartial<State<GlobalStoreStateModel>>>
__manipulate: Action<this, (store: this) => void>
}
export const getGlobalStoreModel = (): GlobalStoreModel => ({
// META STATE
version: CURRENT_APP_VERSION,
rehydrate: action((state, unpersistedState) => {
if (!__TEST__ && state.sessionState.isHydrated) {
console.error("The store was already hydrated. `rehydrate` should only be called once.")
return
}
assignDeep(state, unpersistedState)
state.sessionState.isHydrated = true
}),
reset: action((_, state) => {
const result = createStore(getGlobalStoreModel()).getState()
result.sessionState.isHydrated = true
assignDeep(result, state)
return result
}),
resetAfterSignOut: thunkOn(
(a) => a.auth.signOut,
(actions, _, store) => {
const {
artsyPrefs: existingArtsyConfig,
devicePrefs: existingDeviceConfig,
search,
auth: { userID },
recentPriceRanges,
} = store.getState()
// keep existing config state
const artsyConfig = sanitize(existingArtsyConfig) as typeof existingArtsyConfig
const deviceConfig = sanitize(existingDeviceConfig) as typeof existingDeviceConfig
actions.reset({
artsyPrefs: artsyConfig,
devicePrefs: deviceConfig,
search,
recentPriceRanges,
auth: { previousSessionUserID: userID },
})
}
),
didRehydrate: thunkOn(
(actions) => actions.rehydrate,
() => {
LegacyNativeModules.ARNotificationsManager.reactStateUpdated(unsafe__getEnvironment())
LegacyNativeModules.ARNotificationsManager.didFinishBootstrapping()
}
),
sessionState: {
// we don't perform hydration at test time so let's set it to always true for tests
isHydrated: __TEST__,
isNavigationReady: false,
},
// NATIVE MIGRATION STATE
native: getNativeModel(),
// APP MODULE STATE
bottomTabs: getBottomTabsModel(),
search: getSearchModel(),
myCollection: getMyCollectionModel(),
artsyPrefs: getArtsyPrefsModel(),
auth: getAuthModel(),
toast: getToastModel(),
devicePrefs: getDevicePrefsModel(),
pendingPushNotification: getPendingPushNotificationModel(),
userPrefs: getUserPrefsModel(),
visualClue: getVisualClueModel(),
artworkSubmission: getSubmissionModel(),
requestedPriceEstimates: getRequestedPriceEstimatesModel(),
recentPriceRanges: getRecentPriceRangesModel(),
progressiveOnboarding: getProgressiveOnboardingModel(),
setSessionState: action((state, payload) => {
state.sessionState = { ...state.sessionState, ...payload }
}),
// for dev only.
_setVersion: action((state, newVersion) => {
state.version = newVersion
}),
// for testing only. noop otherwise.
__inject: __TEST__
? action((state, injectedState) => {
assignDeep(state, injectedState)
})
: action(() => {
console.error("Do not use this function outside of tests!!")
}),
__manipulate: __TEST__
? action((state, theEdits) => {
theEdits(state as unknown as GlobalStoreModel)
})
: action(() => {
console.error("Do not use this function outside of tests!!")
}),
})
export type GlobalStoreState = State<GlobalStoreModel>