-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38381 from software-mansion-labs/travel/workspace…
…-profile-address
- Loading branch information
Showing
15 changed files
with
216 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// TODO: Change API endpoint parameters format to make it possible to follow naming-convention | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
type UpdatePolicyAddressParams = { | ||
policyID: string; | ||
'data[addressStreet]': string; | ||
'data[city]': string; | ||
'data[country]': string; | ||
'data[state]': string; | ||
'data[zipCode]': string; | ||
}; | ||
|
||
export default UpdatePolicyAddressParams; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import type {StackScreenProps} from '@react-navigation/stack'; | ||
import React, {useCallback, useEffect, useMemo, useState} from 'react'; | ||
import AddressForm from '@components/AddressForm'; | ||
import type {FormOnyxValues} from '@components/Form/types'; | ||
import HeaderWithBackButton from '@components/HeaderWithBackButton'; | ||
import ScreenWrapper from '@components/ScreenWrapper'; | ||
import useLocalize from '@hooks/useLocalize'; | ||
import Navigation from '@libs/Navigation/Navigation'; | ||
import type {SettingsNavigatorParamList} from '@libs/Navigation/types'; | ||
import {updateAddress} from '@userActions/Policy'; | ||
import type {Country} from '@src/CONST'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import type SCREENS from '@src/SCREENS'; | ||
import type {CompanyAddress} from '@src/types/onyx/Policy'; | ||
import type {WithPolicyProps} from './withPolicy'; | ||
import withPolicy from './withPolicy'; | ||
|
||
type WorkspaceProfileAddressPagePolicyProps = WithPolicyProps; | ||
|
||
type WorkspaceProfileAddressPageProps = StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.ADDRESS> & WorkspaceProfileAddressPagePolicyProps; | ||
|
||
function WorkspaceProfileAddressPage({policy}: WorkspaceProfileAddressPageProps) { | ||
const {translate} = useLocalize(); | ||
const address = useMemo(() => policy?.address, [policy]); | ||
const [currentCountry, setCurrentCountry] = useState(address?.country); | ||
const [[street1, street2], setStreets] = useState((address?.addressStreet ?? '').split('\n')); | ||
const [state, setState] = useState(address?.state); | ||
const [city, setCity] = useState(address?.city); | ||
const [zipcode, setZipcode] = useState(address?.zipCode); | ||
|
||
const updatePolicyAddress = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.HOME_ADDRESS_FORM>) => { | ||
if (!policy) { | ||
return; | ||
} | ||
updateAddress(policy?.id, { | ||
addressStreet: `${values.addressLine1?.trim() ?? ''}\n${values.addressLine2?.trim() ?? ''}`, | ||
city: values.city.trim(), | ||
state: values.state.trim(), | ||
zipCode: values?.zipPostCode?.trim().toUpperCase() ?? '', | ||
country: values.country, | ||
}); | ||
Navigation.goBack(); | ||
}; | ||
|
||
const handleAddressChange = useCallback((value: unknown, key: unknown) => { | ||
const countryValue = value as Country | ''; | ||
const addressKey = key as keyof CompanyAddress; | ||
|
||
if (addressKey !== 'country' && addressKey !== 'state' && addressKey !== 'city' && addressKey !== 'zipCode') { | ||
return; | ||
} | ||
if (addressKey === 'country') { | ||
setCurrentCountry(countryValue); | ||
setState(''); | ||
setCity(''); | ||
setZipcode(''); | ||
return; | ||
} | ||
if (addressKey === 'state') { | ||
setState(countryValue); | ||
setCity(''); | ||
setZipcode(''); | ||
return; | ||
} | ||
if (addressKey === 'city') { | ||
setCity(countryValue); | ||
setZipcode(''); | ||
return; | ||
} | ||
setZipcode(countryValue); | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (!address) { | ||
return; | ||
} | ||
setStreets((address?.addressStreet ?? '').split('\n')); | ||
setState(address.state); | ||
setCurrentCountry(address.country); | ||
setCity(address.city); | ||
setZipcode(address.zipCode); | ||
}, [address]); | ||
|
||
return ( | ||
<ScreenWrapper | ||
includeSafeAreaPaddingBottom={false} | ||
testID={WorkspaceProfileAddressPage.displayName} | ||
> | ||
<HeaderWithBackButton | ||
title={translate('common.companyAddress')} | ||
shouldShowBackButton | ||
onBackButtonPress={() => Navigation.goBack()} | ||
/> | ||
<AddressForm | ||
formID={ONYXKEYS.FORMS.HOME_ADDRESS_FORM} | ||
onSubmit={updatePolicyAddress} | ||
submitButtonText={translate('common.save')} | ||
city={city} | ||
country={currentCountry} | ||
onAddressChanged={handleAddressChange} | ||
state={state} | ||
street1={street1} | ||
street2={street2} | ||
zip={zipcode} | ||
/> | ||
</ScreenWrapper> | ||
); | ||
} | ||
|
||
WorkspaceProfileAddressPage.displayName = 'WorkspaceProfileAddressPage'; | ||
|
||
export default withPolicy(WorkspaceProfileAddressPage); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters