Skip to content

Commit

Permalink
store settings with react-cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
rayaqin committed Jan 9, 2024
1 parent a9a1b76 commit 068c42c
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 8 deletions.
125 changes: 125 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"eslint-plugin-react-refresh": "^0.4.5",
"identity-obj-proxy": "^3.0.0",
"jest": "^29.7.0",
"react-cookie": "^7.0.1",
"react-icons": "^4.12.0",
"sass": "^1.69.7",
"ts-jest": "^29.1.1",
Expand Down
2 changes: 0 additions & 2 deletions src/components/ToggleButton/ToggleButton.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ h4 {

.tgl {
display: none;

// add default box-sizing for this scope
&,
&:after,
&:before,
Expand Down
5 changes: 4 additions & 1 deletion src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.tsx'
import { CookiesProvider } from 'react-cookie'

ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<App />
<CookiesProvider >
<App />
</CookiesProvider>
</React.StrictMode>,
)
18 changes: 13 additions & 5 deletions src/pages/Spendings/Spendings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CurrencyEnum, CurrencyOptionsEnum, TSpendingEntry, TSpendingsListSettin
import { useQuery } from 'react-query';
import { extractValidData } from '../../utils/validData';
import { assembleSpendingUrl } from '../../utils/assembleSpendingUrl';
import { useCookies } from 'react-cookie';

const spendingURL: string = process.env.VITE_SPENDING_API_URL as string;

Expand All @@ -24,19 +25,22 @@ const sortSpendingsList = (spendingsList: TSpendingEntry[], sortOrder: TSpending
});
};

const defaultSettings: TSpendingsListSettings = {
sortOrder: '-spent_at',
currencyFilter: CurrencyOptionsEnum.ALL,
clientSideSortAndFilter: false,
};


const Spendings: React.FC = () => {

const [cookies, setCookie] = useCookies<keyof TSpendingsListSettings>(['currencyFilter', 'sortOrder', 'clientSideSortAndFilter']);

const defaultSettings: TSpendingsListSettings = {
sortOrder: cookies.sortOrder || '-spent_at',
currencyFilter: cookies.currencyFilter || CurrencyOptionsEnum.ALL,
clientSideSortAndFilter: cookies.clientSideSortAndFilter || false,
};
const [listSettings, setListSettings] = useState<TSpendingsListSettings>(defaultSettings);

const assembledURL = assembleSpendingUrl(listSettings, spendingURL);


const { data, isError, isLoading, refetch } = useQuery(['spendings', listSettings.sortOrder, listSettings.currencyFilter], () => {
return fetch(assembledURL)
.then((response) => {
Expand All @@ -50,6 +54,10 @@ const Spendings: React.FC = () => {

const handleSettingsChange = (newListSettings: Partial<TSpendingsListSettings>) => {
setListSettings((previousState) => ({ ...previousState, ...newListSettings }));
setCookie('currencyFilter', newListSettings.currencyFilter ?? cookies.currencyFilter);
setCookie('sortOrder', newListSettings.sortOrder ?? cookies.sortOrder);
setCookie('clientSideSortAndFilter', newListSettings.clientSideSortAndFilter ?? cookies.clientSideSortAndFilter);

}

const triggerRefetch = () => {
Expand Down

0 comments on commit 068c42c

Please sign in to comment.