const preact = require('preact'); const { Component } = require('preact'); const { connect } = require('preact-redux'); const linkState = require('linkstate').default; const actions = require('./actions'); const { postData, errorToast } = require('./../../client/src/utils'); const AcpGameList = require('./acp.game.list'); const AcpUser = require('./acp.user'); const addState = connect( function receiveState(state) { const { account, user, msg, } = state; return { account, user, msg, }; }, function receiveDispatch(dispatch) { function setUser(user) { dispatch(actions.setUser(user)); } function setGames(list) { dispatch(actions.setGames(list)); } function setMsg(msg) { dispatch(actions.setMsg(msg)); } return { setUser, setMsg, setGames, }; } ); class AcpMain extends Component { constructor(props) { super(props); this.state = { id: null, name: null, }; } render(args, state) { const { setGames, setUser, setMsg, msg, } = args; const { name, id, } = state; const reset = () => { setMsg(null); this.setState({ id: null, name: null }); }; const getUser = () => { reset(); postData('/acp/user', { id, name }) .then(res => res.json()) .then(data => { if (data.error) return setMsg(data.error); setUser(data); }) .catch(error => errorToast(error)); }; const gameList = () => { reset(); postData('/acp/game/list', { number: 20 }) .then(res => res.json()) .then(data => { if (data.error) return setMsg(data.error); console.log(data); setGames(data.data); }) .catch(error => errorToast(error)); }; const gameOpen = () => { reset(); postData('/acp/game/open') .then(res => res.json()) .then(data => { if (data.error) return setMsg(data.error); console.log(data); setGames(data); }) .catch(error => errorToast(error)); }; return (
{msg}
); } } module.exports = addState(AcpMain);