Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PWA-245] Shipping Information (Authenticated) #2380

Merged
merged 35 commits into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
7396499
Automatically set default shipping address if available
tjwiebell Apr 22, 2020
ce72529
Automatically set default shipping address if available
tjwiebell Apr 22, 2020
d2639af
Handle use case for customer entering address for first time
tjwiebell Apr 29, 2020
38f6753
Stub out functionality that will toggle between two views on checkout…
tjwiebell Apr 30, 2020
d0556cf
Merge branch 'tommy/auth-shipping-info' of github.com:magento/pwa-stu…
tjwiebell Apr 30, 2020
e32ac0b
- Complete UI for Address Book
tjwiebell May 1, 2020
ce0d2bc
Complete changes to address edit to support editing customer address
tjwiebell May 1, 2020
9fc603f
Complete the rest of the new address flow and cleanup
tjwiebell May 4, 2020
c8bb3ac
Run linter
tjwiebell May 4, 2020
c862a2c
Big decomp of customer logic into dedicated components
tjwiebell May 7, 2020
92a7272
- Select the current address on inital load of address book
tjwiebell May 8, 2020
f92022e
Address feedback from UX
tjwiebell May 11, 2020
5246c8b
Complete rest of PR feedback in prep for UX review
tjwiebell May 11, 2020
5008370
Fix the GraphQL validation errors
tjwiebell May 11, 2020
9c4eb29
Merge branch 'develop' into tommy/auth-shipping-info
tjwiebell May 12, 2020
945a480
- Remove Customer data from cache during login/create actions
tjwiebell May 14, 2020
832dc72
Different approach to list position and disabled checkbox
tjwiebell May 14, 2020
d4727e2
[PWA-562] Checkout (auth): Stored Address Follow Up (#2403)
tjwiebell May 20, 2020
b5aec84
Address PR feedback and start working on getting existing tests to pass.
tjwiebell May 26, 2020
9ef263d
Switched modifier class for checkout page
tjwiebell May 26, 2020
dbfe4c4
Cast our regionId to string so isRequired validation works
tjwiebell May 26, 2020
aa19861
Merge branch 'develop' into tommy/auth-shipping-info
tjwiebell May 27, 2020
b57b4b8
Merge branch 'develop' into tommy/auth-shipping-info
tjwiebell May 27, 2020
b903477
Prettier and fix existing failing test
tjwiebell May 27, 2020
85fafb6
Merge branch 'develop' into tommy/auth-shipping-info
dpatil-magento May 27, 2020
d07dd22
Cover ui components with tests
tjwiebell May 27, 2020
eda5b4c
- Re-factor useShippingInformation to use skipped getQuery calls
tjwiebell May 27, 2020
2450600
Cover the rest of the newly added talons with tests.
tjwiebell May 28, 2020
defaf9f
Address QA/PR feedback
tjwiebell May 28, 2020
4c2e982
Merge branch 'develop' into tommy/auth-shipping-info
tjwiebell May 28, 2020
ffea854
Update tests and snaps with new click handler
tjwiebell May 28, 2020
b2e9fbd
Merge remote-tracking branch 'origin/tommy/auth-shipping-info' into t…
tjwiebell May 28, 2020
8d29c26
Remove box-shadow transition from add new address card also
tjwiebell May 28, 2020
786c4fd
Update snaps
tjwiebell May 28, 2020
9c8790f
Refactor to use skip instead of lazy query to fix bug with effect ord…
tjwiebell May 28, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { useState, useCallback, useEffect } from 'react';
import { useMutation, useQuery } from '@apollo/react-hooks';

import { useAppContext } from '../../../context/app';
import { useCartContext } from '../../../context/cart';

export const useAddressBook = props => {
const {
mutations: { setCustomerAddressOnCartMutation },
queries: { getCustomerAddressesQuery },
toggleActiveContent
} = props;

const [, { toggleDrawer }] = useAppContext();
const [{ cartId }] = useCartContext();

const [activeAddress, setActiveAddress] = useState();
const [selectedAddress, setSelectedAddress] = useState();

const [
setCustomerAddressOnCart,
{ loading: setCustomerAddressOnCartLoading }
] = useMutation(setCustomerAddressOnCartMutation);

const {
data: customerAddressesData,
error: customerAddressesError,
loading: customerAddressesLoading
} = useQuery(getCustomerAddressesQuery);

useEffect(() => {
console.error(customerAddressesError);
sirugh marked this conversation as resolved.
Show resolved Hide resolved
}, [customerAddressesError]);

const isLoading =
customerAddressesLoading || setCustomerAddressOnCartLoading;
const customerAddresses =
(customerAddressesData && customerAddressesData.customer.addresses) ||
[];

const handleEditAddress = useCallback(
address => {
setActiveAddress(address);
toggleDrawer('shippingInformation.edit');
},
[toggleDrawer]
);

const handleAddAddress = useCallback(() => {
handleEditAddress();
}, [handleEditAddress]);

const handleSelectAddress = useCallback(addressId => {
setSelectedAddress(addressId);
}, []);

const handleApplyAddress = useCallback(async () => {
try {
await setCustomerAddressOnCart({
variables: {
cartId,
addressId: selectedAddress
}
});
} catch (error) {
console.error(error);
}

toggleActiveContent();
}, [
cartId,
selectedAddress,
setCustomerAddressOnCart,
toggleActiveContent
]);

return {
activeAddress,
customerAddresses,
isLoading,
handleAddAddress,
handleApplyAddress,
handleSelectAddress,
handleEditAddress,
selectedAddress,
setSelectedAddress
sirugh marked this conversation as resolved.
Show resolved Hide resolved
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { useCallback, useMemo } from 'react';

export const useAddressCard = props => {
const { address, onEdit, onSelection } = props;
const { id: addressId } = address;

const addressForEdit = useMemo(() => {
const { country_code: countryCode, region, ...addressRest } = address;
const { region_code: regionCode } = region;

return {
...addressRest,
country: {
code: countryCode
},
region: {
code: regionCode
}
};
}, [address]);

const handleClick = useCallback(() => {
onSelection(addressId);
}, [addressId, onSelection]);

const handleKeyPress = useCallback(
e => {
sirugh marked this conversation as resolved.
Show resolved Hide resolved
if (e.key === 'Enter') {
onSelection(addressId);
}
},
[addressId, onSelection]
);

const handleEditAddress = useCallback(() => {
onEdit(addressForEdit);
}, [addressForEdit, onEdit]);

return {
handleClick,
handleEditAddress,
handleKeyPress
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,63 @@ import { useCallback } from 'react';
import { useMutation } from '@apollo/react-hooks';

import { useCartContext } from '../../../../context/cart';
import { useUserContext } from '../../../../context/user';

export const useEditForm = props => {
const {
addressType,
afterSubmit,
mutations: { setShippingInformationMutation },
mutations: {
createCustomerAddressMutation,
setDefaultAddressMutation,
setGuestShippingMutation,
updateCustomerAddressMutaton
},
onCancel,
queries: { getCustomerAddresses },
shippingData
} = props;

const [{ cartId }] = useCartContext();
const [setShippingInformation, { called, loading }] = useMutation(
setShippingInformationMutation
);
const [{ currentUser, isSignedIn }] = useUserContext();

const [
createCustomerAddress,
{
called: createCustomerAddressCalled,
loading: createCustomerAddressLoading
}
] = useMutation(createCustomerAddressMutation);

const [
updateCustomerAddress,
{
called: updateCustomerAddressCalled,
loading: updateCustomerAddressLoading
}
] = useMutation(updateCustomerAddressMutaton);

const [
setDefaultAddress,
{ called: setDefaultAddressCalled, loading: setDefaultAddressLoading }
] = useMutation(setDefaultAddressMutation);

const [
setGuestShipping,
{ called: setGuestShippingCalled, loading: setGuestShippingLoading }
] = useMutation(setGuestShippingMutation);

const isSaving =
(setGuestShippingCalled && setGuestShippingLoading) ||
(createCustomerAddressCalled && createCustomerAddressLoading) ||
(updateCustomerAddressCalled && updateCustomerAddressLoading) ||
(setDefaultAddressCalled && setDefaultAddressLoading);

const { country, region } = shippingData;
const { code: countryCode } = country;
const { code: regionCode } = region;

const initialValues = {
let initialValues = {
...shippingData,
country: countryCode,
region: regionCode
Expand All @@ -29,20 +67,91 @@ export const useEditForm = props => {
// Simple heuristic to indicate form was submitted prior to this render
const isUpdate = !!shippingData.city;

if (isSignedIn && !isUpdate) {
const { email, firstname, lastname } = currentUser;
const defaultUserData = { email, firstname, lastname };
initialValues = {
...initialValues,
...defaultUserData
};
}

const handleSubmit = useCallback(
async formValues => {
const { country, email, ...address } = formValues;
const {
country,
default_shipping: defaultShipping,
email,
region,
...address
} = formValues;
try {
await setShippingInformation({
variables: {
cartId,
email,
address: {
...address,
country_code: country
if (isSignedIn) {
const customerAddress = {
...address,
country_code: country,
default_shipping: defaultShipping,
// Hard-coding region data until MC-33854/MC-33948 is resolved
region: {
region: 'Alabama',
region_id: 1,
// region_code: region
region_code: 'AL'
}
};

if (isUpdate) {
const { id: addressId } = shippingData;
await updateCustomerAddress({
variables: {
addressId,
address: {
...customerAddress,
default_shipping: defaultShipping
}
},
refetchQueries: [{ query: getCustomerAddresses }]
});
} else {
const { data } = await createCustomerAddress({
variables: {
address: {
...customerAddress,
default_shipping:
addressType === 'checkout'
? true
: defaultShipping
}
},
refetchQueries: [{ query: getCustomerAddresses }]
});

const { createCustomerAddress: addressResult } = data;
const { id: addressId } = addressResult;

// If creation originated from checkout, set address on cart
if (addressType === 'checkout') {
await setDefaultAddress({
variables: {
cartId,
addressId
}
});
}
}
});
} else {
await setGuestShipping({
variables: {
cartId,
email,
address: {
...address,
country_code: country,
region
}
}
});
}
} catch (error) {
console.error(error);
}
Expand All @@ -51,7 +160,19 @@ export const useEditForm = props => {
afterSubmit();
}
},
[afterSubmit, cartId, setShippingInformation]
[
addressType,
afterSubmit,
cartId,
createCustomerAddress,
getCustomerAddresses,
isSignedIn,
isUpdate,
setDefaultAddress,
setGuestShipping,
shippingData,
updateCustomerAddress
]
);

const handleCancel = useCallback(() => {
Expand All @@ -62,7 +183,8 @@ export const useEditForm = props => {
handleCancel,
handleSubmit,
initialValues,
isSaving: called && loading,
isSaving,
isSignedIn,
isUpdate
};
};
Loading