Skip to content

Commit

Permalink
3220 testing
Browse files Browse the repository at this point in the history
  • Loading branch information
RaghavendraTirumalasetti committed Dec 20, 2023
1 parent 52229b2 commit 0976369
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 7 deletions.
68 changes: 65 additions & 3 deletions packages/peregrine/lib/context/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@ import React, {
} from 'react';
import { connect } from 'react-redux';
import { useMutation } from '@apollo/client';
import { useDispatch } from 'react-redux';
import gql from 'graphql-tag';
import { useApolloClient } from '@apollo/client';

import { useAwaitQuery } from '@magento/peregrine/lib/hooks/useAwaitQuery';
import actions from '../store/actions/cart/actions';
import * as asyncActions from '../store/actions/cart/asyncActions';
import { createCart, removeCart } from '../store/actions/cart';
import bindActionCreators from '../util/bindActionCreators';
import { useEventListener } from '../hooks/useEventListener';
import BrowserPersistence from '../util/simplePersistence';

const CartContext = createContext();

console.log(createCart,"From the direct file");
console.log(asyncActions.createCart,"Type 2 export");

const isCartEmpty = cart =>
!cart || !cart.details.items || cart.details.items.length === 0;

Expand All @@ -26,7 +32,12 @@ const getTotalQuantity = items =>

const CartContextProvider = props => {
const { actions, asyncActions, cartState, children } = props;
console.log(cartState,"cartstate");
console.log(actions,"actionns");
console.log(asyncActions,"Async Actions");
const dispatch = useDispatch();

const apolloClient = useApolloClient();
// Make deeply nested details easier to retrieve and provide empty defaults
const derivedDetails = useMemo(() => {
if (isCartEmpty(cartState)) {
Expand All @@ -52,15 +63,66 @@ const CartContextProvider = props => {
[actions, asyncActions]
);


// const resetCart = () => {


// actions.reset();
// // if there isn't a cart, create one then retry this operation
// try {
// dispatch(
// createCart({
// fetchCartId
// })
// );
// } catch (error) {
// console.log(error);
// // If creating a cart fails, all is not lost. Return so that the
// // user can continue to at least browse the site.
// return;
// }
// //return thunk(...arguments);

// }


const resetCart = async () => {

console.log("Resetting cart... in cart.js");
// actions.reset();

// Clear all cart/customer data from cache and redux.
await apolloClient.clearCacheData(apolloClient, 'cart');
await apolloClient.clearCacheData(apolloClient, 'customer');
await removeCart();


//here also creating cart id

// Create and get the customer's cart id.
await createCart({
fetchCartId
});
//await dispatch(cartApi.removeCart());

};







const contextValue = useMemo(() => {
const derivedCartState = {
...cartState,
isEmpty: isCartEmpty(cartState),
derivedDetails
derivedDetails,
resetCart
};

return [derivedCartState, cartApi];
}, [cartApi, cartState, derivedDetails]);
}, [cartApi, cartState, derivedDetails,resetCart]);

const [fetchCartId] = useMutation(CREATE_CART_MUTATION);
const fetchCartDetails = useAwaitQuery(CART_DETAILS_QUERY);
Expand All @@ -82,7 +144,7 @@ const CartContextProvider = props => {
cartApi.getCartDetails({
fetchCartId,
fetchCartDetails
});
});
}, [cartApi, fetchCartDetails, fetchCartId]);

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { useCallback, useState, useMemo } from 'react';
import { useIntl } from 'react-intl';
import gql from 'graphql-tag';
import { useDispatch } from 'react-redux';
import { useMutation, useQuery } from '@apollo/client';
import { useCartContext } from '@magento/peregrine/lib/context/cart';
import { useUserContext } from '@magento/peregrine/lib/context/user';
import {createCart} from '../../store/actions/cart/asyncActions'
//store/actions/cart/asyncActions';



import { appendOptionsToPayload } from '@magento/peregrine/lib/util/appendOptionsToPayload';
import { findMatchingVariant } from '@magento/peregrine/lib/util/findMatchingProductVariant';
Expand Down Expand Up @@ -245,6 +251,7 @@ export const useProductFullDetail = props => {
product
} = props;


const [, { dispatch }] = useEventingContext();

const hasDeprecatedOperationProp = !!(
Expand All @@ -257,7 +264,7 @@ export const useProductFullDetail = props => {

const isSupportedProductType = isSupported(productType);

const [{ cartId }] = useCartContext();
const [{ cartId,resetCart }] = useCartContext();
const [{ isSignedIn }] = useUserContext();
const { formatMessage } = useIntl();

Expand Down Expand Up @@ -571,11 +578,33 @@ export const useProductFullDetail = props => {
]
);

// console.log(createCart,"create cart function");
// const [fetchCartId] = useMutation(CREATE_CART_MUTATION);

// const resetCart = async () => {

// console.log("Resetting cart...");
// //actions.reset();

// await dispatch(createCart(fetchCartId));

// //await dispatch(asyncActions.createCart({ fetchCartId }));
// }

console.log(derivedErrorMessage);
if (derivedErrorMessage.includes('Could not find a cart with ID')){
dispatch(resetCart())
console.log("coming to here");
//console.log(fetchCartId);
//const [{ cartId }] = useCartContext();
//console.log(cartId);
}

const wishlistItemOptions = useMemo(() => {
const options = {
quantity: 1,
sku: product.sku
};
};

if (productType === 'ConfigurableProduct') {
options.selected_options = selectedOptionsArray;
Expand Down Expand Up @@ -626,3 +655,10 @@ export const useProductFullDetail = props => {
wishlistItemOptions
};
};


const CREATE_CART_MUTATION = gql`
mutation createCart {
cartId: createEmptyCart
}
`;
9 changes: 8 additions & 1 deletion packages/venia-concept/src/store.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { combineReducers, createStore } from 'redux';
import { combineReducers, createStore,applyMiddleware,compose } from 'redux';
import thunk from 'redux-thunk';
import { enhancer, reducers } from '@magento/peregrine';

// This is the connective layer between the Peregrine store and the
Expand All @@ -11,4 +12,10 @@ import { enhancer, reducers } from '@magento/peregrine';
// export default createStore(rootReducer, rootEnhancer);
const rootReducer = combineReducers(reducers);

// const composedEnhancers = compose(
// applyMiddleware(thunk),
// enhancer
// );

//applyMiddleware(thunk),
export default createStore(rootReducer, enhancer);
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ const ProductFullDetail = props => {

// Handle cases where a cart wasn't created properly.
if (
errorMessage.includes('Variable "$cartId" got invalid value null')
errorMessage.includes('Variable "$cartId" got invalid value null') || errorMessage.includes('Could not find a cart with ID')
) {
errors.set('form', [
new Error(
Expand Down

0 comments on commit 0976369

Please sign in to comment.