Skip to content

Commit bb3be72

Browse files
Fix the redundant network calls made by filter and product API
1 parent 84a7944 commit bb3be72

File tree

5 files changed

+84
-97
lines changed

5 files changed

+84
-97
lines changed

client/src/actions/index.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,14 @@ export const sendPaymentToken = (token) => async dispatch => {
252252
}
253253

254254

255-
export const getDataViaAPI = (type, uri, subsequent_request) => async dispatch => {
256-
log.info(`[ACTION]: invokeAndDispatchAPIData Calling API = ${uri}.`)
257-
255+
export const getDataViaAPI = (type, uri, query) => async dispatch => {
258256
if (uri) {
257+
if(query) {
258+
uri += query
259+
}
260+
261+
log.info(`[ACTION]: invokeAndDispatchAPIData Calling API = ${uri}.`)
262+
259263
// uri = uri.replace(/\s/g, '')
260264
let responseError = false
261265
const response = await commonServiceAPI.get(uri)
@@ -271,10 +275,18 @@ export const getDataViaAPI = (type, uri, subsequent_request) => async dispatch =
271275

272276
if (response != null) {
273277
log.debug(`[ACTION]: Data = ${JSON.parse(JSON.stringify(response.data))}.`)
274-
dispatch({
275-
type: type, payload:
276-
{isLoading: false, data: JSON.parse(JSON.stringify(response.data))}
277-
});
278+
let payload = {isLoading: false, data: JSON.parse(JSON.stringify(response.data))}
279+
if(query) {
280+
dispatch({
281+
type: type, payload:
282+
{...payload, query: query}
283+
});
284+
} else {
285+
dispatch({
286+
type: type, payload: payload
287+
});
288+
}
289+
278290

279291
if (LOAD_FILTER_PRODUCTS.localeCompare(type) === 0 &&
280292
window.location.search.localeCompare(uri.split("/products")[1]) !== 0) {

client/src/components/routes/navbar/searchBar.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ import CloseIcon from '@material-ui/icons/Close';
55
import ArrowBackIcon from '@material-ui/icons/ArrowBack';
66
import {Grid} from "@material-ui/core";
77
import log from 'loglevel';
8-
import {connect, useSelector} from "react-redux";
8+
import {connect, useDispatch, useSelector} from "react-redux";
99
import {getSearchSuggestions, getDataViaAPI} from "../../../actions";
1010
import {makeStyles} from "@material-ui/core/styles";
11-
import {LOAD_FILTER_PRODUCTS} from "../../../actions/types";
12-
import {PRODUCT_BY_CATEGORY_DATA_API} from "../../../constants/api_routes";
11+
import {SAVE_QUERY_STATUS} from "../../../actions/types";
1312
import {Loader} from "semantic-ui-react";
1413
import {StyledSearchBarDimmer} from "../../../styles/semanticUI/customStyles";
1514

@@ -33,6 +32,7 @@ function SearchBar(props) {
3332
const filterProductsReducer = useSelector(state => state.filterProductsReducer)
3433
const classes = useSearchBarStyles()
3534
const [isLoading, setIsLoading] = useState(false)
35+
const dispatch = useDispatch()
3636
let selectedValue = null
3737

3838
useEffect(() => {
@@ -62,8 +62,10 @@ function SearchBar(props) {
6262
log.info(`queryLink = ${queryLink}, value = ${value}`)
6363
if (queryLink) {
6464
setIsLoading(true)
65-
props.getDataViaAPI(LOAD_FILTER_PRODUCTS,
66-
`${PRODUCT_BY_CATEGORY_DATA_API}?q=${queryLink}`)
65+
dispatch({
66+
type: SAVE_QUERY_STATUS,
67+
payload: `?q=${queryLink}`
68+
})
6769
}
6870
}
6971
}

client/src/components/routes/product/filterProductDisplay.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ const FilterProductDisplay = props => {
2424
log.info(`[FilterProductDisplay] Component did mount`)
2525

2626
try {
27-
// if query is present then call the products API
28-
if (queryStatus) {
29-
log.info(`[FilterProductDisplay] Getting products from API.....`)
30-
props.getDataViaAPI(LOAD_FILTER_PRODUCTS, PRODUCT_BY_CATEGORY_DATA_API + queryStatus)
27+
// if we already have the data then dont make a API request.
28+
if (queryStatus && (queryStatus.localeCompare(filterProductsReducer.query) !== 0)) {
29+
log.info(`[FilterProductDisplay] Getting products from API.....query = ${filterProductsReducer.query}, queryStatus = ${queryStatus}, URL = ${history.location.search}`)
30+
props.getDataViaAPI(LOAD_FILTER_PRODUCTS, PRODUCT_BY_CATEGORY_DATA_API, queryStatus)
3131
}
3232
} catch (e) {
3333
log.error(`[FilterProductDisplay] Bad URL found in history.location.search`)

client/src/components/routes/product/filterSideNavbar/filterNavBar.js

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useEffect} from 'react';
1+
import React, {useEffect, useState} from 'react';
22
import Divider from '@material-ui/core/Divider';
33
import Drawer from '@material-ui/core/Drawer';
44
import Hidden from '@material-ui/core/Hidden';
@@ -33,6 +33,7 @@ function FilterNavBar(props) {
3333
const selectedSort = useSelector(state => state.selectSortReducer)
3434
const selectedPage = useSelector(state => state.selectPageReducer)
3535
const dispatch = useDispatch()
36+
const [loadOnlyProducts, setLoadOnlyProducts] = useState(false)
3637

3738
/**
3839
* multiple selected options IDs are appended
@@ -301,49 +302,52 @@ function FilterNavBar(props) {
301302

302303
const {oldQuery, newQuery} = selectedFilterAttributes;
303304

304-
let dispatchQueryForProducts = null
305+
// this means we are seeing new URL
306+
if (!oldQuery || (oldQuery && oldQuery.localeCompare(newQuery) === 0)) {
307+
return
308+
}
309+
305310
let queryFromURL = history.location.search
306311

307312
log.info(`[FilterNavBar] filter selection hook oldQuery = ${oldQuery}, newQuery = ${newQuery}, queryFromURL = ${queryFromURL}`)
308313
if (!newQuery) {
309-
log.info(`[FilterNavBar] filter selection hook oldQuery = ${oldQuery}, newQuery = ${newQuery}`)
314+
log.info(`[FilterNavBar] updating states for filter selection hook`)
310315

311316
// on filter selection scenario
312317
let queryPreparedFromFilters = prepareQuery()
313318

314319
props.loadFilterAttributes(queryPreparedFromFilters).then(data => {
315-
dispatchSortList(data)
316-
})
317-
318-
// set new query
319-
dispatch({
320-
type: ADD_SELECTED_CATEGORY,
321-
payload: {newQuery: queryPreparedFromFilters}
322-
})
323-
324-
// by default first page should be selected.
325-
if (selectedPage.pageNumber > 1) {
326-
log.info(`[FilterNavBar] filter selection hook dispatching selectedPage = ${JSON.stringify(selectedPage)}`)
320+
if(!data) {
321+
log.error(`[FilterNavBar] loadFilterAttributes failed. No data found.`)
322+
return
323+
}
327324

325+
dispatchSortList(data)
326+
// set new query
328327
dispatch({
329-
type: SELECT_PRODUCT_PAGE,
330-
payload: {
331-
pageNumber: 1,
332-
maxProducts: MAX_PRODUCTS_PER_PAGE,
333-
isLoadedFromURL: false
334-
}
328+
type: ADD_SELECTED_CATEGORY,
329+
payload: {newQuery: queryPreparedFromFilters}
335330
})
336-
} else {
337-
dispatchQueryForProducts = queryPreparedFromFilters
338-
}
339-
}
340331

341-
if (dispatchQueryForProducts) {
342-
log.info(`[FilterNavBar] filter selection hook dispatchQueryForProducts = ${dispatchQueryForProducts}`)
332+
// by default first page should be selected.
333+
if (selectedPage.pageNumber > 1) {
334+
log.info(`[FilterNavBar] filter selection hook dispatching selectedPage = ${JSON.stringify(selectedPage)}`)
343335

344-
dispatch({
345-
type: SAVE_QUERY_STATUS,
346-
payload: dispatchQueryForProducts
336+
dispatch({
337+
type: SELECT_PRODUCT_PAGE,
338+
payload: {
339+
pageNumber: 1,
340+
maxProducts: MAX_PRODUCTS_PER_PAGE,
341+
isLoadedFromURL: false
342+
}
343+
})
344+
} else {
345+
log.info(`[FilterNavBar] filter selection hook dispatching SAVE_QUERY_STATUS = ${queryPreparedFromFilters}`)
346+
dispatch({
347+
type: SAVE_QUERY_STATUS,
348+
payload: queryPreparedFromFilters
349+
})
350+
}
347351
})
348352
}
349353

@@ -355,29 +359,33 @@ function FilterNavBar(props) {
355359

356360
const {oldQuery, newQuery} = selectedFilterAttributes;
357361

358-
let dispatchQueryForProducts = null
359362
let queryFromURL = history.location.search
360363

361364
log.info(`[FilterNavBar] new URL hook oldQuery = ${oldQuery}, newQuery = ${newQuery}, queryFromURL = ${queryFromURL}`)
365+
366+
if(loadOnlyProducts) {
367+
setLoadOnlyProducts(false)
368+
return
369+
}
370+
362371
if (!oldQuery || newQuery.localeCompare(queryFromURL) !== 0) {
363-
log.info(`[FilterNavBar] new URL hook oldQuery = ${oldQuery}, newQuery = ${newQuery}`)
372+
log.info(`[FilterNavBar] updating states for new URL hook`)
364373

365374
// on links click from tabs
366375
props.loadFilterAttributes(queryFromURL).then(data => {
376+
if(!data) {
377+
log.error(`[FilterNavBar] loadFilterAttributes failed. No data found.`)
378+
return
379+
}
380+
367381
dispatchFilterAttributesFromURL(data, queryFromURL)
368382
dispatchSortAttributeFromURL(data, queryFromURL)
369383
dispatchPageAttributeFromURL(data, queryFromURL)
370384
dispatchSortList(data)
371-
})
372-
dispatchQueryForProducts = queryFromURL
373-
}
374-
375-
if (dispatchQueryForProducts) {
376-
log.info(`[FilterNavBar] new URL hook dispatchQueryForProducts = ${dispatchQueryForProducts}`)
377-
378-
dispatch({
379-
type: SAVE_QUERY_STATUS,
380-
payload: dispatchQueryForProducts
385+
dispatch({
386+
type: SAVE_QUERY_STATUS,
387+
payload: queryFromURL
388+
})
381389
})
382390
}
383391

@@ -391,12 +399,14 @@ function FilterNavBar(props) {
391399
log.info("[FilterNavBar] Component did mount selectedPage, selectedSort hook.")
392400

393401
if (!selectedPage.isLoadedFromURL || !selectedSort.isLoadedFromURL) {
394-
log.info("[FilterNavBar] selectedPage, selectedSort hook Preparing query for selectedPage and selectedSort.")
402+
log.info(`[FilterNavBar] updating states for selectedPage, selectedSort hook`)
395403

396404
let query = prepareQuery()
397405
log.info(`[FilterNavBar] selectedPage, selectedSort hook dispatchQueryForProducts = ${query}`)
398406

399407
if (query) {
408+
setLoadOnlyProducts(true)
409+
400410
dispatch({
401411
type: SAVE_QUERY_STATUS,
402412
payload: query

client/src/components/routes/product/product.js

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useEffect} from 'react';
1+
import React from 'react';
22

33
import Grid from '@material-ui/core/Grid';
44
import FilterNavBar from "./filterSideNavbar/filterNavBar";
@@ -12,13 +12,6 @@ import Hidden from "@material-ui/core/Hidden";
1212
// import BottomNavBar from "./bottomNavBar";
1313
import history from "../../../history";
1414
import BreadcrumbsSection from "../../ui/breadcrumbs";
15-
import {useDispatch} from "react-redux";
16-
import {
17-
RESET_QUERY_STATUS,
18-
RESET_SELECT_PRODUCT_PAGE,
19-
RESET_SELECT_SORT_CATEGORY,
20-
RESET_SELECTED_CATEGORY,
21-
} from "../../../actions/types";
2215
import {BadRequest} from "../../ui/error/badRequest";
2316
import {HOME_ROUTE} from "../../../constants/react_routes";
2417
import {DocumentTitle} from "../../ui/documentTitle";
@@ -31,15 +24,7 @@ export const stickyBoxStyle = {
3124
paddingLeft: "1rem"
3225
}
3326

34-
const RESET_ALL_PRODUCT_STATES = [
35-
RESET_SELECTED_CATEGORY,
36-
RESET_SELECT_PRODUCT_PAGE,
37-
RESET_SELECT_SORT_CATEGORY,
38-
RESET_QUERY_STATUS]
39-
40-
4127
function Product() {
42-
const dispatch = useDispatch();
4328

4429
// define breadcrumbs
4530
const breadcrumbLinks = [
@@ -53,28 +38,6 @@ function Product() {
5338
},
5439
]
5540

56-
useEffect(() => {
57-
log.info(`[Product] Component did mount...`)
58-
59-
return () => {
60-
log.info(`[Product] Component will unmount...`)
61-
62-
// reset the saved query as we will load
63-
// next time from the URL.
64-
// This required to support the case where user
65-
// executes URL directly and we need to construct
66-
// fresh states for eg selecting options based on URL
67-
RESET_ALL_PRODUCT_STATES.forEach(type => {
68-
dispatch({
69-
type: type
70-
})
71-
})
72-
73-
}
74-
// dont need any dependency as we want to update any states
75-
// eslint-disable-next-line
76-
}, [])
77-
7841
// if we got unexpected uri then just send bad request component.
7942
if (history.location.pathname.localeCompare('/products') !== 0
8043
|| !history.location.search.startsWith('?q=')) {

0 commit comments

Comments
 (0)