-
Notifications
You must be signed in to change notification settings - Fork 16
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 #2286 from Plant-for-the-Planet-org/feature/addres…
…s-mgmt BASE: Address Management
- Loading branch information
Showing
26 changed files
with
1,819 additions
and
199 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const KebabMenuIcon = () => { | ||
return ( | ||
<svg viewBox="0 0 12 20" fill="none" xmlns="http://www.w3.org/2000/svg"> | ||
<rect x="4" y="2" width="4" height="4" rx="2" fill="black" /> | ||
<rect x="4" y="8" width="4" height="4" rx="2" fill="black" /> | ||
<rect x="4" y="14" width="4" height="4" rx="2" fill="black" /> | ||
</svg> | ||
); | ||
}; | ||
|
||
export default KebabMenuIcon; |
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
114 changes: 114 additions & 0 deletions
114
src/features/user/Settings/EditProfile/AddressManagement/AddAddress.tsx
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,114 @@ | ||
import type { ExtendedCountryCode } from '../../../../common/types/country'; | ||
import type { SetState } from '../../../../common/types/common'; | ||
import type { Address, APIError } from '@planet-sdk/common'; | ||
import type { AddressAction } from '../../../../common/types/profile'; | ||
|
||
import { useState, useContext, useCallback } from 'react'; | ||
import { useTranslations } from 'next-intl'; | ||
import { handleError } from '@planet-sdk/common'; | ||
import { useUserProps } from '../../../../common/Layout/UserPropsContext'; | ||
import { postAuthenticatedRequest } from '../../../../../utils/apiRequests/api'; | ||
import { useTenant } from '../../../../common/Layout/TenantContext'; | ||
import { ErrorHandlingContext } from '../../../../common/Layout/ErrorHandlingContext'; | ||
import AddressForm from './microComponents/AddressForm'; | ||
import { ADDRESS_TYPE } from '../../../../../utils/addressManagement'; | ||
import AddressFormLayout from './microComponents/AddressFormLayout'; | ||
import { getStoredConfig } from '../../../../../utils/storeConfig'; | ||
|
||
export type FormData = { | ||
address: string | undefined; | ||
address2: string | null; | ||
city: string | undefined; | ||
zipCode: string | undefined; | ||
state: string | null; | ||
}; | ||
|
||
interface Props { | ||
setIsModalOpen: SetState<boolean>; | ||
setUserAddresses: SetState<Address[]>; | ||
setAddressAction: SetState<AddressAction | null>; | ||
} | ||
|
||
const defaultAddressDetail = { | ||
address: '', | ||
address2: '', | ||
city: '', | ||
zipCode: '', | ||
state: '', | ||
}; | ||
|
||
const AddAddress = ({ | ||
setIsModalOpen, | ||
setUserAddresses, | ||
setAddressAction, | ||
}: Props) => { | ||
const tAddressManagement = useTranslations('EditProfile.addressManagement'); | ||
const { contextLoaded, user, token, logoutUser } = useUserProps(); | ||
const configCountry = getStoredConfig('country'); | ||
const defaultCountry = user?.country || configCountry || 'DE'; | ||
const { tenantConfig } = useTenant(); | ||
const { setErrors } = useContext(ErrorHandlingContext); | ||
const [country, setCountry] = useState<ExtendedCountryCode | ''>( | ||
defaultCountry | ||
); | ||
const [isLoading, setIsLoading] = useState(false); | ||
|
||
const addAddress = useCallback( | ||
async (data: FormData) => { | ||
if (!contextLoaded || !user || !token) return; | ||
setIsLoading(true); | ||
const bodyToSend = { | ||
...data, | ||
country, | ||
type: ADDRESS_TYPE.OTHER, | ||
}; | ||
try { | ||
const res = await postAuthenticatedRequest<Address>( | ||
tenantConfig.id, | ||
'/app/addresses', | ||
bodyToSend, | ||
token, | ||
logoutUser | ||
); | ||
if (res && setUserAddresses) { | ||
setUserAddresses((prevAddresses) => [...prevAddresses, res]); | ||
} | ||
} catch (error) { | ||
setErrors(handleError(error as APIError)); | ||
} finally { | ||
setIsLoading(false); | ||
setIsModalOpen(false); | ||
setAddressAction(null); | ||
} | ||
}, | ||
[ | ||
contextLoaded, | ||
user, | ||
token, | ||
country, | ||
logoutUser, | ||
setUserAddresses, | ||
handleError, | ||
setIsLoading, | ||
setIsModalOpen, | ||
postAuthenticatedRequest, | ||
] | ||
); | ||
|
||
return ( | ||
<AddressFormLayout label={tAddressManagement('addressForm.addAddress')}> | ||
<AddressForm | ||
country={country} | ||
setCountry={setCountry} | ||
setIsModalOpen={setIsModalOpen} | ||
isLoading={isLoading} | ||
label={tAddressManagement('addressForm.addAddress')} | ||
defaultAddressDetail={defaultAddressDetail} | ||
processFormData={addAddress} | ||
setAddressAction={setAddressAction} | ||
/> | ||
</AddressFormLayout> | ||
); | ||
}; | ||
|
||
export default AddAddress; |
Oops, something went wrong.