-
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.
[#604] GA Display: search, sort and filter is not kept after Back fro…
…m Item details
- Loading branch information
1 parent
b4f132f
commit c7f9e06
Showing
4 changed files
with
137 additions
and
3 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,131 @@ | ||
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; | ||
updateLastPath: (path: string) => void; | ||
} | ||
|
||
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 updateLastPath = useCallback((path: string) => { | ||
setLastPath(path); | ||
}, []); | ||
|
||
const resetState = useCallback(() => { | ||
setSearchText(""); | ||
setChosenFilters([]); | ||
setChosenSorting(""); | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (lastPath && pathname !== lastPath && !pathname.startsWith(lastPath)) { | ||
resetState(); | ||
} | ||
}, [pathname, lastPath, 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, | ||
updateLastPath, | ||
}), | ||
[ | ||
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