import React from "react"; import Store from "./interface/Store"; import * as Act from "./interface/Actions"; import addE from "./Images/addE.png" import addD from "./Images/addD.png" import del from "./Images/minus.png" export default class NewGroup extends React.Component { constructor() { super() this.state = { usrs: Store.getUsers(), grpnme: '', team: [], coin: 1, usrinx: -1, msg: '' } this.getUsers = this.getUsers.bind(this) this.color = ['grey', 'red', 'green', 'blue'] } componentDidMount() { Store.on("users", this.getUsers) Act.apiCall('service', 'USRLST', {}); } componentWillUnmount() { Store.removeListener("users", this.getUsers) } getUsers() { this.setState({ usrs: Store.getUsers(), usrinx: 0 }) } addUser() { let pers = this.state.usrs[this.state.usrinx] let team = this.state.team team.push({ tag: pers.tag, nme: pers.desc, coin: this.state.coin }) this.setState({ team: team }) } newGroup() { Act.apiCall('service', 'GRPNEW', { desc: this.state.grpnme, group: this.state.team }); this.setState({ grpnme: '', team: [], usrinx: 0 }) } teamOff(inx, e) { let tm = this.state.team tm.splice(inx, 1) this.setState({ team: tm }) } check(me) { let msg = [] let i = this.state.team.length if (i === 0) return ['create group'] let cn = [...new Set(this.state.team.map((ent) => ent.coin))]; let nm = [...new Set(this.state.team.map((ent) => ent.tag))]; if (this.state.grpnme.length < 4) msg.push('group name required') if (nm.indexOf(me) === -1) msg.push('must be in list') if ([1, 5, 7, 9, 11].indexOf(i) > -1) msg.push('uneven players') if (i !== nm.length) msg.push('duplicate player') if (i === 2 && cn.length !== 2) msg.push('different tokens') if (i > 2 && i % cn.length !== 0) msg.push('token mismatch') return msg } render() { let me = '' let usrlst = this.state.usrs.map((u, i) => { if (u.me === 1) me = u.tag return }) let team = this.state.team ? this.state.team.map((d, i) => { return
{d.nme}
}) :
no memebers
let msg = this.check(me); return (
group name
this.setState({ grpnme: e.target.value })} />
select token
this.setState({ coin: 1 })}>
this.setState({ coin: 2 })} >
this.setState({ coin: 3 })} >
{team}
{msg.length === 0 ? : }
{msg.map((m) => { return
{m}
})}
) } }