forked from artsy/eigen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalStoreModel.ts
More file actions
142 lines (131 loc) · 4.71 KB
/
GlobalStoreModel.ts
File metadata and controls
142 lines (131 loc) · 4.71 KB
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
import { LegacyNativeModules } from "app/NativeModules/LegacyNativeModules"
import { BottomTabsModel, getBottomTabsModel } from "app/Scenes/BottomTabs/BottomTabsModel"
import {
getSubmissionModel,
SubmissionModel,
} from "app/Scenes/Consignments/Screens/SubmitArtworkOverview/State/SubmissionModel"
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 { Action, action, createStore, State, thunkOn, ThunkOn } from "easy-peasy"
import { ArtsyPrefsModel, getArtsyPrefsModel } from "./ArtsyPrefsModel"
import { AuthModel, getAuthModel } from "./AuthModel"
import { unsafe__getEnvironment } from "./GlobalStore"
import { CURRENT_APP_VERSION } from "./migration"
import { getNativeModel, NativeModel } from "./NativeModel"
import {
getPendingPushNotificationModel,
PendingPushNotificationModel,
} from "./PendingPushNotificationModel"
import { assignDeep, sanitize } from "./persistence"
import { getToastModel, ToastModel } from "./ToastModel"
import { getVisualClueModel, VisualClueModel } from "./VisualClueModel"
interface GlobalStoreStateModel {
version: number
sessionState: {
isHydrated: boolean
}
native: NativeModel
bottomTabs: BottomTabsModel
search: SearchModel
myCollection: MyCollectionModel
auth: AuthModel
toast: ToastModel
pendingPushNotification: PendingPushNotificationModel
artsyPrefs: ArtsyPrefsModel
userPrefs: UserPrefsModel
devicePrefs: DevicePrefsModel
visualClue: VisualClueModel
artworkSubmission: SubmissionModel
}
export interface GlobalStoreModel extends GlobalStoreStateModel {
rehydrate: Action<this, DeepPartial<State<GlobalStoreStateModel>>>
reset: Action<this, DeepPartial<State<GlobalStoreStateModel>>>
resetAfterSignOut: ThunkOn<this>
didRehydrate: ThunkOn<this>
// 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: existingConfig,
search,
auth: { userID },
} = store.getState()
// keep existing config state
const config = sanitize(existingConfig) as typeof existingConfig
actions.reset({ artsyPrefs: config, search, 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__,
},
// 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(),
// 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>