a mighty battle against an evil opponent

This commit is contained in:
ntr
2019-06-15 00:00:29 +10:00
parent 5c6e587044
commit 66462ac670
6 changed files with 198 additions and 104 deletions
+34 -5
View File
@@ -3,16 +3,45 @@ const preact = require('preact');
const { Component } = require('preact')
const { connect } = require('preact-redux');
const SERVER = process.env.NODE_ENV === 'production' ? '/' : 'http://localhost:40000';
function postData(url = '/', data = {}) {
// Default options are marked with *
return fetch(url, {
method: "POST", // *GET, POST, PUT, DELETE, etc.
// mode: "no-cors", // no-cors, cors, *same-origin
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
credentials: "include", // include, same-origin, *omit
headers: {
'Accept': 'application/json',
'content-type': 'application/json'
},
redirect: "error", // manual, *follow, error
// referrer: "", // no-referrer, *client
body: JSON.stringify(data), // body data type must match "Content-Type" header
})
.then(response => response.json()); // parses response to JSON
}
const addState = connect(
(state) => {
const { ws, account } = state;
null,
(dispatch) => {
function submitLogin(name, password) {
return ws.sendAccountLogin(name, password);
postData(`${SERVER}/login`, { name, password })
.then(data => console.log(JSON.stringify(data)))
.catch(error => console.error(error));
}
function submitRegister(name, password, code) {
return ws.sendAccountCreate(name, password, code);
postData(`${SERVER}/register`, { name, password, code })
.then(data => console.log(JSON.stringify(data)))
.catch(error => console.error(error));
}
return {
submitLogin,
submitRegister,
}
return { account, submitLogin, submitRegister };
},
);