Skip to content

Commit

Permalink
feat: filters save in localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
vatsalsinghkv committed Oct 31, 2024
1 parent 7ecd954 commit db2ffdd
Show file tree
Hide file tree
Showing 13 changed files with 260 additions and 224 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"parserOptions": { "ecmaVersion": "latest", "sourceType": "module" },
"settings": { "react": { "version": "18.2" } },
"plugins": ["react-refresh", "unused-imports", "vitest"],
"parser": "@babel/eslint-parser",
"rules": {
"react-refresh/only-export-components": [
"warn",
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v18.18.0
v20.6.0
9 changes: 9 additions & 0 deletions .vscode/extensions.json
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"
]
}
2 changes: 1 addition & 1 deletion src/__tests__/hooks/useUrlValues.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
DEFAULT_PAGE,
DEFAULT_SORTING_TAG,
} from '@/lib/utils/config';
import UrlProvider, { useUrlValues } from '@/providers/urlProvider';
import UrlProvider, { useUrlValues } from '@/providers/urlProvider/reducer';
import { act, render } from '@testing-library/react';
import { beforeEach, describe, expect, it } from 'vitest';

Expand Down
2 changes: 1 addition & 1 deletion src/containers/Issues.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getTotalPages, toId } from '@/lib/utils';
import httpGateway from '@/lib/utils/HttpGateway';
import { MAX_ISSUES_ALLOWED } from '@/lib/utils/config';
import { githubIssueSearchResponse } from '@/models/GithubIssueSearch';
import { useUrlValues } from '@/providers/urlProvider';
import { useUrlValues } from '@/providers/urlProvider/reducer';
import { useEffect } from 'react';

const Issues = () => {
Expand Down
Empty file added src/lib/api/requrest.ts
Empty file.
3 changes: 1 addition & 2 deletions src/lib/hooks/use-filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ 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';
import { useUrlValues } from '@/providers/urlProvider/reducer';
import { FormEvent, createContext, useContext, useState } from 'react';

// Define the type for the context state
type FilterContextType = {
customLabel: string;
language: Language;
Expand Down
19 changes: 14 additions & 5 deletions src/lib/hooks/use-local-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,28 @@ export default function useLocalStorage<T>(
key: string,
initialValue: T
): readonly [T, (val: T | ((val: T) => T)) => void, () => void] {
const [storedValue, setStoredValue] = useState(() => {
const [storedValue, setStoredValue] = useState<T>(() => {
const value = window.localStorage.getItem(key);
return value ? JSON.parse(value) : initialValue;

if (value === 'true' || value === 'false') {
return JSON.parse(value) as T;
}

return value ? (value as T) : initialValue;
});

useEffect(() => {
window.localStorage.setItem(key, storedValue);
}, [storedValue]);
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];
return [storedValue, setStoredValue, removeValue] as const;
}
2 changes: 1 addition & 1 deletion src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ReactDOM from 'react-dom/client';

import FilterProvider from './lib/hooks/use-filter';
import ThemeProvider from './lib/hooks/use-theme';
import UrlProvider from './providers/urlProvider';
import UrlProvider from './providers/urlProvider/reducer';

ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
Expand Down
58 changes: 0 additions & 58 deletions src/providers/urlProvider/index.tsx

This file was deleted.

155 changes: 0 additions & 155 deletions src/providers/urlProvider/reducer.ts

This file was deleted.

Loading

0 comments on commit db2ffdd

Please sign in to comment.