From fbd247880d7fa0d04aecb61c8960876a3c7a6033 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Sun, 14 Jun 2020 15:14:44 -0700 Subject: [PATCH 1/1] Fix to use server-sent state for whether player has answered/judged Previously we were using a component state Boolean "submitted" for this information, but that's fundamentally broken since if the client reloads the page that state is lost, resulting in the user being presented with the form as to submit a second time (which is not allowed by the game). So, now the rendering looks to see whether the current player's name is in the list of submitted players, and if so, it does not display the form. This is exactly what we want. --- empathy/empathy.jsx | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/empathy/empathy.jsx b/empathy/empathy.jsx index 566ad10..8006942 100644 --- a/empathy/empathy.jsx +++ b/empathy/empathy.jsx @@ -402,14 +402,19 @@ class Ambiguities extends React.PureComponent { this.state = { word_sets: word_sets, - submitted: false, selected: null }; + this.submitted = false; this.judging_sent_recently = false; } async handle_submit() { + + /* Don't submit a second time. */ + if (this.submitted) + return; + const response = await fetch_post_json( `judged/${this.props.prompt.id}`,{ word_groups: this.state.word_sets.map(set => Array.from(set)) @@ -427,9 +432,7 @@ class Ambiguities extends React.PureComponent { return; } - this.setState({ - submitted: true - }); + this.submitted = true; } handle_click(word) { @@ -498,7 +501,7 @@ class Ambiguities extends React.PureComponent { } render() { - if (this.state.submitted) + if (this.props.players_judged.has(this.props.player.name)) { return (

Submission received

@@ -543,6 +546,7 @@ class Ambiguities extends React.PureComponent {
); + } const btn_class = "ambiguity-button"; const btn_selected_class = btn_class + " selected"; @@ -596,9 +600,7 @@ class ActivePrompt extends React.PureComponent { super(props); const items = props.prompt.items; - this.state = { - submitted: false - }; + this.submitted = false; this.answers = [...Array(items)].map(() => React.createRef()); this.answering_sent_recently = false; @@ -630,6 +632,10 @@ class ActivePrompt extends React.PureComponent { /* Prevent the default page-changing form-submission behavior. */ event.preventDefault(); + /* And don't submit a second time. */ + if (this.submitted) + return; + const response = await fetch_post_json(`answer/${this.props.prompt.id}`, { answers: this.answers.map(r => r.current.value) }); @@ -646,13 +652,11 @@ class ActivePrompt extends React.PureComponent { /* Everything worked. Server is happy with our answers. */ form.reset(); - this.setState({ - submitted: true - }); + this.submitted = true; } render() { - if (this.state.submitted) + if (this.props.players_answered.has(this.props.player.name)) { return (

Submission received

@@ -697,6 +701,7 @@ class ActivePrompt extends React.PureComponent {
); + } return (
@@ -983,6 +988,7 @@ class Game extends React.PureComponent { return