Skip to content

Commit 84a7944

Browse files
Fix the filterNavBar component for not rendering products and attributes via selection of tab links.
1 parent b447434 commit 84a7944

File tree

8 files changed

+79
-82
lines changed

8 files changed

+79
-82
lines changed

client/src/actions/types.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const RESET_DELIVERY_CHARGES = "RESET_DELIVERY_CHARGES";
3333

3434
export const SAVE_QUERY_STATUS = "SAVE_QUERY_STATUS";
3535
export const RESET_QUERY_STATUS = "RESET_QUERY_STATUS";
36+
export const LOAD_SELECTED_CATEGORY_FROM_URL = "LOAD_SELECTED_CATEGORY_FROM_URL";
3637
export const ADD_SELECTED_CATEGORY = "ADD_SELECTED_CATEGORY";
3738
export const REMOVE_SELECTED_CATEGORY = "REMOVE_SELECTED_CATEGORY";
3839
export const RESET_SELECTED_CATEGORY = "RESET_SELECTED_CATEGORY";

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

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,10 @@ import AccordionDetails from '@material-ui/core/AccordionDetails';
66
import Typography from '@material-ui/core/Typography';
77
import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
88
import {Link} from "react-router-dom";
9-
import {useDispatch, useSelector} from "react-redux";
9+
import {useSelector} from "react-redux";
1010
import {TAB_CONFIG} from "../../../constants/constants";
1111
import log from "loglevel";
1212
import {PRODUCTS_ROUTE} from "../../../constants/react_routes";
13-
import {
14-
RESET_QUERY_STATUS,
15-
RESET_SELECT_PRODUCT_PAGE,
16-
RESET_SELECT_SORT_CATEGORY,
17-
RESET_SELECTED_CATEGORY
18-
} from "../../../actions/types";
1913

2014
const height = 48
2115

