|
|
@ -27,9 +27,12 @@ const ERROR_STYLE: CSSProperties = {
|
|
|
|
borderColor: "red",
|
|
|
|
borderColor: "red",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const GUEST_MODE_CONTENT_STORAGE_KEY = "guest_mode_content"
|
|
|
|
|
|
|
|
const GUEST_MODE_TITLE_STORAGE_KEY = "guest_mode_title"
|
|
|
|
|
|
|
|
|
|
|
|
export interface EditorViewProps {
|
|
|
|
export interface EditorViewProps {
|
|
|
|
tactic: Tactic
|
|
|
|
tactic: Tactic
|
|
|
|
onContentChange: (tactic: TacticContent) => Promise<boolean>
|
|
|
|
onContentChange: (tactic: TacticContent) => Promise<SaveState>
|
|
|
|
onNameChange: (name: string) => Promise<boolean>
|
|
|
|
onNameChange: (name: string) => Promise<boolean>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -50,31 +53,55 @@ export default function Editor({
|
|
|
|
name: string
|
|
|
|
name: string
|
|
|
|
content: string
|
|
|
|
content: string
|
|
|
|
}) {
|
|
|
|
}) {
|
|
|
|
|
|
|
|
const isInGuestMode = id == -1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const storage_content = localStorage.getItem(GUEST_MODE_CONTENT_STORAGE_KEY)
|
|
|
|
|
|
|
|
const editorContent =
|
|
|
|
|
|
|
|
isInGuestMode && storage_content != null ? storage_content : content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const storage_name = localStorage.getItem(GUEST_MODE_TITLE_STORAGE_KEY)
|
|
|
|
|
|
|
|
const editorName = isInGuestMode && storage_name != null ? storage_name : name
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<EditorView
|
|
|
|
<EditorView
|
|
|
|
tactic={{ name, id, content: JSON.parse(content) }}
|
|
|
|
tactic={{ name: editorName, id, content: JSON.parse(editorContent) }}
|
|
|
|
onContentChange={(content: TacticContent) =>
|
|
|
|
onContentChange={async (content: TacticContent) => {
|
|
|
|
fetchAPI(`tactic/${id}/save`, { content }).then((r) => r.ok)
|
|
|
|
if (isInGuestMode) {
|
|
|
|
|
|
|
|
localStorage.setItem(
|
|
|
|
|
|
|
|
GUEST_MODE_CONTENT_STORAGE_KEY,
|
|
|
|
|
|
|
|
JSON.stringify(content),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
return SaveStates.Guest
|
|
|
|
}
|
|
|
|
}
|
|
|
|
onNameChange={(name: string) =>
|
|
|
|
return fetchAPI(`tactic/${id}/save`, { content }).then((r) =>
|
|
|
|
fetchAPI(`tactic/${id}/edit/name`, { name }).then((r) => r.ok)
|
|
|
|
r.ok ? SaveStates.Ok : SaveStates.Err,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
onNameChange={async (name: string) => {
|
|
|
|
|
|
|
|
if (isInGuestMode) {
|
|
|
|
|
|
|
|
localStorage.setItem(GUEST_MODE_TITLE_STORAGE_KEY, name)
|
|
|
|
|
|
|
|
return true //simulate that the name has been changed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fetchAPI(`tactic/${id}/edit/name`, { name }).then(
|
|
|
|
|
|
|
|
(r) => r.ok,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function EditorView({
|
|
|
|
function EditorView({
|
|
|
|
tactic: { name, content: initialContent },
|
|
|
|
tactic: { id, name, content: initialContent },
|
|
|
|
onContentChange,
|
|
|
|
onContentChange,
|
|
|
|
onNameChange,
|
|
|
|
onNameChange,
|
|
|
|
}: EditorViewProps) {
|
|
|
|
}: EditorViewProps) {
|
|
|
|
|
|
|
|
const isInGuestMode = id == -1
|
|
|
|
|
|
|
|
|
|
|
|
const [style, setStyle] = useState<CSSProperties>({})
|
|
|
|
const [style, setStyle] = useState<CSSProperties>({})
|
|
|
|
const [content, setContent, saveState] = useContentState(
|
|
|
|
const [content, setContent, saveState] = useContentState(
|
|
|
|
initialContent,
|
|
|
|
initialContent,
|
|
|
|
(content) =>
|
|
|
|
isInGuestMode ? SaveStates.Guest : SaveStates.Ok,
|
|
|
|
onContentChange(content).then((success) =>
|
|
|
|
onContentChange,
|
|
|
|
success ? SaveStates.Ok : SaveStates.Err,
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
const [allies, setAllies] = useState(
|
|
|
|
const [allies, setAllies] = useState(
|
|
|
|
getRackPlayers(Team.Allies, content.players),
|
|
|
|
getRackPlayers(Team.Allies, content.players),
|
|
|
@ -220,12 +247,14 @@ function getRackPlayers(team: Team, players: Player[]): RackedPlayer[] {
|
|
|
|
|
|
|
|
|
|
|
|
function useContentState<S>(
|
|
|
|
function useContentState<S>(
|
|
|
|
initialContent: S,
|
|
|
|
initialContent: S,
|
|
|
|
|
|
|
|
initialSaveState: SaveState,
|
|
|
|
saveStateCallback: (s: S) => Promise<SaveState>,
|
|
|
|
saveStateCallback: (s: S) => Promise<SaveState>,
|
|
|
|
): [S, Dispatch<SetStateAction<S>>, SaveState] {
|
|
|
|
): [S, Dispatch<SetStateAction<S>>, SaveState] {
|
|
|
|
const [content, setContent] = useState(initialContent)
|
|
|
|
const [content, setContent] = useState(initialContent)
|
|
|
|
const [savingState, setSavingState] = useState(SaveStates.Ok)
|
|
|
|
const [savingState, setSavingState] = useState(initialSaveState)
|
|
|
|
|
|
|
|
|
|
|
|
const setContentSynced = useCallback((newState: SetStateAction<S>) => {
|
|
|
|
const setContentSynced = useCallback(
|
|
|
|
|
|
|
|
(newState: SetStateAction<S>) => {
|
|
|
|
setContent((content) => {
|
|
|
|
setContent((content) => {
|
|
|
|
const state =
|
|
|
|
const state =
|
|
|
|
typeof newState === "function"
|
|
|
|
typeof newState === "function"
|
|
|
@ -239,7 +268,9 @@ function useContentState<S>(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return state
|
|
|
|
return state
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}, [saveStateCallback])
|
|
|
|
},
|
|
|
|
|
|
|
|
[saveStateCallback],
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
return [content, setContentSynced, savingState]
|
|
|
|
return [content, setContentSynced, savingState]
|
|
|
|
}
|
|
|
|
}
|
|
|
|