Skip to content
This repository has been archived by the owner on Jan 4, 2023. It is now read-only.

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
sonicname committed Sep 27, 2022
1 parent 87d72e7 commit 21e20ca
Show file tree
Hide file tree
Showing 24 changed files with 246 additions and 133 deletions.
10 changes: 4 additions & 6 deletions src/components/anime-details/DetailListItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import { NavLink } from 'react-router-dom';
const DetailListItem = ({ items, title }) => {
return (
<div className='flex items-center gap-x-2'>
<h3 className='font-semibold text-white text-sm'>{title}: </h3>
<h3 className='text-sm font-semibold text-white'>{title}: </h3>
<div className='flex flex-wrap gap-5'>
{items.map((item) => (
<NavLink key={item.mal_id} to={'/search/genres'}>
<span className='p-2 bg-blue-500 rounded-md font-medium hover:opacity-70 transition-all'>
{item.name}
</span>
</NavLink>
<span className='p-1 font-medium transition-all bg-blue-500 rounded-md cursor-pointer'>
{item.name}
</span>
))}
</div>
</div>
Expand Down
16 changes: 9 additions & 7 deletions src/components/anime/AnimeItem.jsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
import { useNavigate } from 'react-router-dom';
import { memo } from 'react';
import { useNavigate } from 'react-router-dom';

import Button from '../buttons/Button';
import IconStar from '../icons/IconStar';
import IconFavorite from '../icons/IconFavorite';
import Button from '../buttons/Button';

const AnimeItem = ({ anime }) => {
const navigate = useNavigate();
const { title, score, favorites, images, mal_id } = anime;

return (
<div className='shadow-md bg-slate-900 flex flex-col gap-y-1 h-full rounded-lg select-none'>
<div className='flex flex-col h-full rounded-lg shadow-md select-none bg-slate-900 gap-y-1'>
<div className='w-full h-[350px] rounded-t-lg'>
<img
srcSet={`${images.jpg.large_image_url} 2x`}
alt=''
className='w-full h-full object-cover rounded-t-lg'
className='object-cover w-full h-full rounded-t-lg'
/>
</div>
<div className='flex flex-1 flex-col gap-y-3 p-3'>
<h3 className='text-md font-semibold'>{title}</h3>
<div className='flex flex-col flex-1 p-3 gap-y-3'>
<h3 className='font-semibold text-md'>{title}</h3>
<div className='flex items-center justify-between mt-auto'>
<div className='flex items-center gap-x-1'>
<span className='text-sm opacity-80'>{score || '0'}</span>

<IconStar className={'h-[16px] w-[16px] flex items-center text-yellow-300'} />
<IconStar
className={'h-[16px] w-[16px] flex items-center text-yellow-300'}
/>
</div>

<div className='flex items-center gap-x-1'>
Expand Down
11 changes: 7 additions & 4 deletions src/components/anime/AnimeItemSkeleton.jsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { memo } from 'react';

import IconStar from '../icons/IconStar';
import IconFavorite from '../icons/IconFavorite';
import LoadingSkeleton from '../loading/LoadingSkeleton';
import { memo } from 'react';

const AnimeItemSkeleton = () => {
return (
<div className='shadow-md bg-slate-900 flex flex-col gap-y-1 h-full rounded-lg select-none'>
<div className='flex flex-col h-full rounded-lg shadow-md select-none bg-slate-900 gap-y-1'>
<div className='w-full h-[350px] rounded-t-lg'>
<LoadingSkeleton width={'100%'} height={'100%'} radius={'8px'} />
</div>
<div className='flex flex-1 flex-col gap-y-3 p-3'>
<div className='flex flex-col flex-1 p-3 gap-y-3'>
<LoadingSkeleton width={'100%'} height={'20px'} radius={'6px'} />
<div className='flex items-center justify-between mt-auto'>
<div className='flex items-center gap-x-1'>
<LoadingSkeleton width={'20px'} height={'20px'} radius={'6px'} />

<IconStar className={'h-[16px] w-[16px] flex items-center text-yellow-300'} />
<IconStar
className={'h-[16px] w-[16px] flex items-center text-yellow-300'}
/>
</div>

<div className='flex items-center gap-x-1'>
Expand Down
18 changes: 11 additions & 7 deletions src/components/anime/AnimeList.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
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 { getListAnime } from '../../apis/apis';
import { useQuery } from '@tanstack/react-query';
import { toast } from 'react-toastify';
import { useNavigate } from 'react-router-dom';
import { memo } from 'react';

const AnimeList = ({ type }) => {
const { data, isError, isLoading } = useQuery(['list-anime', type], () => getListAnime(type));
const navigate = useNavigate();
const { data, isError, isLoading } = useQuery(['list-anime', { type }], () =>
getListAnime(type),
);

if (isError) {
toast.error('Something went wrong! Please try again!');
Expand All @@ -20,7 +24,7 @@ const AnimeList = ({ type }) => {
<div className='anime-list'>
<Swiper grabCursor={'true'} spaceBetween={20} slidesPerView={'auto'}>
{isLoading &&
new Array(10).fill(0).map((item, index) => (
new Array(10).fill(0).map((_, index) => (
<SwiperSlide key={index}>
<AnimeItemSkeleton />
</SwiperSlide>
Expand Down
20 changes: 11 additions & 9 deletions src/components/character/CharacterItem.jsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
import { memo } from 'react';
import Button from '../buttons/Button';
import { useNavigate } from 'react-router-dom';

import Button from '../buttons/Button';
import IconFavorite from '../icons/IconFavorite';

const CharacterItem = ({ character }) => {
const CharacterItem = ({ character: { mal_id, images, name, favorites } }) => {
const navigate = useNavigate();
const { mal_id, images, name, favorites } = character;
return (
<div className='shadow-md bg-slate-900 flex flex-col gap-y-1 h-full rounded-lg select-none'>
<div className='flex flex-col h-full rounded-lg shadow-md select-none bg-slate-900 gap-y-1'>
<div className='w-full h-[350px] rounded-t-lg'>
<img
srcSet={`${images.jpg.image_url} 2x`}
alt=''
className='w-full h-full object-cover rounded-t-lg'
className='object-cover w-full h-full rounded-t-lg'
/>
</div>
<div className='flex flex-1 flex-col gap-y-3 p-3 h-full'>
<h3 className='text-md font-semibold'>{name}</h3>
<div className='flex mt-auto justify-end'>
<div className='flex flex-col flex-1 h-full p-3 gap-y-3'>
<h3 className='font-semibold text-md'>{name}</h3>
<div className='flex justify-end mt-auto'>
<div className='flex gap-x-1'>
<span className='text-sm opacity-80'>{favorites || '0'}</span>
<IconFavorite className={'h-[16px] w-[16px] text-red-500'} />
</div>
</div>

<Button onClick={() => navigate(`/character/${mal_id}`)}>More Info</Button>
<Button onClick={() => navigate(`/character/${mal_id}`)}>
More Info
</Button>
</div>
</div>
);
Expand Down
7 changes: 4 additions & 3 deletions src/components/character/CharacterItemSkeleton.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { memo } from 'react';

import IconFavorite from '../icons/IconFavorite';
import LoadingSkeleton from '../loading/LoadingSkeleton';

const CharacterItemSkeleton = () => {
return (
<div className='shadow-md bg-slate-900 flex flex-col gap-y-1 h-full rounded-lg select-none'>
<div className='flex flex-col h-full rounded-lg shadow-md select-none bg-slate-900 gap-y-1'>
<div className='w-full h-[350px] rounded-t-lg'>
<LoadingSkeleton width={'100%'} height={'100%'} radius={'8px'} />
</div>
<div className='flex flex-1 flex-col gap-y-3 p-3 h-full'>
<div className='flex flex-col flex-1 h-full p-3 gap-y-3'>
<LoadingSkeleton width={'100%'} height={'20px'} radius={'6px'} />
<div className='flex mt-auto justify-end'>
<div className='flex justify-end mt-auto'>
<div className='flex gap-x-1'>
<LoadingSkeleton width={'20px'} height={'20px'} radius={'6px'} />
<IconFavorite className={'h-[16px] w-[16px] text-red-500'} />
Expand Down
6 changes: 5 additions & 1 deletion src/components/header/HeaderHamburger.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ const HeaderHamburger = ({ show }) => {
show && 'rotate-45 translate-y-[14px]'
} transition-all`}
/>
<span className={`w-full h-[4px] bg-white ${show && 'scale-0'} transition-all`} />
<span
className={`w-full h-[4px] bg-white ${
show && 'scale-0'
} transition-all`}
/>
<span
className={`w-full h-[4px] bg-white ${
show && '-rotate-45 -translate-y-[14px]'
Expand Down
8 changes: 5 additions & 3 deletions src/components/header/HeaderLink.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { NavLink } from 'react-router-dom';
import { memo } from 'react';
import { NavLink } from 'react-router-dom';

const HeaderLink = ({ to, setShow, children }) => {
return (
<li className='m-3 font-semibold w-full lg:w-auto'>
<li className='w-full m-3 font-semibold lg:w-auto'>
<NavLink
className={({ isActive }) => `p-3 ${isActive && 'bg-green-500'} text-white rounded-lg`}
className={({ isActive }) =>
`p-3 ${isActive && 'bg-green-500'} text-white rounded-lg`
}
to={to}
onClick={() => setShow(false)}
>
Expand Down
12 changes: 6 additions & 6 deletions src/components/index.jsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import AnimeItem from './anime/AnimeItem';
import AnimeItemSkeleton from './anime/AnimeItemSkeleton';
import AnimeList from './anime/AnimeList';
import AnimeItemSkeleton from './anime/AnimeItemSkeleton';

import DetailListItem from './anime-details/DetailListItem';
import DetailStatus from './anime-details/DetailStatus';
import DetailListItem from './anime-details/DetailListItem';

import Button from './buttons/Button';

import CharacterItem from './character/CharacterItem';
import CharacterItemSkeleton from './character/CharacterItemSkeleton';

import HeaderHamburger from './header/HeaderHamburger';
import HeaderLink from './header/HeaderLink';
import HeaderHamburger from './header/HeaderHamburger';

import IconStar from './icons/IconStar';
import IconRank from './icons/IconRank';
import IconEmail from './icons/IconEmail';
import IconFavorite from './icons/IconFavorite';
import IconPhone from './icons/IconPhone';
import IconRank from './icons/IconRank';
import IconStar from './icons/IconStar';
import IconFavorite from './icons/IconFavorite';
import IconUserGroup from './icons/IconUserGroup';

import ShareLayout from './layout/ShareLayout';
Expand Down
17 changes: 9 additions & 8 deletions src/components/layout/Footer.jsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import { memo } from 'react';

import IconEmail from '../icons/IconEmail';
import IconPhone from '../icons/IconPhone';

const Footer = () => {
return (
<div className='bg-slate-900'>
<div className='page-container grid grid-cols-1 md:grid-cols-2 text-md'>
<div className='text-slate-400 flex-col p-3 hidden md:flex italic'>
<div className='grid grid-cols-1 page-container md:grid-cols-2 text-md'>
<div className='flex-col hidden p-3 italic text-slate-400 md:flex'>
<h4>
“There are no regrets. If one can be proud of one’s life, one should not wish for
another chance.”
“There are no regrets. If one can be proud of one’s life, one should
not wish for another chance.”
</h4>
<h4 className='text-end mt-3'>Saber (Fate Stay Night)</h4>
<h4 className='mt-3 text-end'>Saber (Fate Stay Night)</h4>
</div>

<div className='flex flex-col items-start md:items-end p-3 text-slate-400'>
<div className='flex flex-col items-start p-3 md:items-end text-slate-400'>
<h4>Contact</h4>
<div className='flex items-center gap-x-3 mt-3'>
<div className='flex items-center mt-3 gap-x-3'>
<span>[email protected]</span>
<IconEmail className={'h-5 w-5'} />
</div>

<div className='flex items-center gap-x-3 mt-3'>
<div className='flex items-center mt-3 gap-x-3'>
<span>+843092002</span>
<IconPhone className={'h-5 w-5'} />
</div>
Expand Down
11 changes: 7 additions & 4 deletions src/components/layout/Header.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { memo, useState } from 'react';
import { NavLink } from 'react-router-dom';

import HeaderLink from '../header/HeaderLink';
import HeaderHamburger from '../header/HeaderHamburger';

Expand All @@ -25,13 +26,15 @@ const Header = () => {
const [show, setShow] = useState(false);

return (
<header className='w-full bg-slate-900 text-white top-0 sticky select-none z-50'>
<div className='page-container flex items-center justify-between relative z-10'>
<header className='sticky top-0 z-50 w-full text-white select-none bg-slate-900'>
<div className='relative z-10 flex items-center justify-between page-container'>
<NavLink
className={({ isActive }) => `p-3 ${isActive && 'bg-green-500'} text-white rounded-lg`}
className={({ isActive }) =>
`p-3 ${isActive && 'bg-green-500'} text-white rounded-lg`
}
to={'/'}
>
<h1 className='font-bold text-xl'>Home</h1>
<h1 className='text-xl font-bold'>Home</h1>
</NavLink>

<div
Expand Down
3 changes: 2 additions & 1 deletion src/components/layout/ShareLayout.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { memo } from 'react';
import { Outlet } from 'react-router-dom';

import Header from './Header';
import Footer from './Footer';
import { Outlet } from 'react-router-dom';

const ShareLayout = () => {
return (
Expand Down
20 changes: 10 additions & 10 deletions src/components/loading/LoadingComponent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import { memo } from 'react';

const LoadingComponent = () => {
return (
<div className="fixed inset-0 w-screen h-screen bg-gray-400 flex items-center justify-center">
<div className='fixed inset-0 flex items-center justify-center w-screen h-screen bg-gray-400'>
<div>
<svg
role="status"
className="mr-2 w-16 h-16 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
role='status'
className='w-16 h-16 mr-2 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600'
viewBox='0 0 100 101'
fill='none'
xmlns='http://www.w3.org/2000/svg'
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
d='M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z'
fill='currentColor'
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
d='M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z'
fill='currentFill'
/>
</svg>
</div>
Expand Down
19 changes: 19 additions & 0 deletions src/hooks/useBindEnter.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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;
9 changes: 9 additions & 0 deletions src/hooks/useGetAnimeDetail.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useQuery } from '@tanstack/react-query';

const useGetAnimeDetail = (animeID) => {
return useQuery(['anime', { animeID }], async () =>
(await fetch(`https://api.jikan.moe/v4/anime/${animeID}`)).json(),
);
};

export default useGetAnimeDetail;
9 changes: 9 additions & 0 deletions src/hooks/useGetCharacterDetail.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useQuery } from '@tanstack/react-query';

const useGetCharacterDetail = (characterID) => {
return useQuery(['character', { characterID }], async () =>
(await fetch(`https://api.jikan.moe/v4/characters/${characterID}`)).json(),
);
};

export default useGetCharacterDetail;
Loading

1 comment on commit 21e20ca

@vercel
Copy link

@vercel vercel bot commented on 21e20ca Sep 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.