-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ExitSurvey.ts
80 lines (73 loc) · 2.49 KB
/
ExitSurvey.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
import type {OnyxUpdate} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import * as API from '@libs/API';
import {SIDE_EFFECT_REQUEST_COMMANDS} from '@libs/API/types';
import ONYXKEYS from '@src/ONYXKEYS';
import REASON_INPUT_IDS from '@src/types/form/ExitSurveyReasonForm';
import type {ExitReason} from '@src/types/form/ExitSurveyReasonForm';
import RESPONSE_INPUT_IDS from '@src/types/form/ExitSurveyResponseForm';
let exitReason: ExitReason | undefined;
let exitSurveyResponse: string | undefined;
Onyx.connect({
key: ONYXKEYS.FORMS.EXIT_SURVEY_REASON_FORM,
callback: (value) => (exitReason = value?.[REASON_INPUT_IDS.REASON]),
});
Onyx.connect({
key: ONYXKEYS.FORMS.EXIT_SURVEY_RESPONSE_FORM,
callback: (value) => (exitSurveyResponse = value?.[RESPONSE_INPUT_IDS.RESPONSE]),
});
function saveExitReason(reason: ExitReason) {
Onyx.set(ONYXKEYS.FORMS.EXIT_SURVEY_REASON_FORM, {[REASON_INPUT_IDS.REASON]: reason});
}
function saveResponse(response: string) {
Onyx.set(ONYXKEYS.FORMS.EXIT_SURVEY_RESPONSE_FORM, {[RESPONSE_INPUT_IDS.RESPONSE]: response});
}
/**
* Save the user's response to the mandatory exit survey in the back-end.
*/
function switchToOldDot() {
const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.SET,
key: ONYXKEYS.IS_SWITCHING_TO_OLD_DOT,
value: true,
},
];
const finallyData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.SET,
key: ONYXKEYS.IS_SWITCHING_TO_OLD_DOT,
value: false,
},
{
onyxMethod: Onyx.METHOD.SET,
key: ONYXKEYS.FORMS.EXIT_SURVEY_REASON_FORM,
value: null,
},
{
onyxMethod: Onyx.METHOD.SET,
key: ONYXKEYS.FORMS.EXIT_SURVEY_REASON_FORM_DRAFT,
value: null,
},
{
onyxMethod: Onyx.METHOD.SET,
key: ONYXKEYS.FORMS.EXIT_SURVEY_RESPONSE_FORM,
value: null,
},
{
onyxMethod: Onyx.METHOD.SET,
key: ONYXKEYS.FORMS.EXIT_SURVEY_RESPONSE_FORM_DRAFT,
value: null,
},
];
// eslint-disable-next-line rulesdir/no-api-side-effects-method
return API.makeRequestWithSideEffects(
SIDE_EFFECT_REQUEST_COMMANDS.SWITCH_TO_OLD_DOT,
{
reason: exitReason,
surveyResponse: exitSurveyResponse,
},
{optimisticData, finallyData},
);
}
export {saveExitReason, saveResponse, switchToOldDot};