diff options
| author | Factiven <[email protected]> | 2023-04-13 22:14:50 +0700 |
|---|---|---|
| committer | Factiven <[email protected]> | 2023-04-13 22:14:50 +0700 |
| commit | 5007e48c8546d8138092039bf3cecc0b9904a407 (patch) | |
| tree | 8b27c6fce4dcde5d21c04b65e1f2059924532cb4 | |
| parent | Update index.js (diff) | |
| download | moopa-5007e48c8546d8138092039bf3cecc0b9904a407.tar.xz moopa-5007e48c8546d8138092039bf3cecc0b9904a407.zip | |
Update 4th
| -rw-r--r-- | components/videoPlayer.js | 18 | ||||
| -rw-r--r-- | pages/anime/[...id].js | 60 | ||||
| -rw-r--r-- | pages/anime/watch/[...info].js | 2 | ||||
| -rw-r--r-- | pages/api/update-user.js | 10 | ||||
| -rw-r--r-- | pages/api/watched-episode.js | 43 | ||||
| -rw-r--r-- | pages/index.js | 4 | ||||
| -rw-r--r-- | pages/testing.js | 30 |
7 files changed, 117 insertions, 50 deletions
diff --git a/components/videoPlayer.js b/components/videoPlayer.js index e568772..092f53a 100644 --- a/components/videoPlayer.js +++ b/components/videoPlayer.js @@ -90,7 +90,7 @@ export default function VideoPlayer({ // use >= instead of > markProgress(aniId, progress); } else { - console.log("Something went wrong"); + return; } }); @@ -99,11 +99,25 @@ export default function VideoPlayer({ console.log("Video ended"); }); - art.on("destroy", () => { + art.on("destroy", async () => { + if (!session) return; const lastPlayed = { id: id, time: art.currentTime, }; + const res = await fetch("/api/watched-episode", { + method: "POST", + body: JSON.stringify({ + username: session?.user.name, + id: aniId, + newData: lastPlayed, + }), + headers: { + "Content-Type": "application/json", + }, + }); + + console.log(res.status); const title = titles; const prevDataStr = localStorage.getItem("lastPlayed") || "[]"; diff --git a/pages/anime/[...id].js b/pages/anime/[...id].js index d9ef319..dd9a3d8 100644 --- a/pages/anime/[...id].js +++ b/pages/anime/[...id].js @@ -7,7 +7,6 @@ import Head from "next/head"; import { closestMatch } from "closest-match"; import Content from "../../components/hero/content"; -import Image from "next/image"; import { useSession } from "next-auth/react"; @@ -23,7 +22,6 @@ export default function Himitsu({ }) { const [isLoading, setIsloading] = useState(false); const [showText, setShowtext] = useState(false); - const [title, setTitle] = useState(info.title.english || info.title.romaji); const [load, setLoad] = useState(true); const [Lang, setLang] = useState(true); const [showAll, setShowAll] = useState(false); @@ -31,6 +29,7 @@ export default function Himitsu({ const { data: session } = useSession(); const [lastPlayed, setLastPlayed] = useState(null); + const [user, setUser] = useState(null); const episode = episodeList; const epi1 = episode1; @@ -44,13 +43,22 @@ export default function Himitsu({ setLang(false); } - // const { ref } = useParallax({ speed: 10 }); - useEffect(() => { - const playedStr = JSON.parse(localStorage.getItem("lastPlayed")); - setLastPlayed( - playedStr?.filter((item) => item.title === info.title.romaji)[0]?.data - ); + async function userData() { + setLoad(false); + if (!session) return; + setLoad(true); + const res = await fetch(`/api/get-user?userName=${session?.user.name}`); + const data = await res.json(); + setLastPlayed( + data?.recentWatch.filter( + (item) => item.title.romaji === info.title.romaji + )[0]?.episode + ); + setUser(data); + setLoad(false); + } + function getBrightness(color) { const rgb = color.match(/\d+/g); return (299 * rgb[0] + 587 * rgb[1] + 114 * rgb[2]) / 1000; @@ -71,22 +79,9 @@ export default function Himitsu({ elements.forEach((element) => { setTextColor(element); }); - }, [color]); - - // const handleStore = (props) => { - // let existingData = JSON.parse(localStorage.getItem("recentWatch")); - // if (!Array.isArray(existingData)) { - // existingData = []; - // } - // const index = existingData.findIndex( - // (item) => item.title.romaji === props.title.romaji - // ); - // if (index !== -1) { - // existingData.splice(index, 1); - // } - // const updatedData = [props, ...existingData]; - // localStorage.setItem("recentWatch", JSON.stringify(updatedData)); - // }; + + userData(); + }, [color, session]); if (!info) { return; @@ -113,18 +108,9 @@ export default function Himitsu({ "Content-Type": "application/json", }, }); - // console.log(res.status); + console.log(res.status); } - // console.log({ NEXT: subIndo }); - - // console.log(episodeIndo); - - // console.log(lastPlayed); - - function handleLoad() { - setLoad(false); - } return ( <> <Head> @@ -430,12 +416,14 @@ export default function Himitsu({ </div> </div> <div className="flex h-[640px] flex-col gap-5 overflow-y-hidden scrollbar-thin scrollbar-thumb-[#1b1c21] scrollbar-thumb-rounded-full hover:overflow-y-scroll hover:scrollbar-thumb-[#2e2f37]"> - {episode && Lang ? ( + {load ? ( + <p>Loading...</p> + ) : episode && Lang ? ( episode.map((episode, index) => { const item = lastPlayed?.find( (item) => item.id === episode.id ); - // console.log(item); + console.log(item); return ( <div key={index} className="flex flex-col gap-3"> <Link diff --git a/pages/anime/watch/[...info].js b/pages/anime/watch/[...info].js index ed1a50b..075f730 100644 --- a/pages/anime/watch/[...info].js +++ b/pages/anime/watch/[...info].js @@ -60,7 +60,7 @@ export default function Info({ info }) { return <p>Loading...</p>; } - console.log(parseInt(playingEpisode)); + console.log(); return ( <> diff --git a/pages/api/update-user.js b/pages/api/update-user.js index 210d70f..67c80d0 100644 --- a/pages/api/update-user.js +++ b/pages/api/update-user.js @@ -9,6 +9,16 @@ export default async function handler(req, res) { const { name, newData } = req.body; // id is the user ID and newData is the new data you want to set try { + const existingData = await collection.findOne({ + name: name, + "recentWatch.id": newData.recentWatch.id, + }); + + if (existingData) { + res.status(200).json({ message: "Data already exists" }); + return; + } + const result = await collection.updateOne( { name: name }, { $addToSet: newData } diff --git a/pages/api/watched-episode.js b/pages/api/watched-episode.js new file mode 100644 index 0000000..271348d --- /dev/null +++ b/pages/api/watched-episode.js @@ -0,0 +1,43 @@ +// pages/api/update-user.js +import clientPromise from "../../lib/mongodb"; + +export default async function handler(req, res) { + const client = await clientPromise; + const db = client.db("authbase"); + const collection = db.collection("users"); + + const { username, id, newData } = req.body; // id is the user ID and newData is the new data you want to set + + try { + const result = await collection.updateOne( + { + name: username, + "recentWatch.id": id, + "recentWatch.episode.id": { $ne: newData.id }, + }, + { $addToSet: { "recentWatch.$.episode": newData } } + ); + + if (result.modifiedCount === 0) { + const updateResult = await collection.updateOne( + { + name: username, + "recentWatch.id": id, + "recentWatch.episode.id": newData.id, + "recentWatch.episode.time": { $ne: newData.time }, + }, + { $set: { "recentWatch.$.episode.$[elem].time": newData.time } }, + { arrayFilters: [{ "elem.id": newData.id }] } + ); + if (updateResult.modifiedCount === 0) { + console.log("The episode already exists with the same time."); + } + } + + console.log("Successfully updated the recentWatch collection."); + + res.status(200).json(result); + } catch (error) { + res.status(500).json({ error: "Unable to update user data", dat: newData }); + } +} diff --git a/pages/index.js b/pages/index.js index 6740949..6c0ccb8 100644 --- a/pages/index.js +++ b/pages/index.js @@ -113,7 +113,7 @@ export default function Home({ detail, populars }) { if (!session) return; const res = await fetch(`/api/get-user?userName=${session?.user.name}`); const data = await res.json(); - setArray(data?.recentWatch.reverse()); + setArray(data?.recentWatch); setUser(data); } function fetchData() { @@ -398,7 +398,7 @@ export default function Home({ detail, populars }) { <Content ids="recentlyWatched" section="Recently Watched" - data={array} + data={array.reverse()} /> </motion.div> )} diff --git a/pages/testing.js b/pages/testing.js index ffa8080..cae165a 100644 --- a/pages/testing.js +++ b/pages/testing.js @@ -1,26 +1,38 @@ -import { getUser } from "./api/getUser"; +import { getUser } from "./api/get-user"; +import { useSession, signIn } from "next-auth/react"; export default function Testing({ user }) { + const { data: session } = useSession(); async function handleUpdate() { - const res = await fetch("/api/update-user", { + const lastPlayed = { + id: "apahisya", + time: 812989929, + }; + const res = await fetch("/api/watched-episode", { method: "POST", body: JSON.stringify({ - name: "Factiven", - newData: { - settings: { - tracking: false, - }, - }, + username: session?.user.name, + id: 150672, + newData: lastPlayed, }), headers: { "Content-Type": "application/json", }, }); + // const data = await res.json(); // parse the response body as JSON + // console.log(data.dat.id); console.log(res.status); } console.log(user.settings); - return <button onClick={() => handleUpdate()}>Click for update</button>; + return ( + <div> + <button onClick={() => handleUpdate()}>Click for update</button> + {!session && ( + <button onClick={() => signIn("AniListProvider")}>LOGIN</button> + )} + </div> + ); } export async function getServerSideProps(context) { |