-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* New : 히스토리 관련 로컬스토리지 키 타입 추가 * New : 로컬스토리지 관리 Hooks 추가 * Refactor : 히스토리 관리 로직 처리 Hooks 로 분리 * Refactor : 스켈레톤 처리 List 컴포넌트로 위임 * Refactor : Namtag 컴포넌트 Presentational component 화 * New : 위키 디테일로 이동하는 함수 Hooks 으로 변경 * Minor : 사용하지 않는 모듈 제거, FIXME 주석 추가
- Loading branch information
1 parent
91064a8
commit 7ad4678
Showing
11 changed files
with
185 additions
and
71 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
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
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,33 @@ | ||
import { useCallback, useEffect, useState } from "react"; | ||
|
||
const useLocalStorage = <T>(storageKey: string) => { | ||
/** | ||
* 로컬스토리지에 아이템을 stringify해 저장하는 함수 | ||
*/ | ||
const setItem = useCallback( | ||
(keyword: T) => { | ||
localStorage.setItem(storageKey, JSON.stringify(keyword)); | ||
}, | ||
[storageKey] | ||
); | ||
/** | ||
* 로컬 스토리지 아이템을 파싱해서 리턴하는 함수 | ||
*/ | ||
const getItems = useCallback((): T | null => { | ||
return JSON.parse(localStorage.getItem(storageKey) || "null"); | ||
}, [storageKey]); | ||
|
||
const [storageValue, setStorageValue] = useState<T | null>(getItems()); | ||
|
||
// 새로운 값이 저장될 경우, 로컬스토리지에도 같이 저장 | ||
useEffect(() => { | ||
if (!storageValue) { | ||
return; | ||
} | ||
setItem(storageValue); | ||
}, [storageValue]); | ||
|
||
return [storageValue, setStorageValue] as const; | ||
}; | ||
|
||
export default useLocalStorage; |
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,54 @@ | ||
import { SearchHistoryKeyType } from "@/types/LocalStorageKey"; | ||
import { useCallback, useEffect } from "react"; | ||
import useLocalStorage from "../localStorage/useLocalStorage"; | ||
|
||
/** | ||
* 로컬스토리지 키를 입력받아 | ||
* 해당 스토리지를 바라보는 state를 리턴 (State 업데이트시 자동으로 반영) | ||
* | ||
* @param storageKey 로컬스토리지 키 | ||
* @returns | ||
*/ | ||
const useSearchHistory = (storageKey: SearchHistoryKeyType) => { | ||
const [searchHistory, setSearchHistory] = | ||
useLocalStorage<string[]>(storageKey); | ||
|
||
useEffect(() => { | ||
if (searchHistory === null) { | ||
setSearchHistory([]); | ||
} | ||
}, []); | ||
|
||
const removeAll = useCallback(() => { | ||
setSearchHistory([]); | ||
}, [storageKey]); | ||
|
||
const removeByKeyword = useCallback( | ||
(keyword: string) => { | ||
const filteredHistory = (searchHistory ?? []).filter( | ||
(prevKeyword) => prevKeyword !== keyword | ||
); | ||
setSearchHistory(filteredHistory); | ||
}, | ||
[storageKey] | ||
); | ||
const addSearchHistory = useCallback( | ||
(keyword: string) => { | ||
setSearchHistory((prev) => { | ||
return [ | ||
keyword, | ||
...(prev ?? []).filter((prevKeyword) => prevKeyword !== keyword), | ||
]; | ||
}); | ||
}, | ||
[storageKey] | ||
); | ||
return { | ||
state: searchHistory ?? [], | ||
add: addSearchHistory, | ||
removeAll, | ||
removeByKeyword, | ||
}; | ||
}; | ||
|
||
export default useSearchHistory; |
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,27 @@ | ||
import { AlcoholDetailInterface } from "@/types/alcohol/AlcoholInterface"; | ||
import { useRouter } from "next/navigation"; | ||
import { useCallback } from "react"; | ||
import useSearchHistory from "../searchHistory/useSearchHistory"; | ||
import { ALCOHOL_SEARCH_HISTORY } from "@/const/localstorageKey"; | ||
import { WIKI_DETAIL } from "@/const/clientPath"; | ||
/** | ||
* 검색히스토리에 해당 술을 남기고, 디테일페이지로 이동시키는 함수를 리턴하는 훅 | ||
* @returns 해당 callback함수 | ||
*/ | ||
const usePushToWikiDetail = () => { | ||
const { add: addToSearchHistory } = useSearchHistory(ALCOHOL_SEARCH_HISTORY); | ||
const router = useRouter(); | ||
/** | ||
* 검색히스토리에 해당 술을 남기고, 디테일페이지로 이동시키는 함수를 리턴하는 함수 | ||
*/ | ||
const onClickElementHandler = useCallback( | ||
({ alcoholName, alcoholNo }: AlcoholDetailInterface) => { | ||
addToSearchHistory(alcoholName); | ||
router.push(WIKI_DETAIL(String(alcoholNo))); | ||
}, | ||
[addToSearchHistory] | ||
); | ||
return onClickElementHandler; | ||
}; | ||
|
||
export default usePushToWikiDetail; |
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,4 @@ | ||
/** | ||
* 검색 기록 관련 키 타입 | ||
*/ | ||
export type SearchHistoryKeyType = "alcohol-search-history"; |