Skip to content

Commit

Permalink
added api calls in lib folder
Browse files Browse the repository at this point in the history
  • Loading branch information
ShivaBhattacharjee committed Aug 27, 2023
1 parent 8473dc1 commit fbd2ccd
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 125 deletions.
13 changes: 2 additions & 11 deletions src/app/details/[animeId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,12 @@ import EpisodeLoading from '@/components/loading/EpisodeLoading';
import RelationLoading from '@/components/loading/RelationLoading';
import CharactersLoading from '@/components/loading/CharactersLoading';
import RecommendedLoading from '@/components/loading/RecommendedLoading';
import { getAnimeDetails } from '@/lib/GetAnime';
export default async function page({ params }: {
params:
{ animeId: number }
}) {
const getAnimeDetails = async () => {
try {
const response = await fetch(`https://animetrix-api.vercel.app/meta/anilist/info/${params.animeId}`);
const data = await response.json();
return data;
} catch (error) {
console.error("Error fetching details:", error);
return [];
}
};
const details = await getAnimeDetails()
const details = await getAnimeDetails(params.animeId)

if (Object.keys(details).length === 0 || !details.title) {
return (
Expand Down
12 changes: 1 addition & 11 deletions src/app/movie/page.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
import ServerError from '@/components/error/ServerError';
import VerticalCards from '@/components/shared/cards/VerticalCards';
import AnimeApi from '@/lib/animeapi/animetrixapi';
import React from 'react'
const getAnimeMovies = async () => {
try {
const response = await fetch(`${AnimeApi}/MOVIE?perPage=24`);
const data = await response.json();
return data.results;
} catch (error) {
console.error("Error fetching trending anime:", error);
return [];
}
};
import { getAnimeMovies } from '@/lib/GetAnime';
export default async function page() {
const Movies = await getAnimeMovies()
return (
Expand Down
22 changes: 1 addition & 21 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,7 @@ import UpcomingSeason from '@/components/shared/upcomingSeason/UpcomingSeason';
import { RandomAnimeCard } from "@/components/shared/RandomAnimeCard"
import { UpcomingSeasonLoading } from '@/components/loading/UpcomingSeasonLoading';
import RandomAnimeLoading from '@/components/loading/RandomAnimeLoading';
import AnimeApi from '@/lib/animeapi/animetrixapi';
const getTrendingAnime = async () => {
try {
const response = await fetch(`${AnimeApi}/trending?perPage=20`);
const data = await response.json();
return data.results;
} catch (error) {
console.error("Error fetching trending anime:", error);
return [];
}
};
const getPopularAnime = async () => {
try {
const response = await fetch(`${AnimeApi}/popular?perPage=20`);
const data = await response.json();
return data.results;
} catch (error) {
console.error("Error fetching popular anime:", error);
return [];
}
};
import { getTrendingAnime, getPopularAnime } from '@/lib/GetAnime';

export default async function page() {
const Trending = await getTrendingAnime()
Expand Down
12 changes: 1 addition & 11 deletions src/app/popular/page.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
import ServerError from '@/components/error/ServerError';
import VerticalCards from '@/components/shared/cards/VerticalCards';
import AnimeApi from '@/lib/animeapi/animetrixapi';
import React from 'react'
const getPopularAnime = async () => {
try {
const response = await fetch(`${AnimeApi}/popular?perPage=24`);
const data = await response.json();
return data.results;
} catch (error) {
console.error("Error fetching trending anime:", error);
return [];
}
};
import { getPopularAnime } from '@/lib/GetAnime';
export default async function page() {
const Popular = await getPopularAnime()
return (
Expand Down
11 changes: 1 addition & 10 deletions src/app/trending/page.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import ServerError from '@/components/error/ServerError';
import VerticalCards from '@/components/shared/cards/VerticalCards';
import React from 'react'
const getTrendingAnime = async () => {
try {
const response = await fetch("https://animetrix-api.vercel.app/meta/anilist/trending?perPage=24");
const data = await response.json();
return data.results;
} catch (error) {
console.error("Error fetching trending anime:", error);
return [];
}
};
import { getTrendingAnime } from '@/lib/GetAnime';
export default async function page() {
const trending = await getTrendingAnime()
return (
Expand Down
16 changes: 2 additions & 14 deletions src/components/shared/RandomAnimeCard.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
import React from 'react';
import ReloadFunc from '../error/ReloadFunc';
import Link from 'next/link';
import AnimeApi from '@/lib/animeapi/animetrixapi';
const getRandomAnime = async () => {
try {
const response = await fetch(`${AnimeApi}/random-anime`, {
cache: "no-store"
});

const data = await response.json();
return data;
} catch (error) {
console.error("Error fetching random anime:", error);
return null;
}
};
import { getRandomAnime } from '@/lib/GetAnime';
getRandomAnime

export async function RandomAnimeCard() {
const randomAnime = await getRandomAnime();
Expand Down
17 changes: 4 additions & 13 deletions src/components/shared/RecommendedAnime.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
import React from 'react'
import Cards from './cards/Cards';
import ContentNotFound from '../error/Contentnotfound';
import { getAnimeDetails } from '@/lib/GetAnime';
export async function RecommendedAnime({ episode }: { episode: number }) {
const getAnimeDetails = async () => {
try {
const response = await fetch(`https://animetrix-api.vercel.app/meta/anilist/info/${episode}`);
const data = await response.json();
return data.recommendations;
} catch (error) {
console.error("Error fetching details:", error);
return [];
}
};
const details = await getAnimeDetails()
const details = await getAnimeDetails(episode)
return (
<>
{details.length > 0 ? (
{details.recommendations.length > 0 ? (
<>
<h1 className=' text-4xl font-semibold'>Recommended</h1>
<Cards props={details} /></>
<Cards props={details.recommendations} /></>
) : (
<ContentNotFound message='recommendations' />
)}
Expand Down
13 changes: 1 addition & 12 deletions src/components/shared/airingschedule/AiringSchedule.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import React from 'react';
import AiringScheduleCard from './AiringScheduleCard';

const getAiringSchedule = async () => {
try {
const response = await fetch(`https://api.anify.tv/schedule?apikey=${process.env.NEXT_PUBLIC_ANIFY_KEY}`, {
cache: 'no-cache'
});
return response.json();
} catch (error) {
console.error("Error getting airing list: ", error);
return [];
}
}
import { getAiringSchedule } from '@/lib/GetAnime';

export default async function AiringSchedule() {
const Airing = await getAiringSchedule();
Expand Down
6 changes: 4 additions & 2 deletions src/components/shared/airingschedule/AiringScheduleCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ const AiringScheduleCard: React.FC<AiringScheduleCardProps> = ({ airingData }) =
<div className="bg-white/10 h-auto max-h-[400px] md:max-h-[600px] w-full rounded-lg mt-5 overflow-y-auto">
<div className="flex flex-col gap-3">
<div className='p-4'>
<div className='flex flex-col gap-3'>
<div className='flex flex-col gap-3 '>
{animeForCurrentDay === undefined ? (
<ReloadFunc message={`Oops! No schedule found for ${currentDay}`} />
<div className='flex justify-center items-center text-center'>
<ReloadFunc message={`Oops! No schedule found for ${currentDay}`} />
</div>
) : (
animeForCurrentDay?.map((anime: Anime) => (
<div className='flex justify-between items-center' key={anime.id}>
Expand Down
19 changes: 2 additions & 17 deletions src/components/shared/upcomingSeason/UpcomingSeason.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
import React from 'react';
import UpcomingSeasonCard from './UpcomingSeasonCard';
import Anime from '@/types/animetypes';
import AnimeApi from '@/lib/animeapi/animetrixapi';
const getCurrentYear = () => {
return new Date().getFullYear();
};

const getAnimeData = async (season: string): Promise<Anime[]> => {
try {
const response = await fetch(`${AnimeApi}/advanced-search?season=${season}&&year=${getCurrentYear()}`);
const data = await response.json();
return data.results;
} catch (error) {
console.error("Error fetching recent anime:", error);
return [];
}
};
import { getUpcomingData, getCurrentYear } from '@/lib/GetAnime';

export default async function UpcomingSeason() {
const seasons = ['FALL', 'SUMMER', 'WINTER', 'SPRING'];
const animeDataPromises = seasons.map(season => getAnimeData(season));
const animeDataPromises = seasons.map(season => getUpcomingData(season));
const animeDataResults = await Promise.all(animeDataPromises);

return (
Expand Down
89 changes: 89 additions & 0 deletions src/lib/GetAnime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { AnifyApi, AnimeApi } from "./animeapi/animetrixapi";
// trending anime
export const getTrendingAnime = async () => {
try {
const response = await fetch(`${AnimeApi}/trending?perPage=20`);
const data = await response.json();
return data.results;
} catch (error) {
console.error("Error fetching trending anime:", error);
return [];
}
};
// popular anime
export const getPopularAnime = async () => {
try {
const response = await fetch(`${AnimeApi}/popular?perPage=20`);
const data = await response.json();
return data.results;
} catch (error) {
console.error("Error fetching popular anime:", error);
return [];
}
};
// get anime movies
export const getAnimeMovies = async () => {
try {
const response = await fetch(`${AnimeApi}/MOVIE?perPage=24`);
const data = await response.json();
return data.results;
} catch (error) {
console.error("Error fetching anime movies:", error);
return [];
}
};

// get random anime
export const getRandomAnime = async () => {
try {
const response = await fetch(`${AnimeApi}/random-anime`, {
cache: "no-store"
});

const data = await response.json();
return data;
} catch (error) {
console.error("Error fetching random anime:", error);
return null;
}
};

// get airing schedule
export const getAiringSchedule = async () => {
try {
const response = await fetch(`${AnifyApi}/schedule?apikey=${process.env.NEXT_PUBLIC_ANIFY_KEY}`, {
cache: 'no-cache'
});
return response.json();
} catch (error) {
console.error("Error getting airing list: ", error);
return [];
}
}
// upcoming seasons
export const getCurrentYear = () => {
return new Date().getFullYear();
};

export const getUpcomingData = async (season: string) => {
try {
const response = await fetch(`${AnimeApi}/advanced-search?season=${season}&&year=${getCurrentYear()}`);
const data = await response.json();
return data.results;
} catch (error) {
console.error("Error fetching upcoming seasons:", error);
return [];
}
};
// get anime details
export const getAnimeDetails = async (animeid: number) => {
try {
const response = await fetch(`${AnimeApi}/info/${animeid}`);
const data = await response.json();
return data;
} catch (error) {
console.error("Error fetching details:", error);
return [];
}
};

1 change: 0 additions & 1 deletion src/lib/GetPopularAnime.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/lib/animeapi/animetrixapi.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const AnimeApi = `https://animetrix-api.vercel.app/meta/anilist`
export default AnimeApi
export const AnimeApi = `https://animetrix-api.vercel.app/meta/anilist`
export const AnifyApi = `https://api.anify.tv`

0 comments on commit fbd2ccd

Please sign in to comment.