~vpzom/bracketmonster

9b8e121071855740c0e56678d78bba3d1fa8c700 — Colin Reeder 5 months ago 4a3c73d
formSubmit for newBracket
2 files changed, 72 insertions(+), 7 deletions(-)

M rosebush/src/client/pages/newBracket.tsx
M rosebush/src/server/index.tsx
M rosebush/src/client/pages/newBracket.tsx => rosebush/src/client/pages/newBracket.tsx +4 -3
@@ 68,7 68,8 @@ class PageContent extends Component<Props, State> {

			return <Fragment>
				<h1><Text id="newBracketPage" /></h1>
				<form onSubmit={this.submit.bind(this)}>
				<form onSubmit={this.submit.bind(this)} method="POST" action="/formSubmit/newBracket">
					{props.app.userInfo.value === null && <input type="hidden" name="newUser" value="true" />}
					<p>
						<label>
							<Text id="name" />: <input type="text" name="bracketName" />


@@ 87,7 88,7 @@ class PageContent extends Component<Props, State> {
					</p>
					<p>
						<label>
							<input type="checkbox" checked={state.useScores} onChange={this.onChangeUseScores} />
							<input type="checkbox" checked={state.useScores} onChange={this.onChangeUseScores} name="use_scores" />
							{" "}<Text id="useScoring" />
						</label>
					</p>


@@ 99,7 100,7 @@ class PageContent extends Component<Props, State> {
					</p>
					<p>
						<label>
							<input type="checkbox" checked={state.shufflePlayers} onChange={this.onChangeShufflePlayers} />
							<input type="checkbox" checked={state.shufflePlayers} onChange={this.onChangeShufflePlayers} name="shufflePlayers" />
							{" "}<Text id="shufflePlayers" />
						</label>
					</p>

M rosebush/src/server/index.tsx => rosebush/src/server/index.tsx +68 -4
@@ 5,7 5,7 @@ import * as http from "http";
import * as httpProxy from "http-proxy";
import isomorphicCookie from "isomorphic-cookie";
import jsesc from "jsesc";
import fetch, { RequestInit } from "node-fetch";
import fetch, { HeadersInit, RequestInit } from "node-fetch";
import * as path from "path";
import { Fragment, JSX, h } from "preact";
import { Dictionary, IntlProvider } from "preact-jsx-i18n";


@@ 13,6 13,7 @@ import { render } from "preact-render-to-string";
import { Route, RouteProps, Router } from "preact-router";
import * as serveStatic from "serve-static";
import * as querystring from "querystring";
import { shuffle } from "@pacote/shuffle";

import App, { UserInfo } from "../client/App";
import { COOKIE_AGE } from "../client/constants";


@@ 72,10 73,11 @@ apiServer.on("proxyReq", function(proxyReq, req) {
function apiFetchRaw(srcReq: http.IncomingMessage, url: string, init?: RequestInit): ReturnType<typeof fetch> {
	const token = isomorphicCookie.load("rosebushToken", srcReq);

	const headers: {[key: string]: string} = {};
	const headers: HeadersInit = (init && init.headers) ? init.headers : {};

	if(token) {
		headers["Authorization"] = "Bearer " + token;
	if(token && !("Authorization" in headers)) {
		// probably shouldn't cast this, but it's fine for now
		(headers as any).Authorization = "Bearer " + token;
	}

	return fetch(API_HOST + "/" + url, {...init, headers});


@@ 188,6 190,68 @@ http.createServer(async (req_, res) => {
				res.end();
			}
		}
		else if(rest === "newBracket") {
			try {
				const fields = querystring.decode(await getStream(req));
				let newUserToken = null;
				const newHeaders: {[key: string]: string} = {};
				if(fields.newUser) {
					const resp = await apiFetch(req, "v1/users", {method: "POST"})
						.then(resp => resp.json());

					newUserToken = resp.token;
				}

				if(newUserToken !== null) {
					newHeaders.Authorization = "Bearer " + newUserToken;
				}	

				let players = String(fields.playerList).split("\n");

				if(fields.shufflePlayers) {
					players = shuffle(players);
				}

				const resp = await apiFetchRaw(req, "v1/brackets", {method: "POST", body: JSON.stringify({
					name: fields.bracketName,
					players,
					type: fields.bracketType,
					use_scores: !!fields.use_scores,
				}), headers: newHeaders});

				if(resp.status < 200 || resp.status >= 300) {
					const text = await resp.text();
					res.writeHead(resp.status);
					res.write(text);
					res.end();
					return;
				}

				const result = await resp.json();

				const newID = result.id;

				const outHeaders: {[key: string]: string} = {
					Location: "/bracket/" + newID,
				};

				if(newUserToken !== null) {
					outHeaders["Set-Cookie"] = "rosebushToken=" + newUserToken + "; Path=/; Max-Age=" + COOKIE_AGE;
				}

				res.writeHead(
					303,
					outHeaders,
				);
				res.write("Created bracket " + newID);
				res.end();
			} catch(e) {
				console.error(e);
				res.writeHead(500);
				res.write("Internal Server Error");
				res.end();
			}
		}
		else {
			res.writeHead(404);
			res.write("Not Found");