import { useState, useEffect } from "react"; import { useParams } from "react-router-dom"; import { Person, AddOpinionRequest, Game, GameList } from "../items"; import { apiFetch } from "./api"; export const PersonDetails = () => { const { name } = useParams<{ name: string }>(); const [person, setPerson] = useState(null); const [games, setGames] = useState([]); const [gameTitle, setGameTitle] = useState(""); const [wouldPlay, setWouldPlay] = useState(false); useEffect(() => { apiFetch("/api/games") .then((res) => res.arrayBuffer()) .then((buffer) => { try { const list = GameList.decode(new Uint8Array(buffer)); setGames(list.games); if (list.games.length > 0) { setGameTitle(list.games[0].title); } } catch (e) { console.error("Failed to decode games:", e); } }) .catch(console.error); }, []); useEffect(() => { if (name) { apiFetch(`/api/${name}`) .then((res) => res.arrayBuffer()) .then((buffer) => { try { setPerson(Person.decode(new Uint8Array(buffer))); } catch (e) { console.error("Failed to decode person:", e); } }) .catch(console.error); } }, [name]); const handleAddOpinion = async () => { if (!person) return; const req = AddOpinionRequest.create({ gameTitle, wouldPlay, }); const buffer = AddOpinionRequest.encode(req).finish(); try { const res = await apiFetch("/api/opinion", { method: "POST", headers: { "Content-Type": "application/octet-stream", }, body: buffer, }); if (res.ok) { const resBuffer = await res.arrayBuffer(); setPerson(Person.decode(new Uint8Array(resBuffer))); setGameTitle(""); setWouldPlay(false); } } catch (e) { console.error(e); } }; if (!person) return
Loading...
; return (

{person.name}

    {person.opinion.map((op, i) => (
  • {op.title} - {op.wouldPlay ? "Would Play" : "Would Not Play"}
  • ))}

Add Opinion

); };