Skip to content

Commit

Permalink
refactor: keep hooks DRY!
Browse files Browse the repository at this point in the history
  • Loading branch information
pooriaset committed May 22, 2024
1 parent b89c711 commit f8e7866
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 77 deletions.
34 changes: 2 additions & 32 deletions src/hooks/useAddOrUpdateCartItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@ import {
ADD_TO_CART_MUTATION,
UPDATE_CART_ITEMS_QUANTITIES_MUTATION,
} from '@/graphql/queries/cart';
import { getFragmentData } from '@/graphql/types';
import {
AddToCartMutation,
CartContentFragmentDoc,
CartItemContentFragmentDoc,
GetCartQuery,
ProductVariationContentSliceFragmentDoc,
UpdateCartItemQuantitiesMutation,
} from '@/graphql/types/graphql';
import { ICartAtom, cartAtom } from '@/store/atoms';
import { useMutation } from '@apollo/client';
import { useAtom } from 'jotai';
import useCartUtils from './useCartUtils';

export interface MutateCartFunction {
(values: {
Expand All @@ -32,31 +26,7 @@ export interface IUseAddOrUpdateCartItem {
}

const useAddOrUpdateCartItem: IUseAddOrUpdateCartItem = () => {
const [cart, setCart] = useAtom(cartAtom);

const findInCart = ({ variationId }: { variationId: number }) => {
cart?.contents?.nodes.find((item) => {
const { variation } = getFragmentData(CartItemContentFragmentDoc, item);
const productContent = getFragmentData(
ProductVariationContentSliceFragmentDoc,
variation?.node,
)!;
return productContent?.databaseId == variationId;
});

return { quantity: 0, key: 0 };
};

const setCartAtom = (value: GetCartQuery['cart']) => {
const _value: ICartAtom = {
...value,
productsCount:
getFragmentData(CartContentFragmentDoc, value)?.contents?.nodes
?.length ?? 0,
};

setCart(_value);
};
const { findInCart, setCartAtom } = useCartUtils();

const [addToCart, { loading: addToCartLoading }] =
useMutation<AddToCartMutation>(ADD_TO_CART_MUTATION, {
Expand Down
15 changes: 5 additions & 10 deletions src/hooks/useCartQuery.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { GET_CART_QUERY } from '@/graphql/queries/cart';
import { getFragmentData } from '@/graphql/types';
import { CartContentFragmentDoc, GetCartQuery } from '@/graphql/types/graphql';
import { cartAtom } from '@/store/atoms';
import { GetCartQuery } from '@/graphql/types/graphql';
import { useQuery } from '@apollo/client';
import { useSetAtom } from 'jotai';
import useCartUtils from './useCartUtils';

const useCartQuery = () => {
const setCart = useSetAtom(cartAtom);
const { setCartAtom } = useCartUtils();

return useQuery<GetCartQuery>(GET_CART_QUERY, {
onCompleted: (data) => {
const cart = getFragmentData(CartContentFragmentDoc, data.cart);
setCart({
...cart,
productsCount: cart?.contents?.nodes.length ?? 0,
});
setCartAtom(data.cart);
},
fetchPolicy: 'network-only',
});
Expand Down
58 changes: 58 additions & 0 deletions src/hooks/useCartUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { getFragmentData } from '@/graphql/types';
import {
CartContentFragmentDoc,
CartItemContentFragmentDoc,
GetCartQuery,
ProductVariationContentSliceFragmentDoc,
} from '@/graphql/types/graphql';
import { ICartAtom, cartAtom } from '@/store/atoms';
import { useAtom } from 'jotai';

export interface ReturnTypeOfUseCartUtils {
findInCart: (args: { variationId: number }) => {
quantity: number;
key: string;
};
setCartAtom: (value: GetCartQuery['cart']) => void;
}

export interface IUseCartUtils {
(): ReturnTypeOfUseCartUtils;
}

const useCartUtils: IUseCartUtils = () => {
const [cart, setCart] = useAtom(cartAtom);

const findInCart: ReturnTypeOfUseCartUtils['findInCart'] = ({
variationId,
}) => {
cart?.contents?.nodes.find((item) => {
const { variation } = getFragmentData(CartItemContentFragmentDoc, item);
const productContent = getFragmentData(
ProductVariationContentSliceFragmentDoc,
variation?.node,
)!;
return productContent?.databaseId == variationId;
});

return { quantity: 0, key: '' };
};

const setCartAtom: ReturnTypeOfUseCartUtils['setCartAtom'] = (value) => {
const _value: ICartAtom = {
...value,
productsCount:
getFragmentData(CartContentFragmentDoc, value)?.contents?.nodes
?.length ?? 0,
};

setCart(_value);
};

return {
findInCart,
setCartAtom,
};
};

export default useCartUtils;
38 changes: 3 additions & 35 deletions src/hooks/useRmoveCartItem.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import { REMOVE_ITEMS_FROM_CART_MUTATION } from '@/graphql/queries/cart';
import { getFragmentData } from '@/graphql/types';
import {
CartContentFragmentDoc,
CartItemContentFragmentDoc,
GetCartQuery,
ProductVariationContentSliceFragmentDoc,
RemoveItemsFromCartMutation,
} from '@/graphql/types/graphql';
import { ICartAtom, cartAtom } from '@/store/atoms';
import { RemoveItemsFromCartMutation } from '@/graphql/types/graphql';
import { useMutation } from '@apollo/client';
import { useAtom } from 'jotai';
import useCartUtils from './useCartUtils';

export interface RemoveCartItemMutate {
(values: { variationId: number }): Promise<any>;
Expand All @@ -23,31 +15,7 @@ export interface IUseRemoveCartItem {
}

const useRemoveCartItem: IUseRemoveCartItem = () => {
const [cart, setCart] = useAtom(cartAtom);

const findInCart = ({ variationId }: { variationId: number }) => {
cart?.contents?.nodes.find((item) => {
const { variation } = getFragmentData(CartItemContentFragmentDoc, item);
const productContent = getFragmentData(
ProductVariationContentSliceFragmentDoc,
variation?.node,
)!;
return productContent?.databaseId == variationId;
});

return { quantity: 0, key: 0 };
};

const setCartAtom = (value: GetCartQuery['cart']) => {
const _value: ICartAtom = {
...value,
productsCount:
getFragmentData(CartContentFragmentDoc, value)?.contents?.nodes
?.length ?? 0,
};

setCart(_value);
};
const { findInCart, setCartAtom } = useCartUtils();

const [removeCartItem, { loading }] =
useMutation<RemoveItemsFromCartMutation>(REMOVE_ITEMS_FROM_CART_MUTATION, {
Expand Down

0 comments on commit f8e7866

Please sign in to comment.