-
Notifications
You must be signed in to change notification settings - Fork 3k
/
WorkspaceInvoicingDetailsWebsite.tsx
101 lines (90 loc) · 4.63 KB
/
WorkspaceInvoicingDetailsWebsite.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
import type {StackScreenProps} from '@react-navigation/stack';
import {Str} from 'expensify-common';
import React from 'react';
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 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 * as Url from '@libs/Url';
import * as ValidationUtils from '@libs/ValidationUtils';
import Navigation from '@navigation/Navigation';
import type {SettingsNavigatorParamList} from '@navigation/types';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type SCREENS from '@src/SCREENS';
import INPUT_IDS from '@src/types/form/WorkspaceInvoicesCompanyWebsiteForm';
type WorkspaceInvoicingDetailsWebsiteProps = StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.INVOICES_COMPANY_WEBSITE>;
function WorkspaceInvoicingDetailsWebsite({route}: WorkspaceInvoicingDetailsWebsiteProps) {
const {policyID} = route.params;
const {translate} = useLocalize();
const {inputCallbackRef} = useAutoFocusInput();
const styles = useThemeStyles();
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const submit = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_INVOICES_COMPANY_WEBSITE_FORM>) => {
// TODO: implement UpdateInvoiceCompanyWebsite API call when it's supported
Navigation.goBack();
};
const validate = (
values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_INVOICES_COMPANY_WEBSITE_FORM>,
): FormInputErrors<typeof ONYXKEYS.FORMS.WORKSPACE_INVOICES_COMPANY_WEBSITE_FORM> => {
const errors = ValidationUtils.getFieldRequiredErrors(values, [INPUT_IDS.COMPANY_WEBSITE]);
if (values.companyWebsite) {
if (!ValidationUtils.isValidWebsite(values.companyWebsite)) {
errors.companyWebsite = translate('bankAccount.error.website');
} else {
const domain = Url.extractUrlDomain(values.companyWebsite);
if (!domain || !Str.isValidDomainName(domain)) {
errors.companyWebsite = translate('iou.invalidDomainError');
} else if (ValidationUtils.isPublicDomain(domain)) {
errors.companyWebsite = translate('iou.publicDomainError');
}
}
}
return errors;
};
return (
<AccessOrNotFoundWrapper
accessVariants={[CONST.POLICY.ACCESS_VARIANTS.ADMIN, CONST.POLICY.ACCESS_VARIANTS.PAID]}
policyID={policyID}
// TODO: uncomment when CONST.POLICY.MORE_FEATURES.ARE_INVOICES_ENABLED is supported
// featureName={CONST.POLICY.MORE_FEATURES.ARE_INVOICES_ENABLED}
>
<ScreenWrapper
testID={WorkspaceInvoicingDetailsWebsite.displayName}
shouldEnablePickerAvoiding={false}
shouldEnableMaxHeight
>
<HeaderWithBackButton title={translate('workspace.invoices.companyWebsite')} />
<FormProvider
formID={ONYXKEYS.FORMS.WORKSPACE_INVOICES_COMPANY_WEBSITE_FORM}
submitButtonText={translate('common.save')}
onSubmit={submit}
style={[styles.flex1, styles.mh5]}
enabledWhenOffline
validate={validate}
>
<InputWrapper
InputComponent={TextInput}
inputID={INPUT_IDS.COMPANY_WEBSITE}
label={translate('workspace.invoices.companyWebsite')}
accessibilityLabel={translate('workspace.invoices.companyWebsite')}
role={CONST.ROLE.PRESENTATION}
defaultValue={policy?.invoice?.companyWebsite}
ref={inputCallbackRef}
inputMode={CONST.INPUT_MODE.URL}
/>
</FormProvider>
</ScreenWrapper>
</AccessOrNotFoundWrapper>
);
}
WorkspaceInvoicingDetailsWebsite.displayName = 'WorkspaceInvoicingDetailsWebsite';
export default WorkspaceInvoicingDetailsWebsite;