Commit 59e7ea51 authored by Yannick Li's avatar Yannick Li
Browse files

Add form to manage different grids

parent 1de49d9d
......@@ -27,6 +27,7 @@ import assert from 'assert';
import Cell from './Cell';
import { validInput } from './Cell';
import { client } from '@concordant/c-client';
import Submit1Input from './Submit1Input';
/**
* Interface for the properties of the Grid
......@@ -40,6 +41,7 @@ interface IGridProps {
* Interface for the state of the Grid
*/
interface IGridState {
gridNum: string,
mvmap: any,
cells: {value: string, modifiable: boolean, error: boolean}[],
isConnected: boolean,
......@@ -57,8 +59,10 @@ class Grid extends React.Component<IGridProps, IGridState> {
super(props);
let cells = new Array(81).fill(null).map(()=>({value:"", modifiable:false, error:false}));
this.modifiedCells = new Array(81).fill(null);
let mvmap = this.props.collection.open("room", "MVMap", false, function () {return});
let gridNum = "1";
let mvmap = this.props.collection.open("grid" + gridNum, "MVMap", false, function () {return});
this.state = {
gridNum: gridNum,
mvmap: mvmap,
cells: cells,
isConnected: true,
......@@ -189,6 +193,16 @@ class Grid extends React.Component<IGridProps, IGridState> {
}
}
/**
* This handler is called when a new grid number is submit.
* @param gridNum Desired grid number.
*/
handleSubmit(gridNum: string) {
let mvmap = this.props.collection.open("grid" + gridNum, "MVMap", false, function () {return});
this.setState({gridNum: gridNum, mvmap: mvmap});
this.initFrom(generateStaticGrid());
}
/**
* This function return a React element corresponding to a cell.
* @param index The index of the cell to render.
......@@ -227,6 +241,8 @@ class Grid extends React.Component<IGridProps, IGridState> {
render() {
return (
<div className="sudoku">
<div>Current grid : {this.state.gridNum}</div>
<Submit1Input inputName="Grid" onSubmit={this.handleSubmit.bind(this)} /><br />
<div><button onClick={this.reset.bind(this)}>Reset</button></div><br />
<div><button onClick={() => this.switchConnection()}>{this.state.isConnected ? "Disconnect" : "Connect"}</button></div><br />
<table className="grid">
......
/**
* MIT License
*
* Copyright (c) 2020, Concordant and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import React from 'react';
interface ISubmit1InputProps {
onSubmit: any,
inputName: string
}
interface ISubmit1InputState {
value: string
}
class Submit1Input extends React.Component<ISubmit1InputProps, ISubmit1InputState> {
constructor(props: any) {
super(props);
this.state = {value: ""};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
/**
* onChange event handler
* @param event handled
*/
handleChange(event: any) {
this.setState({value: event.target.value});
}
handleSubmit(event: any) {
event.preventDefault();
this.props.onSubmit(this.state.value);
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
{this.props.inputName} :
<input value={this.state.value} onChange={this.handleChange} />
</label>
<input type="submit" value={"Change " + this.props.inputName} />
</form>
);
}
}
export default Submit1Input
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment