import { useState, useEffect } from 'react'; type WatchDataResponse = { result: { data: { json: WatchData } } }; type WatchData = { id: number, title: string, description: string, imageKey: string, currentEpisode: string, tmdbID: string, finished: boolean, startedOn: Date, createdAt: Date, updatedAt: Date, createdById: string } export default function RecentWatch() { const [ data, setData ] = useState(null); const [ loading, setLoading ] = useState(true); const [ errorState, setErrorState ] = useState(""); useEffect(() => { fetch("https://watch.alexav.gg/api/trpc/watchItem.getLatest", { method: 'get' }) .then(async (res) => { if (res.ok) { const json = await res.json() as WatchDataResponse; setData(json.result.data.json); setLoading(false) } else { setErrorState("Failed to fetch data!"); setLoading(false); } }) .catch((e) => { setLoading(false); setErrorState(e.message) }) }, []); const parseDate = (date: Date) => { const jsDate = new Date(date); return `${jsDate.getFullYear()}/${jsDate.getMonth() + 1}/${jsDate.getDate()}`; } return (
{ loading ? (

Loading..

) : !errorState ? (

{data?.title}

{ data?.finished ? (

Finished!

) : (

Started on {parseDate(new Date(data?.startedOn as Date))}

) }

{data?.currentEpisode}

) : ( <>

Failed to fetch :(

Check out watch.alexav.gg if this isn't working.

) }
) }