refactor jamtracks loading

reduce the nertwork calls it had when loading jamtracks on
jamtacks listing which increases the loading time.
This commit is contained in:
Nuwan 2025-02-12 06:14:12 +05:30
parent 36a184638f
commit 342960e57b
8 changed files with 40 additions and 60 deletions

View File

@ -1,58 +1,15 @@
import React from 'react'; import React from 'react';
import { Button } from 'reactstrap'; import { Button } from 'reactstrap';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
//import { useHistory } from 'react-router-dom';
import { useAuth } from '../../context/UserAuth'; import { useAuth } from '../../context/UserAuth';
//import { toast } from 'react-toastify';
//import { useShoppingCart } from '../../hooks/useShoppingCart';
import useJamTrackShopping from '../../hooks/useJamTrackShopping'; import useJamTrackShopping from '../../hooks/useJamTrackShopping';
import { Link } from 'react-router-dom'; import { Link } from 'react-router-dom';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import JKTooltip from '../common/JKTooltip'; import JKTooltip from '../common/JKTooltip';
//import { placeOrder, updateUser } from '../../helpers/rest';
const JKJamTrackPurchaseButton = ({ jamTrack }) => { const JKJamTrackPurchaseButton = ({ jamTrack }) => {
//const history = useHistory();
const { currentUser } = useAuth(); const { currentUser } = useAuth();
//const { addCartItem, getCartItems, hasOnlyFreeItemsInShoppingCart } = useShoppingCart();
const { t } = useTranslation('jamtracks'); const { t } = useTranslation('jamtracks');
// const addToCart = async () => {
// if (!currentUser) {
// return;
// }
// const options = {
// id: jamTrack.id,
// variant: 'full'
// };
// try {
// const resp = await addCartItem(options);
// if(resp.fast_reedem){ //if this is a free jamtrack
// //get shopping cart items and see if all are free
// if(!hasOnlyFreeItemsInShoppingCart()){
// history.push('/jamtracks');
// }else{
// const purchadeResp = await placeOrder();
// if(purchadeResp.ok){
// const userResp = await updateUser(currentUser.id);
// if(userResp.ok){
// history.push('/checkout/success?free=yes&jamtrackId=' + jamTrack.id);
// }
// }
// }
// }else{ //if this is a paid jamtrack
// toast.success(t('search.list.add_success_alert'));
// history.push('/shopping-cart');
// }
// } catch (error) {
// console.log(error);
// toast.error(t('search.list.add_error_alert'));
// }
// };
const { addToCart } = useJamTrackShopping(); const { addToCart } = useJamTrackShopping();
const handleAddToCart = () => { const handleAddToCart = () => {

View File

@ -11,6 +11,7 @@ import JKJamTracksSlider from './JKJamTracksSlider';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { useShoppingCart } from '../../hooks/useShoppingCart'; import { useShoppingCart } from '../../hooks/useShoppingCart';
import { useResponsive } from '@farfetch/react-context-responsive'; import { useResponsive } from '@farfetch/react-context-responsive';
//import { useAppData } from '../../context/AppDataContext';
const JKJamTracksFilter = () => { const JKJamTracksFilter = () => {
const { t } = useTranslation('jamtracks'); const { t } = useTranslation('jamtracks');
@ -23,12 +24,17 @@ const JKJamTracksFilter = () => {
const [autoCompleteInputValue, setAutoCompleteInputValue] = useState(''); const [autoCompleteInputValue, setAutoCompleteInputValue] = useState('');
const page = useRef(1); const page = useRef(1);
const PER_PAGE = 10; const PER_PAGE = 10;
const { shoppingCart } = useShoppingCart(); const { getCartItems, shoppingCart } = useShoppingCart();
// const { shoppingCart } = useAppData();
const { greaterThan } = useResponsive(); const { greaterThan } = useResponsive();
const queryString = useBrowserQuery(); const queryString = useBrowserQuery();
const query = queryString.get('query'); const query = queryString.get('query');
const artist = queryString.get('artist'); const artist = queryString.get('artist');
useEffect(() => {
getCartItems()
}, [])
useEffect(() => { useEffect(() => {
if (selected) { if (selected) {
setSearchTerm(selected.type === 'artist' ? selected.original_artist : selected.name); setSearchTerm(selected.type === 'artist' ? selected.original_artist : selected.name);
@ -198,7 +204,7 @@ const JKJamTrackFilterLinks = ({ shoppingCart, wrapperClassNames, shoppingCartCl
<strong>PDF file</strong> <strong>PDF file</strong>
</a> </a>
</div> </div>
{shoppingCart.length > 0 && ( {shoppingCart && shoppingCart.length > 0 && (
<div className={shoppingCartClassNames}> <div className={shoppingCartClassNames}>
<Link to="shopping-cart" className="btn btn-primary btn-sm"> <Link to="shopping-cart" className="btn btn-primary btn-sm">
<FontAwesomeIcon icon="shopping-cart" className="mr-1" /> <FontAwesomeIcon icon="shopping-cart" className="mr-1" />

View File

@ -1,8 +1,5 @@
import React from 'react'; import React from 'react';
import { Row, Col, Table, Button } from 'reactstrap'; import { Row, Col, Table, Button } from 'reactstrap';
import JKJamTrackPreview from './JKJamTrackPreview';
import JKJamTrackPurchaseButton from './JKJamTrackPurchaseButton';
import { JamTrackPreviewProvider } from '../../context/JamTrackPreviewContext';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import JKJamTrackItem from './JKJamTrackItem'; import JKJamTrackItem from './JKJamTrackItem';

View File

@ -32,7 +32,7 @@ import { useCheckout } from '../../hooks/useCheckout';
const JKCheckout = () => { const JKCheckout = () => {
const { currency } = useContext(AppContext); const { currency } = useContext(AppContext);
const { cartTotal: payableTotal, loading: cartLoading } = useShoppingCart(); const { cartTotal: payableTotal, loading: cartLoading, getCartItems, shoppingCart } = useShoppingCart();
const { greaterThan } = useResponsive(); const { greaterThan } = useResponsive();
const { currentUser } = useAuth(); const { currentUser } = useAuth();
const history = useHistory(); const history = useHistory();
@ -78,6 +78,10 @@ const JKCheckout = () => {
} }
}); });
useEffect(() => {
getCartItems()
}, [])
useEffect(() => { useEffect(() => {
if (shouldPreserveBillingInfo) { if (shouldPreserveBillingInfo) {
refreshPreserveBillingInfo(); refreshPreserveBillingInfo();
@ -313,7 +317,7 @@ const JKCheckout = () => {
</div> </div>
)} )}
<ContentWithAsideLayout <ContentWithAsideLayout
aside={cartLoading ? <div>Cart Loading...</div> : <CheckoutAside />} aside={cartLoading ? <div>Cart Loading...</div> : <CheckoutAside shoppingCart={shoppingCart} />}
isStickyAside={false} isStickyAside={false}
> >

View File

@ -1,4 +1,4 @@
import React from 'react'; import React, { useEffect } from 'react';
import FalconCardHeader from '../common/FalconCardHeader'; import FalconCardHeader from '../common/FalconCardHeader';
import { Button, Card, CardBody } from 'reactstrap'; import { Button, Card, CardBody } from 'reactstrap';
import { Link } from 'react-router-dom'; import { Link } from 'react-router-dom';
@ -13,7 +13,11 @@ import { useResponsive } from '@farfetch/react-context-responsive';
const JKShoppingCart = () => { const JKShoppingCart = () => {
const { greaterThan } = useResponsive(); const { greaterThan } = useResponsive();
const { shoppingCart, loading, removeCartItem } = useShoppingCart(); const { shoppingCart, loading, removeCartItem, getCartItems } = useShoppingCart();
useEffect(() => {
getCartItems();
}, []);
const handleRemoveItem = async id => { const handleRemoveItem = async id => {
if (await removeCartItem(id)) { if (await removeCartItem(id)) {

View File

@ -1,4 +1,4 @@
import React, { Fragment, useContext } from 'react'; import React, { Fragment, useContext, useEffect } from 'react';
//import PropTypes from 'prop-types'; //import PropTypes from 'prop-types';
import AppContext from '../../../context/Context'; import AppContext from '../../../context/Context';
import { Alert, Card, CardBody, CardFooter, Media, Table } from 'reactstrap'; import { Alert, Card, CardBody, CardFooter, Media, Table } from 'reactstrap';
@ -8,9 +8,12 @@ import Flex from '../../common/Flex';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { useShoppingCart } from '../../../hooks/useShoppingCart'; import { useShoppingCart } from '../../../hooks/useShoppingCart';
const CheckoutAside = () => { const CheckoutAside = ({ shoppingCart}) => {
const { currency } = useContext(AppContext); const { currency } = useContext(AppContext);
const { shoppingCart, cartTotal } = useShoppingCart(); const { cartTotal } = useShoppingCart();
// useEffect(() => {
// getCartItems()
// }, [])
return ( return (
<Card> <Card>
<FalconCardHeader title="Order Summary" titleTag="h5" light={false}> <FalconCardHeader title="Order Summary" titleTag="h5" light={false}>

View File

@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import useUserProfile from '../hooks/useUserProfile'; import useUserProfile from '../hooks/useUserProfile';
import { useAuth } from './UserAuth'; import { useAuth } from './UserAuth';
//import { useShoppingCart } from '../hooks/useShoppingCart';
// AppDataContext.js // AppDataContext.js
// this context is used to store app data that is shared across the app // this context is used to store app data that is shared across the app
@ -10,9 +11,17 @@ export const AppDataProvider = ({ children }) => {
const [appData, setAppData] = React.useState({}); const [appData, setAppData] = React.useState({});
const { currentUser } = useAuth(); const { currentUser } = useAuth();
const { userProfile, photoUrl } = useUserProfile({ user: currentUser, useCache: false }); const { userProfile, photoUrl } = useUserProfile({ user: currentUser, useCache: false });
//const { shoppingCart, getCartItems } = useShoppingCart();
// React.useEffect(() => {
// getCartItems();
// }, [currentUser]);
React.useEffect(() => { React.useEffect(() => {
setAppData({ userProfile, currentUserPhotoUrl: photoUrl }); setAppData({
userProfile, currentUserPhotoUrl:
photoUrl,
});
}, [currentUser, userProfile, photoUrl]); }, [currentUser, userProfile, photoUrl]);
return <AppDataContext.Provider value={{ appData, setAppData }}>{children}</AppDataContext.Provider>; return <AppDataContext.Provider value={{ appData, setAppData }}>{children}</AppDataContext.Provider>;

View File

@ -7,9 +7,9 @@ export const useShoppingCart = () => {
const [error, setError] = useState(null); const [error, setError] = useState(null);
const TAX_RATE = 0.1; const TAX_RATE = 0.1;
useEffect(() => { // useEffect(() => {
getCartItems(); // getCartItems();
}, []); // }, []);
const cartTotal = useMemo(() => { const cartTotal = useMemo(() => {
//calculate total price //calculate total price
@ -63,6 +63,6 @@ export const useShoppingCart = () => {
}, [shoppingCart]); }, [shoppingCart]);
return{ return{
shoppingCart, error, loading, removeCartItem, addCartItem, cartTotal, hasOnlyFreeItemsInShoppingCart shoppingCart, error, loading, getCartItems, removeCartItem, addCartItem, cartTotal, hasOnlyFreeItemsInShoppingCart
} }
} }