Skip to content

Commit

Permalink
Added search players function to cmd handler. Added json tags to play…
Browse files Browse the repository at this point in the history
…er model. Test code in tracking form.
  • Loading branch information
tifye committed Dec 22, 2023
1 parent 637fae0 commit f376d6d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
12 changes: 12 additions & 0 deletions core/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand Down
6 changes: 3 additions & 3 deletions core/model/player.go
Original file line number Diff line number Diff line change
@@ -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"`
}
45 changes: 39 additions & 6 deletions gui/src/pages/tracking/tracking-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLInputElement>(null);
const restoreRef = React.useRef<HTMLInputElement>(null);
const [playerIdInput, setPlayerIdInput] = React.useState<string>("");

const onSubmit: React.FormEventHandler<HTMLFormElement> = (e) => {
const onSubmit: React.FormEventHandler<HTMLFormElement> = 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: {
Expand Down Expand Up @@ -68,10 +101,10 @@ export const TrackingForm: React.FC = () => {
autoSave="off"
/>
{playerIdInput.length > 0 && (
<button
type="button"
<button
type="button"
onClick={clearInput}
aria-label="Clear"
aria-label="Clear"
className='absolute top-0 right-0 mt-4 mr-4 text-[#bfbcff] hover:text-white hover:bg-[rgba(255,255,255,.11)] transition-colors rounded-md'
>
<Icon icon="ci:close-big" className='w-6 h-6' />
Expand Down

0 comments on commit f376d6d

Please sign in to comment.