1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
import { ChevronLeftIcon } from "@heroicons/react/24/solid";
import Image from "next/image";
import Link from "next/link";
import { Fragment, useEffect, useState } from "react";
import Skeleton from "react-loading-skeleton";
import Footer from "@/components/shared/footer";
import { getServerSession } from "next-auth";
import { authOptions } from "../../api/auth/[...nextauth]";
import Head from "next/head";
import MobileNav from "@/components/shared/MobileNav";
export default function TrendingAnime({ sessions }) {
const [data, setData] = useState(null);
const [page, setPage] = useState(1);
const [nextPage, setNextPage] = useState(true);
const [loading, setLoading] = useState(true);
useEffect(() => {
setLoading(true);
const fetchData = async () => {
const res = await fetch("https://graphql.anilist.co", {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify({
query: `query ($page: Int, $perPage: Int) {
Page (page: $page, perPage: $perPage) {
pageInfo {
total
currentPage
lastPage
hasNextPage
perPage
}
media (sort: TRENDING_DESC, type: ANIME) {
id
idMal
title {
romaji
}
coverImage {
large
}
averageScore
description
episodes
status
}
}
}
`,
variables: {
page: page,
perPage: 20,
},
}),
});
const get = await res.json();
if (get?.data?.Page?.media?.length === 0) {
setNextPage(false);
} else if (get !== null && page > 1) {
setData((prevData) => {
return [...(prevData ?? []), ...get?.data?.Page?.media];
});
setNextPage(get?.data?.Page?.pageInfo.hasNextPage);
} else {
setData(get?.data?.Page?.media);
}
setNextPage(get?.data?.Page?.pageInfo.hasNextPage);
setLoading(false);
};
fetchData();
}, [page]);
useEffect(() => {
function handleScroll() {
if (page > 5 || !nextPage) {
window.removeEventListener("scroll", handleScroll);
return;
}
if (
window.innerHeight + window.pageYOffset >=
document.body.offsetHeight - 3
) {
setPage((prevPage) => prevPage + 1);
}
}
window.addEventListener("scroll", handleScroll);
return () => window.removeEventListener("scroll", handleScroll);
}, [page, nextPage]);
return (
<Fragment>
<Head>
<title>Moopa - Trending Anime</title>
<meta name="title" content="Trending Anime" />
<meta
name="description"
content="Explore Top Trending Anime - Dive into the latest and most popular anime series on Moopa. From thrilling action to heartwarming romance, discover the buzzworthy shows that have everyone talking. Stream now and stay up-to-date with the hottest anime trends!"
/>
</Head>
<MobileNav sessions={sessions} />
<main className="flex flex-col gap-2 items-center min-h-screen w-screen px-2 relative pb-10">
<div className="z-50 bg-primary pt-5 pb-3 shadow-md shadow-primary w-full fixed px-3">
<Link href="/en" className="flex gap-2 items-center font-karla">
<ChevronLeftIcon className="w-5 h-5" />
<h1 className="text-xl">Trending Now</h1>
</Link>
</div>
<div className="grid grid-cols-2 xs:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 xl:grid-cols-6 2xl:grid-cols-6 gap-3 max-w-6xl pt-16">
{data?.map((i, index) => (
<div
key={index}
className="flex flex-col items-center w-[150px] lg:w-[180px]"
>
<Link
href={`/en/anime/${i.id}`}
className="p-2"
title={i.title.romaji}
>
<Image
src={i.coverImage.large}
alt={i.title.romaji}
width={500}
height={500}
className="w-[140px] h-[190px] lg:w-[170px] lg:h-[230px] object-cover rounded hover:scale-105 scale-100 transition-all duration-200 ease-out"
/>
</Link>
<Link
href={`/en/anime/${i.id}`}
className="w-full px-2"
title={i.title.romaji}
>
<h1 className="font-karla font-bold xl:text-base text-[15px] line-clamp-2">
{i.status === "RELEASING" ? (
<span className="dots bg-green-500" />
) : i.status === "NOT_YET_RELEASED" ? (
<span className="dots bg-red-500" />
) : null}
{i.title.romaji}
</h1>
</Link>
</div>
))}
{loading && (
<>
{[1, 2, 4, 5, 6, 7, 8].map((item) => (
<div
key={item}
className="flex flex-col items-center w-[150px] lg:w-[180px]"
>
<div className="w-full p-2">
<Skeleton className="w-[140px] h-[190px] lg:w-[170px] lg:h-[230px] rounded" />
</div>
<div className="w-full px-2">
<Skeleton width={80} height={20} />
</div>
</div>
))}
</>
)}
</div>
{!loading && page > 5 && nextPage && (
<button
onClick={() => setPage((p) => p + 1)}
className="bg-secondary xl:w-[30%] w-[80%] h-10 rounded-md mt-5"
>
Load More
</button>
)}
</main>
<Footer />
</Fragment>
);
}
export async function getServerSideProps(context) {
const session = await getServerSession(context.req, context.res, authOptions);
return {
props: {
sessions: session,
},
};
}
|