Skip to content

Commit

Permalink
Refactor: 포켓몬 전체 데이터를 불러오는 로직 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
cdm1263 committed Jul 16, 2024
1 parent 03bcd2e commit 4dbcc1f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
9 changes: 6 additions & 3 deletions src/components/cardMaker/CardEditPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import Inner from '../Inner';
import { addDocument, getCountDocument } from '@/lib/firebaseQuery';
import useUserStore from '@/store/useUsersStore';
import { MouseEvent, useState } from 'react';
import { filteredPokemonData } from '@/lib/type';
import { useGetAllPokemon } from '@/query/qeuries';
import { PokemonType, filteredPokemonData } from '@/lib/type';
import { usePokemonQueries } from '@/query/qeuries';
import SearchPokemon from './searchPokemon/SearchPokemon';
import { useEffect } from 'react';
import SelectPokemonMobile from './selectPokemon/SelectPokemonMobile';
import { useRouter } from 'next/navigation';
import { UseQueryResult } from 'react-query';
import useFlatData from '@/hook/useFlatData';

const CardEditPage = () => {
const [isSaving, setIsSaving] = useState<boolean>(false);
Expand All @@ -28,7 +30,8 @@ const CardEditPage = () => {
setPokemonData,
pokemonName,
} = useSelectedPokemonForCard();
const { data } = useGetAllPokemon(1017);
const queries = usePokemonQueries(1017);
const data = useFlatData(queries as UseQueryResult<PokemonType>[]);
const filteredPokemonData = {} as filteredPokemonData;

useEffect(() => {
Expand Down
7 changes: 5 additions & 2 deletions src/components/cardMaker/searchPokemon/SearchPokemon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import styles from './search.module.scss';
import { POKEMON_NAME } from '@/lib/pokemonName';
import { reverseObject } from '@/lib/util/reverseObject';
import { POKEMON_TYPES } from '@/lib/constants';
import { useGetAllPokemon } from '@/query/qeuries';
import { usePokemonQueries } from '@/query/qeuries';
import { IoSearch } from '@react-icons/all-files/io5/IoSearch';
import { PokemonType, TypesType } from '@/lib/type';
import SearchDropdown from './SearchDropdown';
import useFlatData from '@/hook/useFlatData';
import { UseQueryResult } from 'react-query';

const SearchPokemon = () => {
const [text, setText] = useState<string>('');
Expand All @@ -15,7 +17,8 @@ const SearchPokemon = () => {
const [foundPokemon, setFoundPokemon] = useState<
(PokemonType | undefined)[] | null
>(null);
const { data } = useGetAllPokemon(1017);
const queries = usePokemonQueries(1017);
const data = useFlatData(queries as UseQueryResult<PokemonType>[]);

useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { PokemonType } from '@/lib/type';
import { useGetAllPokemon } from '@/query/qeuries';
import { usePokemonQueries } from '@/query/qeuries';
import { useCallback, useEffect, useState } from 'react';
import SelectPokemonLayout from './SelectPokemonLayout';
import styles from './select.module.scss';
import useCalculateInnerWidth from '@/hook/useCalculateInnerWidth';
import useFlatData from '@/hook/useFlatData';
import { UseQueryResult } from 'react-query';

const SelectPokemonRandom = () => {
const { data } = useGetAllPokemon(1017);
const queries = usePokemonQueries(1017);
const data = useFlatData(queries as UseQueryResult<PokemonType>[]);
const [randomPokemons, setRandomPokemons] = useState<PokemonType[]>([]);
const windowWidth = useCalculateInnerWidth();
let POKEMONS_PER_PAGE = 3;
Expand Down
17 changes: 10 additions & 7 deletions src/hook/useFilteredPokemonData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ import { POKEMON_NAME } from '@/lib/pokemonName';
import { PokemonType, TypesType } from '@/lib/type';
import { reverseObject } from '@/lib/util/reverseObject';
import { useMemo } from 'react';
import { useGetAllPokemon } from '@/query/qeuries';
import { usePokemonQueries } from '@/query/qeuries';
import useSelectedStore from '@/store/useSelectedStore';
import useSearchInputText from '@/store/useSearchInputText';
import useFlatData from './useFlatData';
import { UseQueryResult } from 'react-query';

const useFilteredPokemonData = () => {
const { data: allPokemonData, isLoading } = useGetAllPokemon(1017);
const queries = usePokemonQueries(1017);
const allData = useFlatData(queries as UseQueryResult<PokemonType>[]);
const { selectedPlate } = useSelectedStore();
const { inputText } = useSearchInputText();
const reversedPokemonNameObject = reverseObject(POKEMON_NAME);

const filteredData = useMemo(() => {
if (!allPokemonData) return [];

return allPokemonData.filter((data: PokemonType) => {
if (!allData.length) return [];
return allData.filter((data: PokemonType) => {
if (inputText) {
return reversedPokemonNameObject[data?.name]?.includes(inputText);
}
Expand All @@ -30,8 +32,9 @@ const useFilteredPokemonData = () => {
),
);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [allPokemonData, inputText, selectedPlate]);
}, [allData, inputText, selectedPlate]);

const isLoading = !allData.length;

return { filteredData, isLoading };
};
Expand Down

0 comments on commit 4dbcc1f

Please sign in to comment.