-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathIOURequestStepCategory.js
152 lines (134 loc) · 5.53 KB
/
IOURequestStepCategory.js
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
import lodashGet from 'lodash/get';
import PropTypes from 'prop-types';
import React from 'react';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import CategoryPicker from '@components/CategoryPicker';
import categoryPropTypes from '@components/categoryPropTypes';
import tagPropTypes from '@components/tagPropTypes';
import Text from '@components/Text';
import transactionPropTypes from '@components/transactionPropTypes';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import compose from '@libs/compose';
import Navigation from '@libs/Navigation/Navigation';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import * as ReportUtils from '@libs/ReportUtils';
import reportPropTypes from '@pages/reportPropTypes';
import * as IOU from '@userActions/IOU';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import {policyPropTypes} from '@src/pages/workspace/withPolicy';
import IOURequestStepRoutePropTypes from './IOURequestStepRoutePropTypes';
import StepScreenWrapper from './StepScreenWrapper';
import withFullTransactionOrNotFound from './withFullTransactionOrNotFound';
import withWritableReportOrNotFound from './withWritableReportOrNotFound';
const propTypes = {
/** Navigation route context info provided by react navigation */
route: IOURequestStepRoutePropTypes.isRequired,
/* Onyx Props */
/** Holds data related to Money Request view state, rather than the underlying Money Request data. */
transaction: transactionPropTypes,
/** The draft transaction that holds data to be persisted on the current transaction */
splitDraftTransaction: transactionPropTypes,
/** The report attached to the transaction */
report: reportPropTypes,
/** The policy of the report */
policy: policyPropTypes.policy,
/** Collection of categories attached to a policy */
policyCategories: PropTypes.objectOf(categoryPropTypes),
/** Collection of tags attached to a policy */
policyTags: tagPropTypes,
};
const defaultProps = {
report: {},
transaction: {},
splitDraftTransaction: {},
policy: null,
policyTags: null,
policyCategories: null,
};
function IOURequestStepCategory({
report,
route: {
params: {transactionID, backTo, action, iouType},
},
transaction,
splitDraftTransaction,
policy,
policyTags,
policyCategories,
}) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const isEditing = action === CONST.IOU.ACTION.EDIT;
const isEditingSplitBill = isEditing && iouType === CONST.IOU.TYPE.SPLIT;
const {category: transactionCategory} = ReportUtils.getTransactionDetails(isEditingSplitBill ? splitDraftTransaction : transaction);
const isPolicyExpenseChat = ReportUtils.isGroupPolicy(report);
// eslint-disable-next-line rulesdir/no-negated-variables
const shouldShowNotFoundPage = !isPolicyExpenseChat || (!transactionCategory && !OptionsListUtils.hasEnabledOptions(_.values(policyCategories)));
const navigateBack = () => {
Navigation.goBack(backTo);
};
/**
* @param {Object} category
* @param {String} category.searchText
*/
const updateCategory = (category) => {
const isSelectedCategory = category.searchText === transactionCategory;
const updatedCategory = isSelectedCategory ? '' : category.searchText;
// In the split flow, when editing we use SPLIT_TRANSACTION_DRAFT to save draft value
if (isEditingSplitBill) {
IOU.setDraftSplitTransaction(transaction.transactionID, {category: category.searchText});
navigateBack();
return;
}
if (isEditing) {
IOU.updateMoneyRequestCategory(transaction.transactionID, report.reportID, updatedCategory, policy, policyTags, policyCategories);
navigateBack();
return;
}
IOU.setMoneyRequestCategory(transactionID, updatedCategory);
navigateBack();
};
return (
<StepScreenWrapper
headerTitle={translate('common.category')}
onBackButtonPress={navigateBack}
shouldShowWrapper
shouldShowNotFoundPage={shouldShowNotFoundPage}
testID={IOURequestStepCategory.displayName}
>
<Text style={[styles.ph5, styles.pv3]}>{translate('iou.categorySelection')}</Text>
<CategoryPicker
selectedCategory={transactionCategory}
policyID={report.policyID}
onSubmit={updateCategory}
/>
</StepScreenWrapper>
);
}
IOURequestStepCategory.displayName = 'IOURequestStepCategory';
IOURequestStepCategory.propTypes = propTypes;
IOURequestStepCategory.defaultProps = defaultProps;
export default compose(
withWritableReportOrNotFound,
withFullTransactionOrNotFound,
withOnyx({
splitDraftTransaction: {
key: ({route}) => {
const transactionID = lodashGet(route, 'params.transactionID', 0);
return `${ONYXKEYS.COLLECTION.SPLIT_TRANSACTION_DRAFT}${transactionID}`;
},
},
policy: {
key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY}${report ? report.policyID : '0'}`,
},
policyCategories: {
key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${report ? report.policyID : '0'}`,
},
policyTags: {
key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY_TAGS}${report ? report.policyID : '0'}`,
},
}),
)(IOURequestStepCategory);