import { ButtonHTMLAttributes, useEffect, useRef, useState } from "react"; import { ReactSortable, ReactSortableProps } from "react-sortablejs"; import { apiAuth, User } from "./api"; import { TeamState, useSession } from "./Session"; import { Chemistry, MVPRanking, PlayerType } from "./types"; import TabController from "./TabController"; type PlayerListProps = Partial> & { orderedList?: boolean; }; function PlayerList(props: PlayerListProps) { return ( {props.list?.map((item, index) => ( {props.orderedList ? index + 1 + ". " + item.display_name : item.display_name} ))} ); } const LoadButton = (props: ButtonHTMLAttributes) => { return ( ๐๏ธ restore previous ); }; const ClearButton = (props: ButtonHTMLAttributes) => { return ( ๐๏ธ start over ); }; function filterSort(list: User[], ids: number[]): User[] { const objectMap = new Map(list.map((obj) => [obj.id, obj])); const filteredAndSortedObjects = ids .map((id) => objectMap.get(id)) .filter((obj) => obj !== undefined); return filteredAndSortedObjects; } interface PlayerInfoProps { user: User; teams: TeamState; players: User[]; } function ChemistryDnD({ user, teams, players }: PlayerInfoProps) { var otherPlayers = players.filter((player) => player.id !== user.id); const [playersLeft, setPlayersLeft] = useState([]); const [playersMiddle, setPlayersMiddle] = useState(otherPlayers); const [playersRight, setPlayersRight] = useState([]); const [loading, setLoading] = useState(false); useEffect(() => { handleGet(); }, [players]); const [dialog, setDialog] = useState("dialog"); const dialogRef = useRef(null); async function handleSubmit() { if (dialogRef.current) dialogRef.current.showModal(); setDialog("sending..."); let left = playersLeft.map(({ id }) => id); let middle = playersMiddle.map(({ id }) => id); let right = playersRight.map(({ id }) => id); const data = { user: user.id, hate: left, undecided: middle, love: right, team: teams.activeTeam, }; const response = await apiAuth("chemistry", data, "PUT"); setDialog(response || "try sending again"); } async function handleGet() { setLoading(true); const data = await apiAuth(`chemistry/${teams.activeTeam}`, null, "GET"); if (data.detail) { console.log(data.detail); setPlayersRight([]); setPlayersMiddle(otherPlayers); setPlayersLeft([]); } else { const chemistry = data as Chemistry; setPlayersLeft(filterSort(otherPlayers, chemistry.hate)); setPlayersMiddle( otherPlayers.filter( (player) => !chemistry.hate.includes(player.id) && !chemistry.love.includes(player.id) ) ); setPlayersRight(filterSort(otherPlayers, chemistry.love)); } setLoading(false); } return ( <> { setPlayersRight([]); setPlayersMiddle(otherPlayers); setPlayersLeft([]); }} /> {loading ? ( ) : ( ๐ฌ {playersLeft.length < 1 && ( drag people here that you'd rather not play with )} ๐คท ๐ {playersRight.length < 1 && ( drag people here that you love playing with from best to ... ok )} )} handleSubmit()}> ๐พ submit { event.currentTarget.close(); }} > {dialog} > ); } function TypeDnD({ user, teams, players }: PlayerInfoProps) { const [availablePlayers, setAvailablePlayers] = useState(players); const [handlers, setHandlers] = useState([]); const [combis, setCombis] = useState([]); const [cutters, setCutters] = useState([]); const [loading, setLoading] = useState(false); useEffect(() => { handleGet(); }, [players]); const [dialog, setDialog] = useState("dialog"); const dialogRef = useRef(null); async function handleSubmit() { if (dialogRef.current) dialogRef.current.showModal(); setDialog("sending..."); let handlerlist = handlers.map(({ id }) => id); let combilist = combis.map(({ id }) => id); let cutterlist = cutters.map(({ id }) => id); const data = { user: user.id, handlers: handlerlist, combis: combilist, cutters: cutterlist, team: teams.activeTeam, }; const response = await apiAuth("playertype", data, "PUT"); setDialog(response || "try sending again"); } async function handleGet() { setLoading(true); const data = await apiAuth(`playertype/${teams.activeTeam}`, null, "GET"); if (data.detail) { console.log(data.detail); setAvailablePlayers(players); setHandlers([]); setCombis([]); setCutters([]); } else { const playertype = data as PlayerType; setAvailablePlayers( players.filter( (player) => !playertype.handlers.includes(player.id) && !playertype.combis.includes(player.id) && !playertype.cutters.includes(player.id) ) ); setHandlers(filterSort(players, playertype.handlers)); setCombis(filterSort(players, playertype.combis)); setCutters(filterSort(players, playertype.cutters)); } setLoading(false); } return ( <> { setAvailablePlayers(players); setHandlers([]); setCombis([]); setCutters([]); }} /> handler {handlers.length < 1 && ( drag people here that you like to see as handlers )} combi {combis.length < 1 && ( drag people here that switch between handling and cutting )} cutter {cutters.length < 1 && ( drag people here that you think are the best cutters )} handleSubmit()}> ๐พ submit { event.currentTarget.close(); }} > {dialog} > ); } function MVPDnD({ user, teams, players }: PlayerInfoProps) { const [availablePlayers, setAvailablePlayers] = useState(players); const [rankedPlayers, setRankedPlayers] = useState([]); const [loading, setLoading] = useState(false); useEffect(() => { handleGet(); }, [players]); const [dialog, setDialog] = useState("dialog"); const dialogRef = useRef(null); async function handleSubmit() { if (dialogRef.current) dialogRef.current.showModal(); setDialog("sending..."); let mvps = rankedPlayers.map(({ id }) => id); const data = { user: user.id, mvps: mvps, team: teams.activeTeam }; const response = await apiAuth("mvps", data, "PUT"); response ? setDialog(response) : setDialog("try sending again"); } async function handleGet() { setLoading(true); const data = await apiAuth(`mvps/${teams.activeTeam}`, null, "GET"); if (data.detail) { console.log(data.detail); setAvailablePlayers(players); setRankedPlayers([]); } else { const mvps = data as MVPRanking; setRankedPlayers(filterSort(players, mvps.mvps)); setAvailablePlayers( players.filter((user) => !mvps.mvps.includes(user.id)) ); } setLoading(false); } return ( <> { setAvailablePlayers(players); setRankedPlayers([]); }} /> {loading ? ( ) : ( ๐ฅ๐ {availablePlayers.length < 1 && ( all sorted ๐ )} ๐ {rankedPlayers.length < 1 && ( carefully place as many of the Most Valuable Players{" "} (according to your humble opinion) in this box )} )} handleSubmit()}> ๐พ submit { event.currentTarget.close(); }} > {dialog} > ); } interface HeaderControlProps { onLoad: () => void; onClear: () => void; } function HeaderControl({ onLoad, onClear }: HeaderControlProps) { return ( <> assign as many or as few players as you want and don't forget to{" "} submit ๐พ when you're done :) > ); } export default function Rankings() { const { user, teams, players } = useSession(); const tabs = [ { id: "Chemistry", label: "๐งช Chemistry" }, { id: "Type", label: "๐ Type" }, { id: "MVP", label: "๐ MVP" }, ]; return ( <> {user && teams && players ? ( ) : ( )} > ); }