forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkspaceEditCardLimitPage.tsx
158 lines (140 loc) · 7.16 KB
/
WorkspaceEditCardLimitPage.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
import type {StackScreenProps} from '@react-navigation/stack';
import React, {useCallback, useMemo, useState} from 'react';
import {useOnyx} from 'react-native-onyx';
import AmountForm from '@components/AmountForm';
import ConfirmModal from '@components/ConfirmModal';
import FormProvider from '@components/Form/FormProvider';
import InputWrapper from '@components/Form/InputWrapper';
import type {FormInputErrors, FormOnyxValues} from '@components/Form/types';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import useAutoFocusInput from '@hooks/useAutoFocusInput';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import * as ValidationUtils from '@libs/ValidationUtils';
import Navigation from '@navigation/Navigation';
import type {SettingsNavigatorParamList} from '@navigation/types';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import * as Card from '@userActions/Card';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import INPUT_IDS from '@src/types/form/EditExpensifyCardLimitForm';
type ConfirmationWarningTranslationPaths = 'workspace.expensifyCard.smartLimitWarning' | 'workspace.expensifyCard.monthlyLimitWarning' | 'workspace.expensifyCard.fixedLimitWarning';
// TODO: remove when Onyx data is available
const mockedCard = {
accountID: 885646,
availableSpend: 1000,
nameValuePairs: {
cardTitle: 'Test 1',
isVirtual: true,
limit: 2000,
limitType: CONST.EXPENSIFY_CARD.LIMIT_TYPES.SMART,
},
lastFourPAN: '1234',
};
type WorkspaceEditCardLimitPageProps = StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.EXPENSIFY_CARD_LIMIT>;
function WorkspaceEditCardLimitPage({route}: WorkspaceEditCardLimitPageProps) {
const {policyID, cardID} = route.params;
const {translate} = useLocalize();
const {inputCallbackRef} = useAutoFocusInput();
const styles = useThemeStyles();
const [isConfirmModalVisible, setIsConfirmModalVisible] = useState(false);
const [cardsList] = useOnyx(`${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${policyID}_${CONST.EXPENSIFY_CARD.BANK}`);
const card = cardsList?.[cardID] ?? mockedCard;
const getPromptTextKey = useMemo((): ConfirmationWarningTranslationPaths => {
switch (card.nameValuePairs?.limitType) {
case CONST.EXPENSIFY_CARD.LIMIT_TYPES.SMART:
return 'workspace.expensifyCard.smartLimitWarning';
case CONST.EXPENSIFY_CARD.LIMIT_TYPES.FIXED:
return 'workspace.expensifyCard.fixedLimitWarning';
case CONST.EXPENSIFY_CARD.LIMIT_TYPES.MONTHLY:
return 'workspace.expensifyCard.monthlyLimitWarning';
default:
return 'workspace.expensifyCard.fixedLimitWarning';
}
}, [card.nameValuePairs?.limitType]);
const updateCardLimit = (newLimit: string) => {
setIsConfirmModalVisible(false);
Card.updateExpensifyCardLimit(policyID, Number(cardID), Number(newLimit) * 100, card.nameValuePairs?.limit);
Navigation.goBack();
};
const submit = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.EDIT_EXPENSIFY_CARD_LIMIT_FORM>) => {
const currentLimit = card.nameValuePairs?.limit ?? 0;
const currentSpend = currentLimit - (card.availableSpend ?? 0);
const newLimit = Number(values[INPUT_IDS.LIMIT]) * 100;
const newAvailableSpend = newLimit - currentSpend;
if (newAvailableSpend <= 0) {
setIsConfirmModalVisible(true);
return;
}
updateCardLimit(values[INPUT_IDS.LIMIT]);
};
const validate = useCallback(
(values: FormOnyxValues<typeof ONYXKEYS.FORMS.EDIT_EXPENSIFY_CARD_LIMIT_FORM>): FormInputErrors<typeof ONYXKEYS.FORMS.EDIT_EXPENSIFY_CARD_LIMIT_FORM> => {
const errors = ValidationUtils.getFieldRequiredErrors(values, [INPUT_IDS.LIMIT]);
// We only want integers to be sent as the limit
if (!Number(values.limit) || !Number.isInteger(Number(values.limit))) {
errors.limit = translate('iou.error.invalidAmount');
}
return errors;
},
[translate],
);
return (
<AccessOrNotFoundWrapper
accessVariants={[CONST.POLICY.ACCESS_VARIANTS.ADMIN, CONST.POLICY.ACCESS_VARIANTS.PAID]}
policyID={policyID}
featureName={CONST.POLICY.MORE_FEATURES.ARE_EXPENSIFY_CARDS_ENABLED}
>
<ScreenWrapper
testID={WorkspaceEditCardLimitPage.displayName}
shouldEnablePickerAvoiding={false}
shouldEnableMaxHeight
>
<HeaderWithBackButton
title={translate('workspace.expensifyCard.cardLimit')}
onBackButtonPress={() => Navigation.goBack(ROUTES.WORKSPACE_EXPENSIFY_CARD_DETAILS.getRoute(policyID, cardID))}
/>
<FormProvider
formID={ONYXKEYS.FORMS.EDIT_EXPENSIFY_CARD_LIMIT_FORM}
submitButtonText={translate('common.save')}
shouldHideFixErrorsAlert
onSubmit={submit}
style={styles.flex1}
submitButtonStyles={[styles.mh5, styles.mt0]}
submitFlexEnabled={false}
enabledWhenOffline
validate={validate}
>
{({inputValues}) => (
<>
<InputWrapper
InputComponent={AmountForm}
defaultValue={CurrencyUtils.convertToFrontendAmountAsString(card.nameValuePairs?.limit, CONST.CURRENCY.USD, false)}
isCurrencyPressable={false}
inputID={INPUT_IDS.LIMIT}
ref={inputCallbackRef}
/>
<ConfirmModal
title={translate('workspace.expensifyCard.changeCardLimit')}
isVisible={isConfirmModalVisible}
onConfirm={() => updateCardLimit(inputValues[INPUT_IDS.LIMIT])}
onCancel={() => setIsConfirmModalVisible(false)}
prompt={translate(getPromptTextKey, CurrencyUtils.convertToDisplayString(Number(inputValues[INPUT_IDS.LIMIT]) * 100, CONST.CURRENCY.USD))}
confirmText={translate('workspace.expensifyCard.changeLimit')}
cancelText={translate('common.cancel')}
danger
shouldEnableNewFocusManagement
/>
</>
)}
</FormProvider>
</ScreenWrapper>
</AccessOrNotFoundWrapper>
);
}
WorkspaceEditCardLimitPage.displayName = 'WorkspaceEditCardLimitPage';
export default WorkspaceEditCardLimitPage;