+class CategoryRequest extends React.PureComponent {
+ constructor(props) {
+ super(props);
+ this.category = React.createRef();
+
+ this.handle_change = this.handle_change.bind(this);
+ this.handle_submit = this.handle_submit.bind(this);
+ }
+
+ handle_change(event) {
+ const category_input = this.category.current;
+ const category = category_input.value;
+
+ const match = category.match(/[0-9]+/);
+ if (match) {
+ const num_items = parseInt(match[0], 10);
+ if (num_items <= MAX_PROMPT_ITEMS)
+ category_input.setCustomValidity("");
+ }
+ }
+
+ async handle_submit(event) {
+ const form = event.currentTarget;
+ const category_input = this.category.current;
+ const category = category_input.value;
+
+ /* Prevent the default page-changing form-submission behavior. */
+ event.preventDefault();
+
+ const match = category.match(/[0-9]+/);
+ if (match === null) {
+ category_input.setCustomValidity("Category must include a number");
+ form.reportValidity();
+ return;
+ }
+
+ const num_items = parseInt(match[0], 10);
+
+ if (num_items > MAX_PROMPT_ITEMS) {
+ category_input.setCustomValidity(`Maximum number of items is ${MAX_PROMPT_ITEMS}`);
+ form.reportValidity();
+ return;
+ }
+
+ const response = await fetch_post_json("prompts", {
+ items: num_items,
+ prompt: category
+ });
+
+ if (response.status === 200) {
+ const result = await response.json();
+ if (! result.valid) {
+ add_message("danger", result.message);
+ return;
+ }
+ } else {
+ add_message("danger", "An error occurred submitting your category");
+ }
+
+ form.reset();
+ }
+
+ render() {
+ return (
+ <div className="category-request">
+ <h2>Submit a Category</h2>
+ <p>
+ Suggest a category to play. Don't forget to include the
+ number of items for each person to submit.
+ </p>
+
+ <form onSubmit={this.handle_submit} >
+ <div className="form-field large">
+ <input
+ type="text"
+ id="category"
+ placeholder="6 things at the beach"
+ required
+ autoComplete="off"
+ onChange={this.handle_change}
+ ref={this.category}
+ />
+ </div>
+
+ <div className="form-field large">
+ <button type="submit">
+ Send
+ </button>
+ </div>
+
+ </form>
+ </div>
+ );
+ }
+}
+
+const PromptOptions = React.memo(props => {
+
+ if (props.prompts.length === 0)
+ return null;
+
+ return (
+ <div className="prompt-options">
+ <h2>Vote on Categories</h2>
+ <p>
+ Select any categories below that you'd like to play.
+ You can choose as many as you'd like.
+ </p>
+ {props.prompts.map(p => {
+ return (
+ <button
+ className="vote-button"
+ key={p.id}
+ onClick={() => fetch_post_json(`vote/${p.id}`) }
+ >
+ {p.prompt}
+ <div className="vote-choices">
+ {p.votes.map(v => {
+ return (
+ <div
+ key={v}
+ className="vote-choice"
+ >
+ {v}
+ </div>
+ );
+ })}
+ </div>
+ </button>
+ );
+ })}
+ </div>
+ );
+});
+
+const LetsPlay = React.memo(props => {
+
+ const quorum = Math.round((props.num_players + 1) / 2);
+ const max_votes = props.prompts.reduce(
+ (max_so_far, v) => Math.max(max_so_far, v.votes.length), 0);
+
+ if (max_votes < quorum)
+ return null;
+
+ const candidates = props.prompts.filter(p => p.votes.length >= quorum);
+ const index = Math.floor(Math.random() * candidates.length);
+ const winner = candidates[index];
+