-
Notifications
You must be signed in to change notification settings - Fork 34
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 #87 from vatsalsinghkv/feat--local-storage
feat: filters save in local storage
- Loading branch information
Showing
16 changed files
with
412 additions
and
262 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
v18.18.0 | ||
v20.6.0 |
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,9 @@ | ||
{ | ||
"recommendations": [ | ||
// Tailwind CSS Intellisense | ||
"bradlc.vscode-tailwindcss", | ||
"esbenp.prettier-vscode", | ||
"dbaeumer.vscode-eslint", | ||
"aaron-bond.better-comments" | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function Provider({ children }: { children: React.ReactNode }) { | ||
return; | ||
} |
Empty file.
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,90 @@ | ||
import { Label, sortedLabels } from '@/models/Label'; | ||
import { Language } from '@/models/Language'; | ||
import { Ordering } from '@/models/Ordering'; | ||
import { SortingTag } from '@/models/SortingTag'; | ||
import { useUrlValues } from '@/providers/urlProvider/reducer'; | ||
import { FormEvent, createContext, useContext, useState } from 'react'; | ||
|
||
type FilterContextType = { | ||
customLabel: string; | ||
language: Language; | ||
label: string; | ||
ordering: Ordering; | ||
sortingTag: SortingTag; | ||
labels: Label[]; | ||
customLabelHandler: (e: FormEvent) => void; | ||
setCustomLabel: (label: string) => void; | ||
languageChangeHandler: (payload: Language) => () => void; | ||
labelsChangeHandler: (payload: Label) => () => void; | ||
sortingTagClickHandler: (payload: SortingTag) => () => void; | ||
}; | ||
|
||
const initialState: FilterContextType = { | ||
customLabel: '', | ||
language: 'all', | ||
label: 'hacktoberfest', | ||
ordering: 'asc', | ||
sortingTag: 'best-match', | ||
labels: sortedLabels, | ||
customLabelHandler: () => {}, | ||
setCustomLabel: () => {}, | ||
languageChangeHandler: () => () => {}, | ||
labelsChangeHandler: () => () => {}, | ||
sortingTagClickHandler: () => () => {}, | ||
}; | ||
|
||
const FilterContext = createContext<FilterContextType>(initialState); | ||
|
||
export default function FilterProvider({ | ||
children, | ||
}: { | ||
children: React.ReactNode; | ||
}) { | ||
const [customLabel, setCustomLabel] = useState<string>(''); | ||
const [labels, setLabels] = useState<Label[]>(sortedLabels); | ||
|
||
const customLabelHandler = (e: FormEvent) => { | ||
e.preventDefault(); | ||
setLabels([...labels, customLabel]); | ||
labelsChangeHandler(customLabel as Label)(); | ||
setCustomLabel(''); | ||
}; | ||
|
||
const { dispatch, language, ordering, sortingTag, label } = useUrlValues(); | ||
|
||
const languageChangeHandler = (payload: Language) => { | ||
return () => dispatch({ type: 'update-language', payload }); | ||
}; | ||
|
||
const labelsChangeHandler = (payload: Label) => { | ||
return () => dispatch({ type: 'update-label', payload }); | ||
}; | ||
|
||
const sortingTagClickHandler = (payload: SortingTag) => { | ||
return () => dispatch({ type: 'update-sorting-tag', payload }); | ||
}; | ||
|
||
return ( | ||
<FilterContext.Provider | ||
value={{ | ||
customLabel, | ||
label, | ||
language, | ||
ordering, | ||
sortingTag, | ||
labels, | ||
customLabelHandler, | ||
languageChangeHandler, | ||
labelsChangeHandler, | ||
sortingTagClickHandler, | ||
setCustomLabel, | ||
}} | ||
> | ||
{children} | ||
</FilterContext.Provider> | ||
); | ||
} | ||
|
||
export function useFilter() { | ||
return useContext(FilterContext); | ||
} |
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,31 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
export default function useLocalStorage<T>( | ||
key: string, | ||
initialValue: T | ||
): readonly [T, (val: T | ((val: T) => T)) => void, () => void] { | ||
const [storedValue, setStoredValue] = useState<T>(() => { | ||
const value = window.localStorage.getItem(key); | ||
|
||
if (value === 'true' || value === 'false') { | ||
return JSON.parse(value) as T; | ||
} | ||
|
||
return value ? (value as T) : initialValue; | ||
}); | ||
|
||
useEffect(() => { | ||
if (typeof storedValue === 'boolean') { | ||
window.localStorage.setItem(key, JSON.stringify(storedValue)); | ||
} else { | ||
window.localStorage.setItem(key, storedValue as string); | ||
} | ||
}, [storedValue, key]); | ||
|
||
const removeValue = () => { | ||
window.localStorage.removeItem(key); | ||
setStoredValue(initialValue); | ||
}; | ||
|
||
return [storedValue, setStoredValue, removeValue] as const; | ||
} |
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.
Oops, something went wrong.