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

fix: don't execute unnecessary requests for anonymous cart on initial load #705

Merged
merged 5 commits into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
45 changes: 32 additions & 13 deletions hooks/cart/useCart.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useMemo, useCallback } from "react";
import { useQuery, useMutation, useApolloClient } from "@apollo/client";
import { useLazyQuery, useMutation, useApolloClient } from "@apollo/client";
import useStores from "hooks/useStores";
import useShop from "hooks/shop/useShop";
import useViewer from "hooks/viewer/useViewer";
Expand Down Expand Up @@ -37,32 +37,43 @@ export default function useCart() {
const shouldSkipAccountCartByAccountIdQuery = Boolean(!accountId || cartStore.hasAnonymousCartCredentials || isLoadingViewer || !shop || !shop._id);
const shouldSkipAnonymousCartByCartIdQuery = Boolean(accountId || isLoadingViewer || !cartStore.anonymousCartId || !cartStore.anonymousCartToken);

const { loading: isLoading, data: cartData, fetchMore, refetch: refetchCart } = useQuery(accountCartByAccountIdQuery, {
skip: shouldSkipAccountCartByAccountIdQuery,
const [
fetchAccountCart,
{ loading: isLoading, called: accountCartQueryCalled, data: cartData, fetchMore, refetch: refetchAccountCart }
] = useLazyQuery(accountCartByAccountIdQuery, {
variables: {
accountId,
shopId: shop && shop._id
},
pollInterval: shouldSkipAccountCartByAccountIdQuery ? 0 : 10000
});

const { data: cartDataAnonymous, refetch: refetchCartAnonymous } = useQuery(anonymousCartByCartIdQuery, {
skip: shouldSkipAnonymousCartByCartIdQuery,

const [
fetchAnonymousCart,
{ data: cartDataAnonymous, called: anonymousCartQueryCalled, refetch: refetchAnonymousCart }
] = useLazyQuery(anonymousCartByCartIdQuery, {
variables: {
cartId: cartStore.anonymousCartId,
cartToken: cartStore.anonymousCartToken
},
pollInterval: shouldSkipAnonymousCartByCartIdQuery ? 0 : 10000
});

if (!accountCartQueryCalled && !shouldSkipAccountCartByAccountIdQuery) {
fetchAccountCart();
} else if (!anonymousCartQueryCalled && !shouldSkipAnonymousCartByCartIdQuery) {
fetchAnonymousCart();
}

useEffect(() => {
if (!shouldSkipAccountCartByAccountIdQuery) {
refetchCart();
if (!shouldSkipAccountCartByAccountIdQuery && accountCartQueryCalled) {
refetchAccountCart();
}
if (!shouldSkipAnonymousCartByCartIdQuery) {
refetchCartAnonymous();
if (!shouldSkipAnonymousCartByCartIdQuery && anonymousCartQueryCalled) {
refetchAnonymousCart();
}
}, [viewer, refetchCart]);
}, [viewer, refetchAccountCart]);

const cart = useMemo(() => {
if (!shouldSkipAccountCartByAccountIdQuery && cartData) {
Expand Down Expand Up @@ -110,7 +121,13 @@ export default function useCart() {
const { cart: cartPayload, token } = addOrCreateCartMutationData.createCart;
cartStore.setAnonymousCartCredentials(cartPayload._id, token);
}
refetchCart();

const { accountCartId, anonymousCartToken } = cartStore;
if (accountCartId) {
refetchAccountCart();
} else if (anonymousCartToken) {
refetchAnonymousCart();
}
}
});

Expand Down Expand Up @@ -209,7 +226,9 @@ export default function useCart() {
});

// Refetch cart
refetchCart && refetchCart();
if (accountCartQueryCalled) {
refetchAccountCart();
}
}
}

Expand Down Expand Up @@ -348,7 +367,7 @@ export default function useCart() {
});
}
},
refetchCart,
refetchAccountCart,
setEmailOnAnonymousCart: async ({ email }) => {
await apolloClient.mutate({
mutation: setEmailOnAnonymousCartMutation,
Expand Down
2 changes: 1 addition & 1 deletion pages/[lang]/cart/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const Login = ({ router }) => {
isLoadingCart,
setEmailOnAnonymousCart
} = useCart();
const hasIdentity = !!((cart && cart.account !== null) || (cart && cart.email));
const hasIdentity = !!((cart && cart.account) || (cart && cart.email));
const pageTitle = `Login | ${shop && shop.name}`;

useEffect(() => {
Expand Down
4 changes: 2 additions & 2 deletions pages/api/account/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import passportMiddleware from "apiUtils/passportMiddleware";

const token = async (req, res) => {
req.session.redirectTo = req.headers.Referer;
if (req.session && req.session.passport) {
if (req.session && req.session.passport && req.session.passport.user) {
try {
const user = JSON.parse(req.session.passport.user);
const { accessToken } = user;
Expand All @@ -17,7 +17,7 @@ const token = async (req, res) => {
}
}

return res.status(401).send(JSON.stringify({ error: "No authorization data present" }));
return res.status(200).send(JSON.stringify({ anonymous: true }));
};

export default passportMiddleware(token);