-
Notifications
You must be signed in to change notification settings - Fork 3k
/
ReportFields.ts
209 lines (189 loc) · 7.61 KB
/
ReportFields.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import type {NullishDeep, OnyxCollection} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import * as API from '@libs/API';
import type {CreateWorkspaceReportFieldParams} from '@libs/API/parameters';
import {WRITE_COMMANDS} from '@libs/API/types';
import * as ErrorUtils from '@libs/ErrorUtils';
import * as ReportUtils from '@libs/ReportUtils';
import {generateFieldID} from '@libs/WorkspaceReportFieldsUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {WorkspaceReportFieldsForm} from '@src/types/form/WorkspaceReportFieldsForm';
import INPUT_IDS from '@src/types/form/WorkspaceReportFieldsForm';
import type {Policy, PolicyReportField} from '@src/types/onyx';
import type {OnyxData} from '@src/types/onyx/Request';
let listValues: string[];
let disabledListValues: boolean[];
Onyx.connect({
key: ONYXKEYS.FORMS.WORKSPACE_REPORT_FIELDS_FORM_DRAFT,
callback: (value) => {
if (!value) {
return;
}
listValues = value[INPUT_IDS.LIST_VALUES] ?? [];
disabledListValues = value[INPUT_IDS.DISABLED_LIST_VALUES] ?? [];
},
});
const allPolicies: OnyxCollection<Policy> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY,
callback: (value, key) => {
if (!key) {
return;
}
if (value === null || value === undefined) {
// If we are deleting a policy, we have to check every report linked to that policy
// and unset the draft indicator (pencil icon) alongside removing any draft comments. Clearing these values will keep the newly archived chats from being displayed in the LHN.
// More info: https://github.com/Expensify/App/issues/14260
const policyID = key.replace(ONYXKEYS.COLLECTION.POLICY, '');
const policyReports = ReportUtils.getAllPolicyReports(policyID);
const cleanUpMergeQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT}${string}`, NullishDeep<Report>> = {};
const cleanUpSetQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${string}` | `${typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${string}`, null> = {};
policyReports.forEach((policyReport) => {
if (!policyReport) {
return;
}
const {reportID} = policyReport;
cleanUpSetQueries[`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`] = null;
cleanUpSetQueries[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${reportID}`] = null;
});
Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, cleanUpMergeQueries);
Onyx.multiSet(cleanUpSetQueries);
delete allPolicies[key];
return;
}
allPolicies[key] = value;
},
});
/**
* Sets the initial form values for the workspace report fields form.
*/
function setInitialCreateReportFieldsForm() {
Onyx.set(ONYXKEYS.FORMS.WORKSPACE_REPORT_FIELDS_FORM_DRAFT, {
[INPUT_IDS.INITIAL_VALUE]: '',
});
}
/**
* Creates a new list value in the workspace report fields form.
*/
function createReportFieldsListValue(valueName: string) {
Onyx.merge(ONYXKEYS.FORMS.WORKSPACE_REPORT_FIELDS_FORM_DRAFT, {
[INPUT_IDS.LIST_VALUES]: [...listValues, valueName],
[INPUT_IDS.DISABLED_LIST_VALUES]: [...disabledListValues, false],
});
}
/**
* Renames a list value in the workspace report fields form.
*/
function renameReportFieldsListValue(valueIndex: number, newValueName: string) {
const listValuesCopy = [...listValues];
listValuesCopy[valueIndex] = newValueName;
Onyx.merge(ONYXKEYS.FORMS.WORKSPACE_REPORT_FIELDS_FORM_DRAFT, {
[INPUT_IDS.LIST_VALUES]: listValuesCopy,
});
}
/**
* Sets the enabled state of a list value in the workspace report fields form.
*/
function setReportFieldsListValueEnabled(valueIndexes: number[], enabled: boolean) {
const disabledListValuesCopy = [...disabledListValues];
valueIndexes.forEach((valueIndex) => {
disabledListValuesCopy[valueIndex] = !enabled;
});
Onyx.merge(ONYXKEYS.FORMS.WORKSPACE_REPORT_FIELDS_FORM_DRAFT, {
[INPUT_IDS.DISABLED_LIST_VALUES]: disabledListValuesCopy,
});
}
/**
* Deletes a list value from the workspace report fields form.
*/
function deleteReportFieldsListValue(valueIndexes: number[]) {
const listValuesCopy = [...listValues];
const disabledListValuesCopy = [...disabledListValues];
valueIndexes
.sort((a, b) => b - a)
.forEach((valueIndex) => {
listValuesCopy.splice(valueIndex, 1);
disabledListValuesCopy.splice(valueIndex, 1);
});
Onyx.merge(ONYXKEYS.FORMS.WORKSPACE_REPORT_FIELDS_FORM_DRAFT, {
[INPUT_IDS.LIST_VALUES]: listValuesCopy,
[INPUT_IDS.DISABLED_LIST_VALUES]: disabledListValuesCopy,
});
}
type CreateReportFieldArguments = Pick<WorkspaceReportFieldsForm, 'name' | 'type' | 'initialValue'>;
/**
* Creates a new report field.
*/
function createReportField(policyID: string, {name, type, initialValue}: CreateReportFieldArguments) {
const previousFieldList = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]?.fieldList ?? {};
const fieldID = generateFieldID(name);
const fieldKey = ReportUtils.getReportFieldKey(fieldID);
const newReportField: PolicyReportField = {
name,
type,
defaultValue: initialValue,
values: listValues,
disabledOptions: disabledListValues,
fieldID,
orderWeight: Object.keys(previousFieldList).length + 1,
deletable: false,
value: type === CONST.REPORT_FIELD_TYPES.LIST ? CONST.REPORT_FIELD_TYPES.LIST : null,
keys: [],
externalIDs: [],
isTax: false,
};
const onyxData: OnyxData = {
optimisticData: [
{
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
onyxMethod: Onyx.METHOD.MERGE,
value: {
fieldList: {
[fieldKey]: newReportField,
},
pendingFields: {
[fieldKey]: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
errorFields: null,
},
},
],
successData: [
{
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
onyxMethod: Onyx.METHOD.MERGE,
value: {
pendingFields: {
[fieldKey]: null,
},
errorFields: null,
},
},
],
failureData: [
{
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
onyxMethod: Onyx.METHOD.MERGE,
value: {
fieldList: {
[fieldKey]: null,
},
pendingFields: {
[fieldKey]: null,
},
errorFields: {
[fieldKey]: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('workspace.reportFields.genericFailureMessage'),
},
},
},
],
};
const parameters: CreateWorkspaceReportFieldParams = {
policyID,
reportFields: JSON.stringify([newReportField]),
};
API.write(WRITE_COMMANDS.CREATE_WORKSPACE_REPORT_FIELD, parameters, onyxData);
}
export type {CreateReportFieldArguments};
export {setInitialCreateReportFieldsForm, createReportFieldsListValue, renameReportFieldsListValue, setReportFieldsListValueEnabled, deleteReportFieldsListValue, createReportField};