Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added test tab and back end connection #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"react-redux": "^7.2.0",
"react-scripts": "3.4.1",
"react-tabs": "^3.1.0",
"redux": "^4.0.5"
"redux": "^4.0.5",
"xmlhttprequest": "^1.8.0"
},
"scripts": {
"start": "react-scripts start",
Expand Down
168 changes: 157 additions & 11 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import SpellsTab from './components/tabs/spells.js'
import InventoryTab from './components/tabs/inventory.js'
import LevelsTab from './components/tabs/levels.js'


export default class App extends React.Component {
constructor(props) {
super(props);
Expand All @@ -26,6 +27,9 @@ export default class App extends React.Component {
this.removeFromList = this.removeFromList.bind(this);
this.addToAvailableList = this.addToAvailableList.bind(this);

this.formSubmitHandler = this.formSubmitHandler.bind(this);


/*TODO: ALL of this is placeholder info, just to check math and logic.
* Every single value here will need to be replaced with data nabbed from the server.
* However, for right now, this will function for testing.
Expand Down Expand Up @@ -153,19 +157,22 @@ export default class App extends React.Component {
copper: 0,

cantrips: [],
availableCantrips: ['firebolt', 'prestigitation', 'ray of frost'],
availableCantrips: [],

spells: [],
availableSpells: ['shield', 'charm person', 'thunderwave'],
availableSpells: [],

feats: [],
availableFeats: ['fleet', 'great weapon master', 'warcaster'],
availableFeats: [],

items: [],
availableItems: ['shortsword', 'staff', 'bag of holding'],
availableItems: [],

classLevels: [],
availableClassLevels: ['fighter', 'sorcerer', 'monk', 'ranger', 'rogue'],
availableClassLevels: [],


Host: "http://localhost:8765",
};
}

Expand Down Expand Up @@ -596,6 +603,104 @@ export default class App extends React.Component {
}
}







getData(slug, handleGen) {

var xhr = new XMLHttpRequest();
xhr.open("GET", this.state.Host + slug);
// When doing authentication:
//xhr.withCredentials = true;
//xhr.open(method, action, true, username, password);
xhr.responseType = "json"
xhr.addEventListener("load", function(e) { handleGen(xhr, e) });
xhr.send();

}

formSubmitHandler(e) {
var that = this;
e.stopPropagation();
e.preventDefault();
var form = e.currentTarget;

var action = form.getAttribute("action");
var method = form.getAttribute("method");

var xhr = new XMLHttpRequest();
xhr.open(method, this.state.Host + action);
// When doing authentication:
//xhr.withCredentials = true;
//xhr.open(method, action, true, username, password);
xhr.responseType = "json";
var body = null;

switch (method) {
case "POST":
var body = new FormData(form);
xhr.addEventListener("load", function(e) {
if (xhr.status === 200) {
var id = xhr.response.data.id;
var id_action = `${action}/${id}`;
document.getElementById("update-race-subtype-form").setAttribute("action", id_action);
document.getElementById("delete-race-subtype-form").setAttribute("action", id_action);
that.getData(id_action, function(xhr2) {
if (xhr2.status === 200) {
document.getElementById("update-racesubtype-name").value = xhr2.response.data.name;
document.getElementById("update-racesubtype-description").value = xhr2.response.data.description;
}
else if (xhr2.status >= 400 && xhr2.status < 500){
alert(`client error: ${xhr2.response.message}`);
}
else if (xhr2.status >= 500) {
console.log(`server error: ${xhr2.response.message}`);
}
});
}
else if (xhr.status >= 400 && xhr.status < 500){
alert(`client error: ${xhr.response.message}`);
}
else if (xhr.status >= 500) {
console.log(`server error: ${xhr.response.message}`);
}
});
break;
case "PUT":
var body = new FormData(form);
xhr.addEventListener("load", function(e) {
if (xhr.status >= 200 && xhr.status < 300 ) {
alert(`successfully updated at ${action}`);
} else if (xhr.status >= 400 && xhr.status < 500){
alert(`client error: ${xhr.response.message}`);
} else if (xhr.status >= 500) {
console.log(`server error: ${xhr.response.message}`);
}
});
break;
case "DELETE":
xhr.addEventListener("load", function(e) {
if (xhr.status >= 200 && xhr.status < 300 ) {
alert(`successfully deleted at ${action}`);
} else if (xhr.status >= 400 && xhr.status < 500){
alert(`client error: ${xhr.response.message}`);
} else if (xhr.status >= 500) {
console.log(`server error: ${xhr.response.message}`);
}
});
break;

default:
console.log(`Invalid form method ${method}, expected one of \'POST\', \'PUT\', \'DELETE\'`);
return;
}

xhr.send(body);
}

render() {
return (
<Tabs>
Expand All @@ -607,6 +712,7 @@ export default class App extends React.Component {
<Tab>Spells & Feats</Tab>
<Tab>Inventory</Tab>
<Tab>Levels</Tab>
<Tab>Testing</Tab>
</TabList>

<TabPanel>
Expand All @@ -619,7 +725,7 @@ export default class App extends React.Component {
*/}

<ReactModal isOpen={this.state.account}>
<button type="button" onClick={() => {this.setState({account: true})}}>X</button>
<button type="button" onClick={() => {this.setState({account: false})}}>X</button>
<div>
<label>
<p>
Expand Down Expand Up @@ -826,11 +932,6 @@ export default class App extends React.Component {
lv7Slots={this.state.lv7SpellSlots}
lv8Slots={this.state.lv8SpellSlots}
lv9Slots={this.state.lv9SpellSlots}






charCantrips={this.state.cantrips}
availableCantrips={this.state.availableCantrips}
Expand Down Expand Up @@ -877,6 +978,51 @@ export default class App extends React.Component {
chText={this.changeText}
/>
</TabPanel>

<TabPanel>


<h1>Testing Page</h1>

<h2>Insert</h2>
<form id="insert-race-subtype-form" action="/race-subtypes" method="POST" onSubmit={this.formSubmitHandler}>
<div>
<label htmlFor="insert-racesubtype-name">Name:</label>
<input id="insert-racesubtype-name" type="text" name="name" required />
</div>
<div>
<label htmlFor="insert-racesubtype-description">Description:</label>
<textarea id="insert-racesubtype-description" name="description" required></textarea>
</div>
<div>
<button type="submit">Insert</button>
</div>

</form>

<h2>Update</h2>
<form id="update-race-subtype-form" method="PUT" onSubmit={this.formSubmitHandler}>
<div>
<label htmlFor="update-racesubtype-name">Name:</label>
<input id="update-racesubtype-name" type="text" name="name" required />
</div>
<div>
<label htmlFor="update-racesubtype-description">Description:</label>
<textarea id="update-racesubtype-description" name="description" required></textarea>
</div>
<div>
<button type="submit">Update</button>
</div>
</form>

<h2>Delete</h2>
<form id="delete-race-subtype-form" method="DELETE" onSubmit={this.formSubmitHandler}>
<div>
<button type="submit">Delete</button>
</div>
</form>

</TabPanel>
</Tabs>
);
}
Expand Down