|
|
|
@ -1,18 +1,26 @@
|
|
|
|
|
import { CSSProperties, useRef, useState } from "react"
|
|
|
|
|
import "../style/editor.css"
|
|
|
|
|
import TitleInput from "../components/TitleInput"
|
|
|
|
|
import { API } from "../Constants"
|
|
|
|
|
import { BasketCourt } from "../components/editor/BasketCourt"
|
|
|
|
|
import {CSSProperties, useEffect, useRef, useState} from "react";
|
|
|
|
|
import "../style/editor.css";
|
|
|
|
|
import TitleInput from "../components/TitleInput";
|
|
|
|
|
import {BasketCourt} from "../components/editor/BasketCourt";
|
|
|
|
|
|
|
|
|
|
import { Rack } from "../components/Rack"
|
|
|
|
|
import { PlayerPiece } from "../components/editor/PlayerPiece"
|
|
|
|
|
import { Player } from "../data/Player"
|
|
|
|
|
import { Team } from "../data/Team"
|
|
|
|
|
import {Rack} from "../components/Rack";
|
|
|
|
|
import {PlayerPiece} from "../components/editor/PlayerPiece";
|
|
|
|
|
|
|
|
|
|
import {Player} from "../tactic/Player";
|
|
|
|
|
import {Tactic, TacticContent} from "../tactic/Tactic";
|
|
|
|
|
import {fetchAPI} from "../Fetcher";
|
|
|
|
|
import {Team} from "../tactic/Team";
|
|
|
|
|
|
|
|
|
|
const ERROR_STYLE: CSSProperties = {
|
|
|
|
|
borderColor: "red",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface EditorViewProps {
|
|
|
|
|
tactic: Tactic,
|
|
|
|
|
onContentChange: (tactic: TacticContent) => Promise<boolean>,
|
|
|
|
|
onNameChange: (name: string) => Promise<boolean>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* information about a player that is into a rack
|
|
|
|
|
*/
|
|
|
|
@ -21,8 +29,21 @@ interface RackedPlayer {
|
|
|
|
|
key: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function Editor({ id, name }: { id: number; name: string }) {
|
|
|
|
|
const [style, setStyle] = useState<CSSProperties>({})
|
|
|
|
|
export default function Editor({tactic}: { tactic: Tactic }) {
|
|
|
|
|
return <EditorView tactic={tactic}
|
|
|
|
|
onContentChange={(content: TacticContent) => (
|
|
|
|
|
fetchAPI(`tactic/${tactic.id}/save`, {content})
|
|
|
|
|
.then((r) => r.ok)
|
|
|
|
|
)}
|
|
|
|
|
onNameChange={(name: string) => (
|
|
|
|
|
fetchAPI(`tactic/${tactic.id}/edit/name`, {name})
|
|
|
|
|
.then((r) => r.ok)
|
|
|
|
|
)}/>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function EditorView({tactic: {name, content}, onContentChange, onNameChange}: EditorViewProps) {
|
|
|
|
|
const [style, setStyle] = useState<CSSProperties>({});
|
|
|
|
|
|
|
|
|
|
const positions = ["1", "2", "3", "4", "5"]
|
|
|
|
|
const [allies, setAllies] = useState(
|
|
|
|
|
positions.map((key) => ({ team: Team.Allies, key })),
|
|
|
|
@ -31,8 +52,17 @@ export default function Editor({ id, name }: { id: number; name: string }) {
|
|
|
|
|
positions.map((key) => ({ team: Team.Opponents, key })),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const [players, setPlayers] = useState<Player[]>([])
|
|
|
|
|
const courtDivContentRef = useRef<HTMLDivElement>(null)
|
|
|
|
|
|
|
|
|
|
const [players, setPlayers] = useState<Player[]>(content.players);
|
|
|
|
|
const courtDivContentRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
onContentChange({players})
|
|
|
|
|
.then(success => {
|
|
|
|
|
if (!success)
|
|
|
|
|
alert("error when saving changes.")
|
|
|
|
|
})
|
|
|
|
|
}, [players])
|
|
|
|
|
|
|
|
|
|
const canDetach = (ref: HTMLDivElement) => {
|
|
|
|
|
const refBounds = ref.getBoundingClientRect()
|
|
|
|
@ -75,28 +105,15 @@ export default function Editor({ id, name }: { id: number; name: string }) {
|
|
|
|
|
<div id="main-div">
|
|
|
|
|
<div id="topbar-div">
|
|
|
|
|
<div>LEFT</div>
|
|
|
|
|
<TitleInput
|
|
|
|
|
style={style}
|
|
|
|
|
default_value={name}
|
|
|
|
|
on_validated={(new_name) => {
|
|
|
|
|
fetch(`${API}/tactic/${id}/edit/name`, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
Accept: "application/json",
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
name: new_name,
|
|
|
|
|
}),
|
|
|
|
|
}).then((response) => {
|
|
|
|
|
if (response.ok) {
|
|
|
|
|
<TitleInput style={style} default_value={name} on_validated={new_name => {
|
|
|
|
|
onNameChange(new_name).then(success => {
|
|
|
|
|
if (success) {
|
|
|
|
|
setStyle({})
|
|
|
|
|
} else {
|
|
|
|
|
setStyle(ERROR_STYLE)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
}}/>
|
|
|
|
|
<div>RIGHT</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div id="edit-div">
|
|
|
|
@ -126,6 +143,12 @@ export default function Editor({ id, name }: { id: number; name: string }) {
|
|
|
|
|
<div id="court-div-bounds" ref={courtDivContentRef}>
|
|
|
|
|
<BasketCourt
|
|
|
|
|
players={players}
|
|
|
|
|
onPlayerChange={(player) => {
|
|
|
|
|
setPlayers(players => {
|
|
|
|
|
const idx = players.indexOf(player)
|
|
|
|
|
return players.toSpliced(idx, 1, player)
|
|
|
|
|
})
|
|
|
|
|
}}
|
|
|
|
|
onPlayerRemove={(player) => {
|
|
|
|
|
setPlayers((players) => {
|
|
|
|
|
const idx = players.indexOf(player)
|
|
|
|
|