-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #758 from IntersectMBO/604-bt-ga-display-searching…
…-sort-order-and-filtering-is-not-kept-after-back-from-item-details-planned-to-next-iter [#604] GA Display: search, sort and filter is not kept after Back fro…
- Loading branch information
Showing
10 changed files
with
148 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import React, { | ||
createContext, | ||
useContext, | ||
useState, | ||
useCallback, | ||
Dispatch, | ||
SetStateAction, | ||
useEffect, | ||
useMemo, | ||
FC, | ||
} from "react"; | ||
import { useLocation } from "react-router-dom"; | ||
|
||
import { useDebounce } from "@hooks"; | ||
|
||
interface DataActionsBarContextType { | ||
chosenFilters: string[]; | ||
chosenFiltersLength: number; | ||
chosenSorting: string; | ||
closeFilters: () => void; | ||
closeSorts: () => void; | ||
debouncedSearchText: string; | ||
filtersOpen: boolean; | ||
searchText: string; | ||
setChosenFilters: Dispatch<SetStateAction<string[]>>; | ||
setChosenSorting: Dispatch<SetStateAction<string>>; | ||
setFiltersOpen: Dispatch<SetStateAction<boolean>>; | ||
setSearchText: Dispatch<SetStateAction<string>>; | ||
setSortOpen: Dispatch<SetStateAction<boolean>>; | ||
sortingActive: boolean; | ||
sortOpen: boolean; | ||
} | ||
|
||
const DataActionsBarContext = createContext< | ||
DataActionsBarContextType | undefined | ||
>(undefined); | ||
DataActionsBarContext.displayName = "DataActionsBarContext"; | ||
|
||
interface ProviderProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
const DataActionsBarProvider: FC<ProviderProps> = ({ children }) => { | ||
const [searchText, setSearchText] = useState<string>(""); | ||
const debouncedSearchText = useDebounce(searchText, 300); | ||
const [filtersOpen, setFiltersOpen] = useState<boolean>(false); | ||
const [chosenFilters, setChosenFilters] = useState<string[]>([]); | ||
const [sortOpen, setSortOpen] = useState<boolean>(false); | ||
const [chosenSorting, setChosenSorting] = useState<string>(""); | ||
const [lastPath, setLastPath] = useState<string>(""); | ||
|
||
const { pathname } = useLocation(); | ||
|
||
const closeFilters = useCallback(() => { | ||
setFiltersOpen(false); | ||
}, []); | ||
|
||
const closeSorts = useCallback(() => { | ||
setSortOpen(false); | ||
}, []); | ||
|
||
const resetState = useCallback(() => { | ||
setSearchText(""); | ||
setChosenFilters([]); | ||
setChosenSorting(""); | ||
}, []); | ||
|
||
const userMovedToDifferentAppArea = | ||
pathname !== lastPath && !pathname.startsWith(lastPath); | ||
const userOpenedGADetailsFromCategoryPage = | ||
lastPath.includes("governance_actions/category") && | ||
pathname.includes("governance_actions/"); | ||
const userMovedFromGAListToCategoryPage = | ||
lastPath.endsWith("governance_actions") && | ||
pathname.includes("governance_actions/category"); | ||
|
||
useEffect(() => { | ||
if ( | ||
(userMovedToDifferentAppArea && !userOpenedGADetailsFromCategoryPage) || | ||
userMovedFromGAListToCategoryPage | ||
) { | ||
resetState(); | ||
} | ||
}, [pathname, resetState]); | ||
|
||
useEffect(() => { | ||
setLastPath(pathname); | ||
}, [searchText, chosenFilters, chosenSorting]); | ||
|
||
const contextValue = useMemo( | ||
() => ({ | ||
chosenFilters, | ||
chosenFiltersLength: chosenFilters.length, | ||
chosenSorting, | ||
closeFilters, | ||
closeSorts, | ||
debouncedSearchText, | ||
filtersOpen, | ||
searchText, | ||
setChosenFilters, | ||
setChosenSorting, | ||
setFiltersOpen, | ||
setSearchText, | ||
setSortOpen, | ||
sortingActive: Boolean(chosenSorting), | ||
sortOpen, | ||
}), | ||
[ | ||
chosenFilters, | ||
chosenSorting, | ||
debouncedSearchText, | ||
filtersOpen, | ||
searchText, | ||
sortOpen, | ||
closeFilters, | ||
closeSorts, | ||
], | ||
); | ||
|
||
return ( | ||
<DataActionsBarContext.Provider value={contextValue}> | ||
{children} | ||
</DataActionsBarContext.Provider> | ||
); | ||
}; | ||
|
||
function useDataActionsBar() { | ||
const context = useContext(DataActionsBarContext); | ||
if (!context) { | ||
throw new Error( | ||
"useDataActionsBar must be used within a DataActionsBarProvider", | ||
); | ||
} | ||
return context; | ||
} | ||
|
||
export { DataActionsBarProvider, useDataActionsBar }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters