From 1e2887e4489f2d061c216b8d74d22db758fc1555 Mon Sep 17 00:00:00 2001 From: SoSmoothy <58429050+sonicname@users.noreply.github.com> Date: Wed, 28 Sep 2022 09:56:06 +0700 Subject: [PATCH] refactor code --- src/apis/apis.jsx | 19 ------------------- src/components/anime/AnimeList.jsx | 8 ++------ src/hooks/useBindEnter.jsx | 19 ------------------- src/hooks/useGetAnimeList.jsx | 10 ++++++++++ src/hooks/useSearchAnime.jsx | 4 +--- 5 files changed, 13 insertions(+), 47 deletions(-) delete mode 100644 src/apis/apis.jsx delete mode 100644 src/hooks/useBindEnter.jsx create mode 100644 src/hooks/useGetAnimeList.jsx diff --git a/src/apis/apis.jsx b/src/apis/apis.jsx deleted file mode 100644 index c76f2f9..0000000 --- a/src/apis/apis.jsx +++ /dev/null @@ -1,19 +0,0 @@ -export const getAnimeDetail = async (id) => { - const res = await fetch(`https://api.jikan.moe/v4/anime/${id}`); - return res.json(); -}; - -export const getListAnime = async (type) => { - const res = await fetch(`https://api.jikan.moe/v4/${type}`); - return res.json(); -}; - -export const getCharacterDetail = async (id) => { - const res = await fetch(`https://api.jikan.moe/v4/characters/${id}`); - return res.json(); -}; - -export const search = async (url) => { - const res = await fetch(url); - return res.json(); -}; diff --git a/src/components/anime/AnimeList.jsx b/src/components/anime/AnimeList.jsx index 14abb2e..09a3e3c 100644 --- a/src/components/anime/AnimeList.jsx +++ b/src/components/anime/AnimeList.jsx @@ -1,19 +1,15 @@ import { memo } from 'react'; import { toast } from 'react-toastify'; import { useNavigate } from 'react-router-dom'; -import { useQuery } from '@tanstack/react-query'; import { Swiper, SwiperSlide } from 'swiper/react'; -import { getListAnime } from '../../apis/apis'; - import AnimeItem from './AnimeItem'; import AnimeItemSkeleton from './AnimeItemSkeleton'; +import useGetAnimeList from '../../hooks/useGetAnimeList'; const AnimeList = ({ type }) => { const navigate = useNavigate(); - const { data, isError, isLoading } = useQuery(['list-anime', { type }], () => - getListAnime(type), - ); + const { data, isError, isLoading } = useGetAnimeList(type); if (isError) { toast.error('Something went wrong! Please try again!'); diff --git a/src/hooks/useBindEnter.jsx b/src/hooks/useBindEnter.jsx deleted file mode 100644 index d65e622..0000000 --- a/src/hooks/useBindEnter.jsx +++ /dev/null @@ -1,19 +0,0 @@ -import { useEffect } from 'react'; - -const useBindEnter = (setQuery = () => {}, inputRef) => { - useEffect(() => { - const handlerEnterKeyPress = (e) => { - if (e.code === 'Enter') { - setQuery(inputRef.current.value); - } - }; - - document.addEventListener('keyup', handlerEnterKeyPress); - - return () => { - document.removeEventListener('keyup', handlerEnterKeyPress); - }; - }, []); -}; - -export default useBindEnter; diff --git a/src/hooks/useGetAnimeList.jsx b/src/hooks/useGetAnimeList.jsx new file mode 100644 index 0000000..c324117 --- /dev/null +++ b/src/hooks/useGetAnimeList.jsx @@ -0,0 +1,10 @@ +import { useQuery } from '@tanstack/react-query'; + +const useGetAnimeList = (type) => { + return useQuery( + ['list-anime', { type }], + async () => await (await fetch(`https://api.jikan.moe/v4/${type}`)).json(), + ); +}; + +export default useGetAnimeList; diff --git a/src/hooks/useSearchAnime.jsx b/src/hooks/useSearchAnime.jsx index a5fd3b2..ee9dc49 100644 --- a/src/hooks/useSearchAnime.jsx +++ b/src/hooks/useSearchAnime.jsx @@ -1,11 +1,9 @@ import { useInfiniteQuery } from '@tanstack/react-query'; -import { search } from '../apis/apis'; - const useSearchAnime = (type, query, url) => { return useInfiniteQuery( ['search', { type, query }], - ({ pageParam = url }) => search(pageParam), + async ({ pageParam = url }) => (await fetch(pageParam)).json(), { getNextPageParam: (lastPage, _) => lastPage.pagination.has_next_page