Skip to content

Commit

Permalink
fix(search): page increment logic for searchNextPage in useAddressSearch
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaewoook committed Jun 23, 2024
1 parent 7fc3f88 commit 74ceaf9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/hooks/useAddressSearch.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import axios from "axios";
import type { AxiosResponse } from "axios";
import { useCallback } from "react";
import { useRecoilState } from "recoil";
import { useRecoilState, useSetRecoilState } from "recoil";

import type { AddressSearchAPIResponse, SearchKey } from "@/shared/models/address";
import { addressListState } from "@/states/address";
import { prevSearchKeyState, searchLoadingState } from "@/states/search";
import { prevSearchKeyState, searchKeywordState, searchLoadingState } from "@/states/search";

const JUSO_API = "http://www.juso.go.kr/addrlink/addrLinkApi.do";
const API_KEY = "U01TX0FVVEgyMDIwMDUyMTEzNTUwOTEwOTc4NDI=";
Expand All @@ -14,6 +14,7 @@ export const useAddressSearch = () => {
const [prevSearchKey, setPrevSearchKey] = useRecoilState(prevSearchKeyState);
const [addressList, setAddressList] = useRecoilState(addressListState);
const [searching, setSearching] = useRecoilState(searchLoadingState);
const setSearchKeyword = useSetRecoilState(searchKeywordState);

const performSearch = useCallback(async (searchKey: SearchKey) => {
if (prevSearchKey === searchKey) {
Expand Down Expand Up @@ -53,16 +54,16 @@ export const useAddressSearch = () => {
}, [performSearch, searching, setSearching, setAddressList]);

const searchNextPage = useCallback(async () => {
if (searching || !prevSearchKey) {
if (searching || !prevSearchKey || prevSearchKey.end) {
return;
}
const searchKey = {
...prevSearchKey,
currentPage: prevSearchKey.currentPage + 1,
currentPage: (Number.parseInt(prevSearchKey.currentPage) + 1).toString(),
};
setSearching(true);
const searchResult = await performSearch(searchKey);
if (!searchResult?.juso.length) {
if (!searchResult?.juso?.length) {
setPrevSearchKey({
...searchKey,
end: true,
Expand All @@ -79,7 +80,8 @@ export const useAddressSearch = () => {
const resetSearch = useCallback(() => {
setPrevSearchKey(null);
setAddressList([]);
}, [setPrevSearchKey, setAddressList]);
setSearchKeyword("");
}, [setPrevSearchKey, setAddressList, setSearchKeyword]);

return {
addressList,
Expand Down
2 changes: 1 addition & 1 deletion src/shared/models/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type AddressResponseBody = {
errorCode: string;
errorMessage: string;
};
juso: AddressData[];
juso: AddressData[] | null;
};

export type AddressSearchAPIResponse = {
Expand Down

0 comments on commit 74ceaf9

Please sign in to comment.