Skip to content

Commit dddf54f

Browse files
committed
fix: cache search params
1 parent 7d6cd07 commit dddf54f

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

src/api/cache.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
import { AxiosResponse, InternalAxiosRequestConfig } from 'axios';
2+
import qs from 'qs';
23

34
function getCacheRequestData(config: InternalAxiosRequestConfig<any>) {
45
if (
56
config.method === 'get' &&
67
config.url &&
78
config.headers['x-app-cache'] === 'true'
89
) {
9-
const cache = localStorage.getItem(config.url);
10+
const url = config.url + '?' + qs.stringify(config.params);
11+
const cache = localStorage.getItem(url);
1012
if (cache) {
1113
const { data, timestamp } = JSON.parse(cache);
1214
const ttl = 1000 * 60 * 5; // 5 minutes
1315
if (new Date().getTime() - timestamp >= ttl) {
14-
console.log(`cache - expirado - ${config.url}`);
15-
localStorage.removeItem(config.url);
16+
console.log(`cache - expirado - ${url}`);
17+
localStorage.removeItem(url);
1618
} else {
17-
console.log(`cache - ok - ${config.url}`);
19+
console.log(`cache - ok - ${url}`);
1820
return data;
1921
}
2022
}
@@ -23,6 +25,7 @@ function getCacheRequestData(config: InternalAxiosRequestConfig<any>) {
2325
}
2426

2527
function handleCacheResponse(resp: AxiosResponse<any, any>) {
28+
const url = resp.config.url + '?' + qs.stringify(resp.config.params);
2629
if (
2730
resp.config.method === 'get' &&
2831
resp.config.url &&
@@ -32,7 +35,7 @@ function handleCacheResponse(resp: AxiosResponse<any, any>) {
3235
) {
3336
const { data, headers, status, statusText } = resp;
3437
localStorage.setItem(
35-
resp.config.url,
38+
url,
3639
JSON.stringify({
3740
data: { data, headers, status, statusText },
3841
timestamp: new Date().getTime(),

src/hooks/useShelters/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ export interface IUseSheltersDataSupplyData {
2020
supply: { name: string };
2121
priority: number;
2222
}
23+
24+
export interface IUseShelterOptions {
25+
cache?: boolean;
26+
}

src/hooks/useShelters/useShelters.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import { AxiosRequestConfig } from 'axios';
44
import { api } from '@/api';
55
import { IServerResponse } from '@/types';
66
import { IPaginatedResponse } from '../usePaginatedQuery/types';
7-
import { IUseSheltersData } from './types';
7+
import { IUseShelterOptions, IUseSheltersData } from './types';
88

9-
const useShelters = () => {
9+
const useShelters = (options: IUseShelterOptions = {}) => {
10+
const { cache } = options;
1011
const [loading, setLoading] = useState<boolean>(false);
1112
const [data, setData] = useState<IPaginatedResponse<IUseSheltersData>>({
1213
count: 0,
@@ -18,10 +19,13 @@ const useShelters = () => {
1819
const refresh = useCallback(
1920
(config: AxiosRequestConfig<any> = {}, append: boolean = false) => {
2021
const { search, ...rest } = (config ?? {}).params ?? {};
22+
const headers = config.headers ?? {};
23+
if (cache) headers['x-app-cache'] = 'true';
2124
if (!append) setLoading(true);
2225
api
2326
.get<IServerResponse<any>>('/shelters', {
2427
...config,
28+
headers,
2529
params: {
2630
orderBy: 'prioritySum',
2731
order: 'desc',

src/pages/Home/Home.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const initialFilterData: IFilterFormProps = {
2020
};
2121

2222
const Home = () => {
23-
const { data: shelters, loading, refresh } = useShelters();
23+
const { data: shelters, loading, refresh } = useShelters({ cache: true });
2424
const {
2525
loading: loadingSession,
2626
refreshSession,

0 commit comments

Comments
 (0)