1
- import React , { useCallback , useEffect , useState } from 'react' ;
1
+ import type { StackScreenProps } from '@react-navigation/stack' ;
2
+ import React , { useCallback , useEffect , useMemo , useState } from 'react' ;
2
3
import type { OnyxEntry } from 'react-native-onyx' ;
4
+ import { withOnyx } from 'react-native-onyx' ;
3
5
import AddressForm from '@components/AddressForm' ;
4
6
import FullscreenLoadingIndicator from '@components/FullscreenLoadingIndicator' ;
5
7
import HeaderWithBackButton from '@components/HeaderWithBackButton' ;
6
8
import ScreenWrapper from '@components/ScreenWrapper' ;
9
+ import useGeographicalStateFromRoute from '@hooks/useGeographicalStateFromRoute' ;
7
10
import useLocalize from '@hooks/useLocalize' ;
8
11
import useThemeStyles from '@hooks/useThemeStyles' ;
9
12
import Navigation from '@libs/Navigation/Navigation' ;
13
+ import type { SettingsNavigatorParamList } from '@libs/Navigation/types' ;
14
+ import * as PersonalDetails from '@userActions/PersonalDetails' ;
10
15
import type { FormOnyxValues } from '@src/components/Form/types' ;
16
+ import CONST from '@src/CONST' ;
11
17
import type { Country } from '@src/CONST' ;
12
18
import ONYXKEYS from '@src/ONYXKEYS' ;
19
+ import type SCREENS from '@src/SCREENS' ;
20
+ import type { PrivatePersonalDetails } from '@src/types/onyx' ;
13
21
import type { Address } from '@src/types/onyx/PrivatePersonalDetails' ;
14
22
15
- type AddressPageProps = {
23
+ type AddressPageOnyxProps = {
16
24
/** User's private personal details */
17
- address ?: Address ;
25
+ privatePersonalDetails : OnyxEntry < PrivatePersonalDetails > ;
18
26
/** Whether app is loading */
19
27
isLoadingApp : OnyxEntry < boolean > ;
20
- /** Function to call when address form is submitted */
21
- updateAddress : ( values : FormOnyxValues < typeof ONYXKEYS . FORMS . HOME_ADDRESS_FORM > ) => void ;
22
- /** Title of address page */
23
- title : string ;
24
28
} ;
25
29
26
- function AddressPage ( { title, address, updateAddress, isLoadingApp = true } : AddressPageProps ) {
30
+ type AddressPageProps = StackScreenProps < SettingsNavigatorParamList , typeof SCREENS . SETTINGS . PROFILE . ADDRESS > & AddressPageOnyxProps ;
31
+
32
+ /**
33
+ * Submit form to update user's first and last legal name
34
+ * @param values - form input values
35
+ */
36
+ function updateAddress ( values : FormOnyxValues < typeof ONYXKEYS . FORMS . HOME_ADDRESS_FORM > ) {
37
+ PersonalDetails . updateAddress (
38
+ values . addressLine1 ?. trim ( ) ?? '' ,
39
+ values . addressLine2 ?. trim ( ) ?? '' ,
40
+ values . city . trim ( ) ,
41
+ values . state . trim ( ) ,
42
+ values ?. zipPostCode ?. trim ( ) . toUpperCase ( ) ?? '' ,
43
+ values . country ,
44
+ ) ;
45
+ }
46
+
47
+ function AddressPage ( { privatePersonalDetails, route, isLoadingApp = true } : AddressPageProps ) {
27
48
const styles = useThemeStyles ( ) ;
28
49
const { translate} = useLocalize ( ) ;
50
+ const address = useMemo ( ( ) => privatePersonalDetails ?. address , [ privatePersonalDetails ] ) ;
51
+ const countryFromUrlTemp = route ?. params ?. country ;
29
52
30
53
// Check if country is valid
31
- const { street, street2} = address ?? { } ;
54
+ const countryFromUrl = CONST . ALL_COUNTRIES [ countryFromUrlTemp as keyof typeof CONST . ALL_COUNTRIES ] ? countryFromUrlTemp : '' ;
55
+ const stateFromUrl = useGeographicalStateFromRoute ( ) ;
32
56
const [ currentCountry , setCurrentCountry ] = useState ( address ?. country ) ;
57
+ const [ street1 , street2 ] = ( address ?. street ?? '' ) . split ( '\n' ) ;
33
58
const [ state , setState ] = useState ( address ?. state ) ;
34
59
const [ city , setCity ] = useState ( address ?. city ) ;
35
60
const [ zipcode , setZipcode ] = useState ( address ?. zip ) ;
@@ -72,13 +97,27 @@ function AddressPage({title, address, updateAddress, isLoadingApp = true}: Addre
72
97
setZipcode ( addressPart ) ;
73
98
} , [ ] ) ;
74
99
100
+ useEffect ( ( ) => {
101
+ if ( ! countryFromUrl ) {
102
+ return ;
103
+ }
104
+ handleAddressChange ( countryFromUrl , 'country' ) ;
105
+ } , [ countryFromUrl , handleAddressChange ] ) ;
106
+
107
+ useEffect ( ( ) => {
108
+ if ( ! stateFromUrl ) {
109
+ return ;
110
+ }
111
+ handleAddressChange ( stateFromUrl , 'state' ) ;
112
+ } , [ handleAddressChange , stateFromUrl ] ) ;
113
+
75
114
return (
76
115
< ScreenWrapper
77
116
includeSafeAreaPaddingBottom = { false }
78
117
testID = { AddressPage . displayName }
79
118
>
80
119
< HeaderWithBackButton
81
- title = { title }
120
+ title = { translate ( 'privatePersonalDetails.address' ) }
82
121
shouldShowBackButton
83
122
onBackButtonPress = { ( ) => Navigation . goBack ( ) }
84
123
/>
@@ -93,7 +132,7 @@ function AddressPage({title, address, updateAddress, isLoadingApp = true}: Addre
93
132
country = { currentCountry }
94
133
onAddressChanged = { handleAddressChange }
95
134
state = { state }
96
- street1 = { street }
135
+ street1 = { street1 }
97
136
street2 = { street2 }
98
137
zip = { zipcode }
99
138
/>
@@ -104,4 +143,11 @@ function AddressPage({title, address, updateAddress, isLoadingApp = true}: Addre
104
143
105
144
AddressPage . displayName = 'AddressPage' ;
106
145
107
- export default AddressPage ;
146
+ export default withOnyx < AddressPageProps , AddressPageOnyxProps > ( {
147
+ privatePersonalDetails : {
148
+ key : ONYXKEYS . PRIVATE_PERSONAL_DETAILS ,
149
+ } ,
150
+ isLoadingApp : {
151
+ key : ONYXKEYS . IS_LOADING_APP ,
152
+ } ,
153
+ } ) ( AddressPage ) ;
0 commit comments