-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Welcome.ts
137 lines (113 loc) · 3.68 KB
/
Welcome.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
import type {OnyxCollection} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import type {OnboardingPurposeType} from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type Onboarding from '@src/types/onyx/Onboarding';
import type OnyxPolicy from '@src/types/onyx/Policy';
import type {EmptyObject} from '@src/types/utils/EmptyObject';
let onboarding: Onboarding | [] | undefined;
let hasDismissedModal: boolean | undefined;
let isLoadingReportData = true;
type HasCompletedOnboardingFlowProps = {
onCompleted?: () => void;
onNotCompleted?: () => void;
};
let resolveIsReadyPromise: (value?: Promise<void>) => void | undefined;
let isServerDataReadyPromise = new Promise<void>((resolve) => {
resolveIsReadyPromise = resolve;
});
let resolveOnboardingFlowStatus: (value?: Promise<void>) => void | undefined;
let isOnboardingFlowStatusKnownPromise = new Promise<void>((resolve) => {
resolveOnboardingFlowStatus = resolve;
});
function onServerDataReady(): Promise<void> {
return isServerDataReadyPromise;
}
function isOnboardingFlowCompleted({onCompleted, onNotCompleted}: HasCompletedOnboardingFlowProps) {
isOnboardingFlowStatusKnownPromise.then(() => {
if (Array.isArray(onboarding) || onboarding?.hasCompletedGuidedSetupFlow === undefined) {
return;
}
if (onboarding?.hasCompletedGuidedSetupFlow) {
onCompleted?.();
} else {
onNotCompleted?.();
}
});
}
/**
* Check if onboarding data is ready in order to check if the user has completed onboarding or not
*/
function checkOnboardingDataReady() {
if (onboarding === undefined) {
return;
}
resolveOnboardingFlowStatus?.();
}
/**
* Check if user dismissed modal and if report data are loaded
*/
function checkServerDataReady() {
if (isLoadingReportData || hasDismissedModal === undefined) {
return;
}
resolveIsReadyPromise?.();
}
function setOnboardingPurposeSelected(value: OnboardingPurposeType) {
Onyx.set(ONYXKEYS.ONBOARDING_PURPOSE_SELECTED, value ?? null);
}
function setOnboardingAdminsChatReportID(adminsChatReportID?: string) {
Onyx.set(ONYXKEYS.ONBOARDING_ADMINS_CHAT_REPORT_ID, adminsChatReportID ?? null);
}
Onyx.connect({
key: ONYXKEYS.NVP_ONBOARDING,
initWithStoredValues: false,
callback: (value) => {
if (value === null) {
return;
}
onboarding = value;
checkOnboardingDataReady();
},
});
Onyx.connect({
key: ONYXKEYS.NVP_HAS_DISMISSED_IDLE_PANEL,
initWithStoredValues: true,
callback: (value) => {
hasDismissedModal = value ?? false;
checkServerDataReady();
},
});
Onyx.connect({
key: ONYXKEYS.IS_LOADING_REPORT_DATA,
initWithStoredValues: false,
callback: (value) => {
isLoadingReportData = value ?? false;
checkServerDataReady();
},
});
const allPolicies: OnyxCollection<OnyxPolicy> | EmptyObject = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY,
callback: (val, key) => {
if (!key) {
return;
}
if (val === null || val === undefined) {
delete allPolicies[key];
return;
}
allPolicies[key] = {...allPolicies[key], ...val};
},
});
function resetAllChecks() {
isServerDataReadyPromise = new Promise((resolve) => {
resolveIsReadyPromise = resolve;
});
isOnboardingFlowStatusKnownPromise = new Promise((resolve) => {
resolveOnboardingFlowStatus = resolve;
});
onboarding = undefined;
isLoadingReportData = true;
}
export {onServerDataReady, isOnboardingFlowCompleted, setOnboardingPurposeSelected, resetAllChecks, setOnboardingAdminsChatReportID};