-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathNamePage.tsx
114 lines (102 loc) · 4.54 KB
/
NamePage.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
import type {StackScreenProps} from '@react-navigation/stack';
import ExpensiMark from 'expensify-common/lib/ExpensiMark';
import React, {useCallback, useState} from 'react';
import {View} from 'react-native';
import FormProvider from '@components/Form/FormProvider';
import InputWrapper from '@components/Form/InputWrapper';
import type {FormOnyxValues} from '@components/Form/types';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import TextInput from '@components/TextInput';
import useAutoFocusInput from '@hooks/useAutoFocusInput';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import {renamePolicyTax, validateTaxName} from '@libs/actions/TaxRate';
import Navigation from '@libs/Navigation/Navigation';
import type {SettingsNavigatorParamList} from '@libs/Navigation/types';
import * as PolicyUtils from '@libs/PolicyUtils';
import NotFoundPage from '@pages/ErrorPage/NotFoundPage';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import type {WithPolicyAndFullscreenLoadingProps} from '@pages/workspace/withPolicyAndFullscreenLoading';
import withPolicyAndFullscreenLoading from '@pages/workspace/withPolicyAndFullscreenLoading';
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/WorkspaceTaxNameForm';
type NamePageProps = WithPolicyAndFullscreenLoadingProps & StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.TAX_NAME>;
const parser = new ExpensiMark();
function NamePage({
route: {
params: {policyID, taxID},
},
policy,
}: NamePageProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const currentTaxRate = PolicyUtils.getTaxByID(policy, taxID);
const {inputCallbackRef} = useAutoFocusInput();
const [name, setName] = useState(() => parser.htmlToMarkdown(currentTaxRate?.name ?? ''));
const goBack = useCallback(() => Navigation.goBack(ROUTES.WORKSPACE_TAX_EDIT.getRoute(policyID ?? '', taxID)), [policyID, taxID]);
const submit = () => {
renamePolicyTax(policyID, taxID, name);
goBack();
};
const validate = useCallback(
(values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_TAX_NAME_FORM>) => {
if (!policy) {
return {};
}
if (values[INPUT_IDS.NAME] === currentTaxRate?.name) {
return {};
}
return validateTaxName(policy, values);
},
[currentTaxRate?.name, policy],
);
if (!currentTaxRate) {
return <NotFoundPage />;
}
return (
<AccessOrNotFoundWrapper
accessVariants={[CONST.POLICY.ACCESS_VARIANTS.ADMIN, CONST.POLICY.ACCESS_VARIANTS.PAID]}
policyID={policyID}
featureName={CONST.POLICY.MORE_FEATURES.ARE_TAXES_ENABLED}
>
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
shouldEnableMaxHeight
testID={NamePage.displayName}
>
<HeaderWithBackButton
title={translate('common.name')}
onBackButtonPress={goBack}
/>
<FormProvider
formID={ONYXKEYS.FORMS.WORKSPACE_TAX_NAME_FORM}
submitButtonText={translate('workspace.editor.save')}
style={[styles.flexGrow1, styles.ph5]}
onSubmit={submit}
enabledWhenOffline
validate={validate}
>
<View style={styles.mb4}>
<InputWrapper
InputComponent={TextInput}
role={CONST.ROLE.PRESENTATION}
inputID={INPUT_IDS.NAME}
label={translate('workspace.editor.nameInputLabel')}
accessibilityLabel={translate('workspace.editor.nameInputLabel')}
value={name}
maxLength={CONST.TAX_RATES.NAME_MAX_LENGTH}
onChangeText={setName}
ref={inputCallbackRef}
/>
</View>
</FormProvider>
</ScreenWrapper>
</AccessOrNotFoundWrapper>
);
}
NamePage.displayName = 'NamePage';
export default withPolicyAndFullscreenLoading(NamePage);