Skip to content

Commit

Permalink
Feat: #35 fetchOnMouseEnter 함수 추가
Browse files Browse the repository at this point in the history
* 유틸 폴더에 추가하였다.
* 네개의 컴포넌트에서 함께 사용한다.
  • Loading branch information
somedaycode committed Jun 17, 2021
1 parent d8656f8 commit 907588b
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions FE/issue-tracker/src/utils/fetchOnEnter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { hostAPI } from '@const/var';
import { SetterOrUpdater } from 'recoil';

type FetchOnEnter = {
query: string;
data: never[];
cachingTime: number;
};

type State = SetterOrUpdater<FetchOnEnter>;

export const fetchOnMouseEnter = async (
{ query, cachingTime }: FetchOnEnter,
setState: State
) => {
const shouldNotFetch = (Date.now() - cachingTime) / 1000 < 3600;
if (shouldNotFetch) return;

const res = await fetch(`${hostAPI}/${query}`);
const fetchedData = await res.json();
setState({
query: query,
data: fetchedData,
cachingTime: Date.now(),
});
};

0 comments on commit 907588b

Please sign in to comment.