diff --git a/core/cmd.go b/core/cmd.go index 71c95738..a1466a02 100644 --- a/core/cmd.go +++ b/core/cmd.go @@ -153,9 +153,21 @@ func (ch *CommandHandler) FetchPlayer(code string) (*model.Player, error) { return player, err } +func (ch *CommandHandler) SearchPlayers(query string) ([]model.Player, error) { + players, err := ch.tracker.SearchPlayers(ch.ctx, query) + if err != nil { + log.Println(err) + if !errorsx.ContainsTrackingError(err) { + err = errorsx.NewError(http.StatusNotFound, fmt.Errorf(`failed to search players %w`, err)) + } + } + return players, err +} + func (ch *CommandHandler) GetThemeList() ([]string, error) { files, err := ioutil.ReadDir(`themes`) if err != nil { + log.Println(err) return nil, errorsx.NewError(http.StatusInternalServerError, errors.New("failed to read themes directory")) } diff --git a/core/model/player.go b/core/model/player.go index a21ec927..a3621963 100644 --- a/core/model/player.go +++ b/core/model/player.go @@ -1,7 +1,7 @@ package model type Player struct { - DisplayName string - Code string - FavoriteCharacter string + DisplayName string `json:"displayName"` + Code string `json:"code"` + FavoriteCharacter string `json:"favoriteCharacter"` } diff --git a/gui/src/pages/tracking/tracking-form.tsx b/gui/src/pages/tracking/tracking-form.tsx index e68c50db..8b0117d5 100644 --- a/gui/src/pages/tracking/tracking-form.tsx +++ b/gui/src/pages/tracking/tracking-form.tsx @@ -5,23 +5,56 @@ import { motion } from "framer-motion"; import { Icon } from "@iconify/react"; import { TrackingMachineContext } from "@/machines/tracking-machine"; +import { FetchPlayer, GetUsers } from "@@/go/core/CommandHandler"; + import { ActionButton } from "@/ui/action-button"; import { Checkbox } from "@/ui/checkbox"; -import { model } from "@@/go/models"; +import { errorsx, model } from "@@/go/models"; import { PageHeader } from "@/ui/page-header"; export const TrackingForm: React.FC = () => { const { t } = useTranslation(); const users = useLoaderData() as model.User[] const trackingActor = TrackingMachineContext.useActorRef() - + const playerIdInputRef = React.useRef(null); const restoreRef = React.useRef(null); const [playerIdInput, setPlayerIdInput] = React.useState(""); - const onSubmit: React.FormEventHandler = (e) => { + const onSubmit: React.FormEventHandler = async (e) => { e.preventDefault(); if (playerIdInput == "") return; + + try { + const player = await FetchPlayer(playerIdInput); + console.log(`Player found ${player.displayName}`); + trackingActor.send({ + type: "submit", + user: { + displayName: player.displayName, + code: player.code, + }, + restore: restoreRef.current && restoreRef.current.checked, + }); + } catch (err) { + if (err.code && err.code == 404) { + console.error(`Player not found ${playerIdInput}`); + + try { + const players = await SearchPlayers(playerIdInput); + } catch (err) { + console.error(err); + } + + + return; + } + + console.error(err); + } + + + return; trackingActor.send({ type: "submit", user: { @@ -68,10 +101,10 @@ export const TrackingForm: React.FC = () => { autoSave="off" /> {playerIdInput.length > 0 && ( -