-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathIOURequestStepDescription.tsx
196 lines (177 loc) · 9.11 KB
/
IOURequestStepDescription.tsx
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
import lodashIsEmpty from 'lodash/isEmpty';
import React, {useCallback, useMemo, useRef} from 'react';
import {View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import {useOnyx} from 'react-native-onyx';
import FormProvider from '@components/Form/FormProvider';
import InputWrapper from '@components/Form/InputWrapper';
import type {FormInputErrors, FormOnyxValues} from '@components/Form/types';
import TextInput from '@components/TextInput';
import useAutoFocusInput from '@hooks/useAutoFocusInput';
import useLocalize from '@hooks/useLocalize';
import usePolicy from '@hooks/usePolicy';
import useThemeStyles from '@hooks/useThemeStyles';
import {addErrorMessage} from '@libs/ErrorUtils';
import {shouldUseTransactionDraft} from '@libs/IOUUtils';
import Navigation from '@libs/Navigation/Navigation';
import {isMoneyRequestAction} from '@libs/ReportActionsUtils';
import {canEditMoneyRequest} from '@libs/ReportUtils';
import {areRequiredFieldsEmpty} from '@libs/TransactionUtils';
import variables from '@styles/variables';
import {setDraftSplitTransaction, setMoneyRequestDescription, updateMoneyRequestDescription} from '@userActions/IOU';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type SCREENS from '@src/SCREENS';
import INPUT_IDS from '@src/types/form/MoneyRequestDescriptionForm';
import type * as OnyxTypes from '@src/types/onyx';
import DiscardChangesConfirmation from './DiscardChangesConfirmation';
import StepScreenWrapper from './StepScreenWrapper';
import withFullTransactionOrNotFound from './withFullTransactionOrNotFound';
import type {WithWritableReportOrNotFoundProps} from './withWritableReportOrNotFound';
import withWritableReportOrNotFound from './withWritableReportOrNotFound';
type IOURequestStepDescriptionProps = WithWritableReportOrNotFoundProps<typeof SCREENS.MONEY_REQUEST.STEP_DESCRIPTION> & {
/** Holds data related to Expense view state, rather than the underlying Expense data. */
transaction: OnyxEntry<OnyxTypes.Transaction>;
};
function IOURequestStepDescription({
route: {
params: {action, iouType, reportID, backTo, reportActionID, transactionID},
},
transaction,
report,
}: IOURequestStepDescriptionProps) {
const policy = usePolicy(report?.policyID);
const [splitDraftTransaction] = useOnyx(`${ONYXKEYS.COLLECTION.SPLIT_TRANSACTION_DRAFT}${transactionID}`);
const [policyCategories] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${report?.policyID}`);
const [policyTags] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${report?.policyID}`);
const reportActionsReportID = useMemo(() => {
let actionsReportID;
if (action === CONST.IOU.ACTION.EDIT) {
actionsReportID = iouType === CONST.IOU.TYPE.SPLIT ? report?.reportID : report?.parentReportID;
}
return actionsReportID;
}, [action, iouType, report?.reportID, report?.parentReportID]);
const [reportAction] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportActionsReportID}`, {
canEvict: false,
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
selector: (reportActions) => reportActions?.[report?.parentReportActionID || reportActionID],
});
const [session] = useOnyx(ONYXKEYS.SESSION);
const styles = useThemeStyles();
const {translate} = useLocalize();
const {inputCallbackRef} = useAutoFocusInput(true);
// In the split flow, when editing we use SPLIT_TRANSACTION_DRAFT to save draft value
const isEditingSplitBill = iouType === CONST.IOU.TYPE.SPLIT && action === CONST.IOU.ACTION.EDIT;
const currentDescription = isEditingSplitBill && !lodashIsEmpty(splitDraftTransaction) ? splitDraftTransaction?.comment?.comment ?? '' : transaction?.comment?.comment ?? '';
const descriptionRef = useRef(currentDescription);
const isSavedRef = useRef(false);
/**
* @returns - An object containing the errors for each inputID
*/
const validate = useCallback(
(values: FormOnyxValues<typeof ONYXKEYS.FORMS.MONEY_REQUEST_DESCRIPTION_FORM>): FormInputErrors<typeof ONYXKEYS.FORMS.MONEY_REQUEST_DESCRIPTION_FORM> => {
const errors = {};
if (values.moneyRequestComment.length > CONST.DESCRIPTION_LIMIT) {
addErrorMessage(
errors,
'moneyRequestComment',
translate('common.error.characterLimitExceedCounter', {length: values.moneyRequestComment.length, limit: CONST.DESCRIPTION_LIMIT}),
);
}
return errors;
},
[translate],
);
const navigateBack = () => {
Navigation.goBack(backTo);
};
const updateDescriptionRef = (value: string) => {
descriptionRef.current = value;
};
const updateComment = (value: FormOnyxValues<typeof ONYXKEYS.FORMS.MONEY_REQUEST_DESCRIPTION_FORM>) => {
if (!transaction?.transactionID) {
return;
}
isSavedRef.current = true;
const newComment = value.moneyRequestComment.trim();
// Only update comment if it has changed
if (newComment === currentDescription) {
navigateBack();
return;
}
// In the split flow, when editing we use SPLIT_TRANSACTION_DRAFT to save draft value
if (isEditingSplitBill) {
setDraftSplitTransaction(transaction?.transactionID, {comment: newComment});
navigateBack();
return;
}
const isTransactionDraft = shouldUseTransactionDraft(action);
setMoneyRequestDescription(transaction?.transactionID, newComment, isTransactionDraft);
if (action === CONST.IOU.ACTION.EDIT) {
updateMoneyRequestDescription(transaction?.transactionID, reportID, newComment, policy, policyTags, policyCategories);
}
navigateBack();
};
const isEditing = action === CONST.IOU.ACTION.EDIT;
const isSplitBill = iouType === CONST.IOU.TYPE.SPLIT;
const canEditSplitBill = isSplitBill && reportAction && session?.accountID === reportAction.actorAccountID && areRequiredFieldsEmpty(transaction);
// eslint-disable-next-line rulesdir/no-negated-variables
const shouldShowNotFoundPage = isEditing && (isSplitBill ? !canEditSplitBill : !isMoneyRequestAction(reportAction) || !canEditMoneyRequest(reportAction));
const isReportInGroupPolicy = !!report?.policyID && report.policyID !== CONST.POLICY.ID_FAKE;
const getDescriptionHint = () => {
return transaction?.category && policyCategories ? policyCategories[transaction?.category]?.commentHint ?? '' : '';
};
return (
<StepScreenWrapper
headerTitle={translate('common.description')}
onBackButtonPress={navigateBack}
shouldShowWrapper
testID={IOURequestStepDescription.displayName}
shouldShowNotFoundPage={shouldShowNotFoundPage}
>
<FormProvider
style={[styles.flexGrow1, styles.ph5]}
formID={ONYXKEYS.FORMS.MONEY_REQUEST_DESCRIPTION_FORM}
onSubmit={updateComment}
validate={validate}
submitButtonText={translate('common.save')}
enabledWhenOffline
>
<View style={styles.mb4}>
<InputWrapper
valueType="string"
InputComponent={TextInput}
inputID={INPUT_IDS.MONEY_REQUEST_COMMENT}
name={INPUT_IDS.MONEY_REQUEST_COMMENT}
defaultValue={currentDescription}
onValueChange={updateDescriptionRef}
label={translate('moneyRequestConfirmationList.whatsItFor')}
accessibilityLabel={translate('moneyRequestConfirmationList.whatsItFor')}
role={CONST.ROLE.PRESENTATION}
autoGrowHeight
maxAutoGrowHeight={variables.textInputAutoGrowMaxHeight}
shouldSubmitForm
type="markdown"
excludedMarkdownStyles={!isReportInGroupPolicy ? ['mentionReport'] : []}
ref={inputCallbackRef}
hint={getDescriptionHint()}
/>
</View>
</FormProvider>
<DiscardChangesConfirmation
getHasUnsavedChanges={() => {
if (isSavedRef.current) {
return false;
}
return descriptionRef.current !== currentDescription;
}}
/>
</StepScreenWrapper>
);
}
IOURequestStepDescription.displayName = 'IOURequestStepDescription';
// eslint-disable-next-line rulesdir/no-negated-variables
const IOURequestStepDescriptionWithFullTransactionOrNotFound = withFullTransactionOrNotFound(IOURequestStepDescription);
// eslint-disable-next-line rulesdir/no-negated-variables
const IOURequestStepDescriptionWithWritableReportOrNotFound = withWritableReportOrNotFound(IOURequestStepDescriptionWithFullTransactionOrNotFound);
export default IOURequestStepDescriptionWithWritableReportOrNotFound;