-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathAddressPage.tsx
108 lines (99 loc) · 3.87 KB
/
AddressPage.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
import React, {useCallback, useEffect, useState} from 'react';
import type {OnyxEntry} from 'react-native-onyx';
import AddressForm from '@components/AddressForm';
import FullscreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
import type {FormOnyxValues} from '@src/components/Form/types';
import type {Country} from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Address} from '@src/types/onyx/PrivatePersonalDetails';
type AddressPageProps = {
/** User's private personal details */
address?: Address;
/** Whether app is loading */
isLoadingApp: OnyxEntry<boolean>;
/** Function to call when address form is submitted */
updateAddress: (values: FormOnyxValues<typeof ONYXKEYS.FORMS.HOME_ADDRESS_FORM>) => void;
/** Title of address page */
title: string;
};
function AddressPage({title, address, updateAddress, isLoadingApp = true}: AddressPageProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
// Check if country is valid
const {street, street2} = address ?? {};
const [currentCountry, setCurrentCountry] = useState(address?.country);
const [state, setState] = useState(address?.state);
const [city, setCity] = useState(address?.city);
const [zipcode, setZipcode] = useState(address?.zip);
useEffect(() => {
if (!address) {
return;
}
setState(address.state);
setCurrentCountry(address.country);
setCity(address.city);
setZipcode(address.zip);
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, [address?.state, address?.country, address?.city, address?.zip]);
const handleAddressChange = useCallback((value: unknown, key: unknown) => {
const addressPart = value as string;
const addressPartKey = key as keyof Address;
if (addressPartKey !== 'country' && addressPartKey !== 'state' && addressPartKey !== 'city' && addressPartKey !== 'zipPostCode') {
return;
}
if (addressPartKey === 'country') {
setCurrentCountry(addressPart as Country | '');
setState('');
setCity('');
setZipcode('');
return;
}
if (addressPartKey === 'state') {
setState(addressPart);
setCity('');
setZipcode('');
return;
}
if (addressPartKey === 'city') {
setCity(addressPart);
setZipcode('');
return;
}
setZipcode(addressPart);
}, []);
return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
testID={AddressPage.displayName}
>
<HeaderWithBackButton
title={title}
shouldShowBackButton
onBackButtonPress={() => Navigation.goBack()}
/>
{isLoadingApp ? (
<FullscreenLoadingIndicator style={[styles.flex1, styles.pRelative]} />
) : (
<AddressForm
formID={ONYXKEYS.FORMS.HOME_ADDRESS_FORM}
onSubmit={updateAddress}
submitButtonText={translate('common.save')}
city={city}
country={currentCountry}
onAddressChanged={handleAddressChange}
state={state}
street1={street}
street2={street2}
zip={zipcode}
/>
)}
</ScreenWrapper>
);
}
AddressPage.displayName = 'AddressPage';
export default AddressPage;