@@ -69,24 +63,12 @@ const useStyles = makeStyles((theme) => ({
6963
export default function AccordionSection() {
7064
const classes = useStyles();
7165
const tabsAPIData = useSelector(state => state.tabsDataReducer)
72-
const dispatch = useDispatch()
73-
74-
const handleLinkClick = () => {
75-
[RESET_SELECTED_CATEGORY,
76-
RESET_SELECT_PRODUCT_PAGE,
77-
RESET_SELECT_SORT_CATEGORY,
78-
RESET_QUERY_STATUS].forEach(type => {
79-
dispatch({
80-
type: type
81-
})
82-
})
83-
}
8466

8567
const renderContent = (contentList, mapKey, queryParam) => {
8668
return contentList.map(({id, value}) => {
8769
return (
8870
<Grid item key={`${mapKey}${id}`}>
89-
<Link onClick={handleLinkClick} to={`${PRODUCTS_ROUTE}?q=${queryParam}=${id}`}>
71+
<Link to={`${PRODUCTS_ROUTE}?q=${queryParam}=${id}`}>
9072
<Typography className={classes.content}>
9173
{value}
9274
</Typography>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default function TabList() {
3333
const mouseLeaveHandler = event => {
3434
log.info(`[TabList]: mouseLeaveHandler = ${event.pageX}, ${window.scrollY}`)
3535
// detect the mouse is going out horizontally and vertically upwards.
36-
if(event.pageX < 230 || event.pageX > 700 || (event.pageY - window.scrollY) < 1) {
36+
if(event.pageX < 230 || event.pageX > 795 || (event.pageY - window.scrollY) < 1) {
3737
dispatch({
3838
type: HANDLE_TAB_HOVER_EVENT, payload: {
3939
index: false,

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function ClearAllButton() {
88
const selectedFilterAttribute = useSelector(state => state.selectedFilterAttributesReducer)
99

1010
// check if any filter is selected or not
11-
if((selectedFilterAttribute.genders.length + selectedFilterAttribute.apparels.length
11+
if ((selectedFilterAttribute.genders.length + selectedFilterAttribute.apparels.length
1212
+ selectedFilterAttribute.brands.length + selectedFilterAttribute.prices.length) === 0) {
1313
log.info(`[ClearAllButton] selected attribute are null`)
1414
return null
@@ -19,15 +19,23 @@ function ClearAllButton() {
1919
*/
2020
const handleClearAllClick = () => {
2121
log.info(`[ClearAllButton] handleClearAllClick(value)`)
22-
dispatch({type: REMOVE_SELECTED_CATEGORY})
22+
dispatch({
23+
type: REMOVE_SELECTED_CATEGORY,
24+
payload: {
25+
newQuery: null
26+
}
27+
})
2328
}
2429

2530
log.info(`[ClearAllButton] Rendering ClearAllButton Component`)
2631

2732
return (
2833
<>
29-
<div onClick={handleClearAllClick} style={{fontWeight: "bold", cursor: 'pointer',
30-
fontSize: '0.9rem', height: 'inherit', color: 'red'}}>CLEAR ALL</div>
34+
<div onClick={handleClearAllClick} style={{
35+
fontWeight: "bold", cursor: 'pointer',
36+
fontSize: '0.9rem', height: 'inherit', color: 'red'
37+
}}>CLEAR ALL
38+
</div>
3139
</>
3240
);
3341
}

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

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
PAGE_ATTRIBUTE, SORT_ATTRIBUTE
1919
} from "../../../../constants/constants";
2020
import {
21-
ADD_SELECTED_CATEGORY,
21+
ADD_SELECTED_CATEGORY, LOAD_SELECTED_CATEGORY_FROM_URL,
2222
SAVE_QUERY_STATUS, SAVE_SORT_LIST, SELECT_PRODUCT_PAGE, SELECT_SORT_CATEGORY,
2323
} from "../../../../actions/types";
2424
import {PRODUCTS_ROUTE} from "../../../../constants/react_routes";
@@ -213,7 +213,7 @@ function FilterNavBar(props) {
213213
log.info(`[FilterNavBar] dispatchFilterAttributesFromURL` +
214214
`dispatching selectedFilterAttributes=${JSON.stringify(selectedFilterAttributes)}`)
215215
dispatch({
216-
type: ADD_SELECTED_CATEGORY,
216+
type: LOAD_SELECTED_CATEGORY_FROM_URL,
217217
payload: selectedFilterAttributes
218218
})
219219
}
@@ -297,35 +297,18 @@ function FilterNavBar(props) {
297297
* Component Did Update for Genders, Apparels, Brands and Prices
298298
*/
299299
useEffect(() => {
300-
log.info("[FilterNavBar] Component did mount for " +
301-
"selectedApparels, selectedGenders, selectedBrands, selectedPriceRanges.")
300+
log.info("[FilterNavBar] Component did mount for filter selection hook")
302301

303302
const {oldQuery, newQuery} = selectedFilterAttributes;
304303

305-
log.info(`[FilterNavBar] oldQuery = ${oldQuery}, newQuery = ${newQuery}`)
306304
let dispatchQueryForProducts = null
307305
let queryFromURL = history.location.search
308306

309-
if (!oldQuery) {
310-
311-
// first load and navigation scenario
312-
if (!newQuery || newQuery.localeCompare(queryFromURL) !== 0) {
313-
log.info(`[FilterNavBar] Updating from First time page load ` +
314-
`scenario is null oldQuery = ${oldQuery}, ` +
315-
`newQuery = ${newQuery}, queryFromURL = ${queryFromURL}`)
316-
317-
props.loadFilterAttributes(queryFromURL).then(data => {
318-
dispatchFilterAttributesFromURL(data, queryFromURL)
319-
dispatchSortAttributeFromURL(data, queryFromURL)
320-
dispatchPageAttributeFromURL(data, queryFromURL)
321-
dispatchSortList(data)
322-
})
307+
log.info(`[FilterNavBar] filter selection hook oldQuery = ${oldQuery}, newQuery = ${newQuery}, queryFromURL = ${queryFromURL}`)
308+
if (!newQuery) {
309+
log.info(`[FilterNavBar] filter selection hook oldQuery = ${oldQuery}, newQuery = ${newQuery}`)
323310

324-
dispatchQueryForProducts = queryFromURL
325-
}
326-
} else if (!newQuery) {
327-
log.info(`[FilterNavBar] Updating when filter is selected ` +
328-
`oldQuery = ${oldQuery}, newQuery = ${newQuery}`)
311+
// on filter selection scenario
329312
let queryPreparedFromFilters = prepareQuery()
330313

331314
props.loadFilterAttributes(queryPreparedFromFilters).then(data => {
@@ -340,7 +323,8 @@ function FilterNavBar(props) {
340323

341324
// by default first page should be selected.
342325
if (selectedPage.pageNumber > 1) {
343-
log.info(`dispatching selectedPage = ${JSON.stringify(selectedPage)}`)
326+
log.info(`[FilterNavBar] filter selection hook dispatching selectedPage = ${JSON.stringify(selectedPage)}`)
327+
344328
dispatch({
345329
type: SELECT_PRODUCT_PAGE,
346330
payload: {
@@ -355,27 +339,62 @@ function FilterNavBar(props) {
355339
}
356340

357341
if (dispatchQueryForProducts) {
358-
log.info(`selectedFilterAttributes useEffect dispatchQueryForProducts = ${dispatchQueryForProducts}`)
342+
log.info(`[FilterNavBar] filter selection hook dispatchQueryForProducts = ${dispatchQueryForProducts}`)
343+
344+
dispatch({
345+
type: SAVE_QUERY_STATUS,
346+
payload: dispatchQueryForProducts
347+
})
348+
}
349+
350+
// eslint-disable-next-line
351+
}, [selectedFilterAttributes]);
352+
353+
useEffect(() => {
354+
log.info("[FilterNavBar] Component did mount for new URL hook")
355+
356+
const {oldQuery, newQuery} = selectedFilterAttributes;
357+
358+
let dispatchQueryForProducts = null
359+
let queryFromURL = history.location.search
360+
361+
log.info(`[FilterNavBar] new URL hook oldQuery = ${oldQuery}, newQuery = ${newQuery}, queryFromURL = ${queryFromURL}`)
362+
if (!oldQuery || newQuery.localeCompare(queryFromURL) !== 0) {
363+
log.info(`[FilterNavBar] new URL hook oldQuery = ${oldQuery}, newQuery = ${newQuery}`)
364+
365+
// on links click from tabs
366+
props.loadFilterAttributes(queryFromURL).then(data => {
367+
dispatchFilterAttributesFromURL(data, queryFromURL)
368+
dispatchSortAttributeFromURL(data, queryFromURL)
369+
dispatchPageAttributeFromURL(data, queryFromURL)
370+
dispatchSortList(data)
371+
})
372+
dispatchQueryForProducts = queryFromURL
373+
}
374+
375+
if (dispatchQueryForProducts) {
376+
log.info(`[FilterNavBar] new URL hook dispatchQueryForProducts = ${dispatchQueryForProducts}`)
377+
359378
dispatch({
360379
type: SAVE_QUERY_STATUS,
361380
payload: dispatchQueryForProducts
362381
})
363382
}
364383

365384
// eslint-disable-next-line
366-
}, [selectedFilterAttributes, props]);
385+
}, [props]);
367386

368387
/**
369388
* Component Did Update for Sort and Page Options
370389
*/
371390
useEffect(() => {
372-
log.info("[FilterNavBar] Component did mount selectedPage, selectedSort.")
391+
log.info("[FilterNavBar] Component did mount selectedPage, selectedSort hook.")
373392

374393
if (!selectedPage.isLoadedFromURL || !selectedSort.isLoadedFromURL) {
375-
log.info("[FilterNavBar] Preparing query for selectedPage and selectedSort.")
394+
log.info("[FilterNavBar] selectedPage, selectedSort hook Preparing query for selectedPage and selectedSort.")
376395

377396
let query = prepareQuery()
378-
log.info(`selectedPage useEffect dispatchQueryForProducts = ${query}`)
397+
log.info(`[FilterNavBar] selectedPage, selectedSort hook dispatchQueryForProducts = ${query}`)
379398

380399
if (query) {
381400
dispatch({

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

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ export const stickyBoxStyle = {
3131
paddingLeft: "1rem"
3232
}
3333

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+
3441
function Product() {
3542
const dispatch = useDispatch();
3643

@@ -49,38 +56,15 @@ function Product() {
4956
useEffect(() => {
5057
log.info(`[Product] Component did mount...`)
5158

52-
/* TODO: Replace this reloadPage option with
53-
setting the right dependency in useEffect*/
54-
const reloadPage = () => {
55-
56-
// this is required to reload the page when user
57-
// navigates on browser using back or forward button.
58-
[RESET_SELECTED_CATEGORY,
59-
RESET_SELECT_PRODUCT_PAGE,
60-
RESET_SELECT_SORT_CATEGORY,
61-
RESET_QUERY_STATUS].forEach(type => {
62-
dispatch({
63-
type: type
64-
})
65-
})
66-
}
67-
68-
window.addEventListener("popstate", reloadPage);
69-
7059
return () => {
7160
log.info(`[Product] Component will unmount...`)
7261

73-
window.removeEventListener("popstate", reloadPage);
74-
7562
// reset the saved query as we will load
7663
// next time from the URL.
7764
// This required to support the case where user
7865
// executes URL directly and we need to construct
7966
// fresh states for eg selecting options based on URL
80-
[RESET_SELECTED_CATEGORY,
81-
RESET_SELECT_PRODUCT_PAGE,
82-
RESET_SELECT_SORT_CATEGORY,
83-
RESET_QUERY_STATUS].forEach(type => {
67+
RESET_ALL_PRODUCT_STATES.forEach(type => {
8468
dispatch({
8569
type: type
8670
})

client/src/reducers/screens/filter/selectedFilterAttributesReducer.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
ADD_SELECTED_CATEGORY,
2+
ADD_SELECTED_CATEGORY, LOAD_SELECTED_CATEGORY_FROM_URL,
33
REMOVE_SELECTED_CATEGORY, RESET_SELECT_PRODUCT_PAGE, RESET_SELECT_SORT_CATEGORY, RESET_SELECTED_CATEGORY,
44
SELECT_PRODUCT_PAGE,
55
SELECT_SORT_CATEGORY
@@ -89,11 +89,14 @@ export const selectedFilterAttributesReducer = (state = INITIAL_SELECTED_FILTER_
8989
return appendNewPayloadToPrevState(state, action.payload);
9090

9191
case REMOVE_SELECTED_CATEGORY:
92-
return {...INITIAL_SELECTED_FILTER_ATTRIBUTE_STATE, oldQuery: state.oldQuery, newQuery: null}
92+
return {...INITIAL_SELECTED_FILTER_ATTRIBUTE_STATE, oldQuery: state.oldQuery, newQuery: action.payload.newQuery}
9393

9494
case RESET_SELECTED_CATEGORY:
9595
return {...INITIAL_SELECTED_FILTER_ATTRIBUTE_STATE, newQuery: state.newQuery}
9696

97+
case LOAD_SELECTED_CATEGORY_FROM_URL:
98+
return appendNewPayloadToPrevState(INITIAL_SELECTED_FILTER_ATTRIBUTE_STATE, action.payload);
99+
97100
default:
98101
return state;
99102
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
spring.jpa.generate-ddl=true
22
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
3-
#spring.jpa.hibernate.ddl-auto=create-drop
3+
spring.jpa.hibernate.ddl-auto=create-drop
44
spring.data.redis.repositories.enabled=false

0 commit comments

Comments
 (0)