33 * Created by Kevin Li 12/21/17
44 */
55
6- import React , { useCallback , useEffect , useState } from 'react' ;
6+ import React , { useCallback } from 'react' ;
77import PropTypes from 'prop-types' ;
88import { useDispatch , useSelector } from 'react-redux' ;
99
1010import { areFiltersEqual } from 'helpers/searchHelper' ;
1111import SearchSidebarSubmit from 'components/search/SearchSidebarSubmit' ;
12- import { initialState , initialStateFY } from 'redux/reducers/search/searchFiltersReducer' ;
12+ import { initialState , initialStateDR } from 'redux/reducers/search/searchFiltersReducer' ;
1313import { clearAllFilters as clearStagedFilters } from 'redux/actions/search/searchFilterActions' ;
1414import { resetMapLegendToggle } from 'redux/actions/search/mapLegendToggleActions' ;
1515import {
1616 applyStagedFilters ,
1717 resetAppliedFilters , setAppliedFilterCompletion
1818} from "redux/actions/search/appliedFilterActions" ;
19- import usePrevious from "hooks/usePrevious" ;
2019import {
2120 convertFiltersToAnalyticEvents ,
2221 sendAnalyticEvents ,
@@ -32,38 +31,17 @@ const SearchSidebarSubmitContainer = ({ setShowMobileFilters }) => {
3231 } = useSelector ( ( state ) => state . appliedFilters ) ;
3332 const stagedFilters = useSelector ( ( state ) => state . filters ) ;
3433 const dispatch = useDispatch ( ) ;
35- const [ filtersChanged , setFiltersChanged ] = useState ( false ) ;
36- const prevStagedFilters = usePrevious ( stagedFilters ) ;
37- const prevAppliedFilters = usePrevious ( appliedFilters ) ;
3834
35+ const filtersChanged = ! areFiltersEqual ( stagedFilters , appliedFilters ) ;
3936 const areStagedFiltersEmpty = areFiltersEqual ( stagedFilters , initialState ) ||
40- areFiltersEqual ( stagedFilters , initialStateFY ) ;
41-
42- const compareStores = ( ) => {
43- // we need to do a deep equality check by comparing every store key
44- const storeKeys = Object . keys ( stagedFilters ) ;
45- if ( storeKeys . length !== Object . keys ( appliedFilters ) . length ) {
46- // key lengths do not match, there's a difference so fail immediately
47- return false ;
48- }
49- return areFiltersEqual ( stagedFilters , appliedFilters ) ;
50- } ;
37+ areFiltersEqual ( stagedFilters , initialStateDR ) ;
5138
5239 const resetFilters = useCallback ( ( ) => {
5340 dispatch ( clearStagedFilters ( ) ) ;
5441 dispatch ( resetAppliedFilters ( ) ) ;
5542 dispatch ( resetMapLegendToggle ( ) ) ;
5643 } , [ dispatch ] ) ;
5744
58- const stagingChanged = ( ) => {
59- // do a deep equality check between the staged filters and applied filters
60- if ( ! compareStores ( ) ) {
61- setFiltersChanged ( true ) ;
62- }
63- else if ( filtersChanged ) {
64- setFiltersChanged ( false ) ;
65- }
66- } ;
6745
6846 const applyFilters = useCallback ( ( ) => {
6947 dispatch ( setAppliedFilterCompletion ( false ) ) ;
@@ -75,24 +53,12 @@ const SearchSidebarSubmitContainer = ({ setShowMobileFilters }) => {
7553 dispatch ( applyStagedFilters ( stagedFilters ) ) ;
7654 dispatch ( setAppliedFilterCompletion ( true ) ) ;
7755 }
78- setFiltersChanged ( false ) ;
7956
8057 const events = convertFiltersToAnalyticEvents ( stagedFilters ) ;
8158 sendAnalyticEvents ( events ) ;
8259 sendFieldCombinations ( events ) ;
8360 } , [ dispatch , resetFilters , stagedFilters ] ) ;
8461
85- useEffect ( ( ) => {
86- const areStagedAndAppliedFiltersEquivalent = (
87- areFiltersEqual ( stagedFilters , prevStagedFilters ) &&
88- areFiltersEqual ( appliedFilters , prevAppliedFilters )
89- ) ;
90- if ( ! areStagedAndAppliedFiltersEquivalent ) {
91- stagingChanged ( ) ;
92- }
93- // eslint-disable-next-line react-hooks/exhaustive-deps
94- } , [ stagedFilters , appliedFilters ] ) ;
95-
9662 return (
9763 < SearchSidebarSubmit
9864 stagedFiltersAreEmpty = { areStagedFiltersEmpty }
0 commit comments