Advertisement
Guest User

Untitled

a guest
Jul 12th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.96 KB | None | 0 0
  1. import React, {Component} from 'react';
  2. import {connect } from 'react-redux'
  3. import {fetch_games, fetch_operators} from '../actions/index'
  4. import { Button, Card, Image, Container,Grid, Segment, Header, Modal, Form,Radio,Dropdown,Message } from 'semantic-ui-react'
  5. import * as extra from '../extra.js'
  6. import axios from 'axios'
  7. import { withAlert } from "react-alert";
  8. import moment from 'moment'
  9. import {SortableContainer, SortableElement, arrayMove} from 'react-sortable-hoc';
  10. import Game from './Game'
  11.  
  12.  
  13.  
  14.  
  15.  
  16. class Games extends Component {
  17. constructor(props){
  18. super()
  19. this.state = {
  20. games: [],
  21. operators: [],
  22. modal_delete: false,
  23. modal_update: false,
  24. selected_game: '',
  25. selected_operator: '',
  26. game_id: '',
  27. game_name: '',
  28. game_type: '',
  29. err:false,
  30. sorted_games:[]
  31. }
  32.  
  33. }
  34.  
  35. componentDidMount(){
  36. this.set_data()
  37. }
  38. set_data = async () => {
  39. await this.props.fetch_games()
  40. await this.props.fetch_operators()
  41. const {games,operators} = await this.props
  42. // console.log(games)
  43. if(games.length === 0 || operators.length === 0 ) return
  44. await this.setState({
  45. games,
  46. operators
  47. })
  48.  
  49. }
  50. componentWillReceiveProps(nextProps) {
  51. // console.log(nextProps.games[1])
  52. // console.log(this.state.games)
  53.  
  54. //this.set_data()
  55. if (nextProps.games != this.state.games) {
  56. //Perform some operation
  57. this.setState({ games: nextProps.games });
  58. //this.classMethod();
  59. }
  60. }
  61. //delete games
  62. handle_delete = async (game) => {
  63. // console.log(game)
  64. await axios.delete('https://'+ window.location.hostname + `:8158/new_path/apiv2/entry/games/${game._id}`)
  65. await this.set_data()
  66. await this.setState({modal_delete:false})
  67. await this.props.alert.success(`you have successfully destroyed a game`)
  68.  
  69.  
  70. }
  71. //update games
  72. handle_update = (selected_game) => {
  73. if(isNaN(this.state.game_id) || this.state.game_type === '' || this.state.selected_game === '' || this.state.game_name === ''){
  74. //console.log('game id must be a number')
  75. this.setState({err:true})
  76.  
  77. }else {
  78. this.setState({
  79. err:false
  80. })
  81. const {operators} = this.state.operators
  82. const {game_id, game_type ,game_name } = this.state
  83. // console.log(operators)
  84. var operator_data = operators.find((op) => {
  85. return op.name === this.state.selected_operator
  86. })
  87. // console.log(operator_data)
  88. var new_game = {
  89. game_name,
  90. game_id,
  91. game_type,
  92. operator: operator_data
  93. }
  94. axios.put('https://'+ window.location.hostname + `:8158/new_path/apiv2/entry/games/${selected_game._id}` , new_game).then((res) => {
  95. this.setState({modal_update: false,game_id:'',game_type: '',err:false,selected_game:'',selected_operator: ''})
  96. this.set_data()
  97. this.props.alert.success('You have successfuly updated a game.')
  98.  
  99. })
  100. // console.log(new_game)
  101.  
  102. }
  103. // console.log(selected_game._id)
  104. // console.log(this.state.game_id)
  105. // console.log(this.state.game_type)
  106. // console.log(this.state.selected_operator)
  107.  
  108.  
  109.  
  110. }
  111. show = (selected_game) => this.setState({ modal_delete: true, selected_game })
  112. show_update_modal = (selected_game) => this.setState({ modal_update: true, selected_game })
  113. close_update_modal = () => this.setState({ modal_update: false,game_id:'',game_type: '',err:false })
  114. close = () => this.setState({ modal_delete: false })
  115. onSortEnd = ({ oldIndex, newIndex }) => {
  116. console.log('movinggggggg')
  117. console.log(this.state.games)
  118. console.log(oldIndex)
  119. console.log(newIndex)
  120. if(this.state.games === undefined || this.state.games[0] === undefined ) return
  121. console.log(this.state.games[0])
  122. var new_state = Object.assign(this.state.games[0])
  123. this.setState({
  124. sorted_games: arrayMove(new_state, oldIndex, newIndex),
  125. });
  126. console.log(this.state.sorted_games)
  127. };
  128. // render_game begining
  129. RENDER_GAMES(){
  130. var row_size = 3
  131. if(this.state.games === undefined || this.state.games[0] === undefined ) return
  132. var {games } = this.state
  133. // console.log(games)
  134. //console.log( delete games[0])
  135. //console.log(games[0])
  136. var rows = games[0].map((game, i) =>{
  137. // console.log(game.operator)
  138. var operator = game.operator.map((op) => {
  139. //console.log(op)
  140. const {created, ip_adress, name , operator_name, pass,portal_code} = op
  141. return {
  142. created,
  143. ip_adress,
  144. name,
  145. operator_name,
  146. pass,
  147. portal_code
  148. }
  149. })
  150. // console.log(operator[0].ip_adress)
  151. //console.log(operator.ip_adress)
  152. // put small image in the game card ?
  153. const SortableItem = SortableElement(() => <Game game={game} operator={operator[0]} show_update_modal={this.show_update_modal} show={this.show} />);
  154.  
  155. const SortableList = SortableContainer(() => {
  156. //console.log(i)
  157. return (
  158. <ul>
  159. <SortableItem key={`item-${i}`} index={i} />
  160. </ul>
  161. );
  162. });
  163.  
  164. return (
  165. // <Grid.Column key={game._id}>
  166. // <Card.Group>
  167. // <Card>
  168. // <Card.Content>
  169. // {/* <Image floated='right' size='mini' src='/images/avatar/large/molly.png' /> */}
  170. // <Card.Header>{game.game_name}</Card.Header>
  171. // <Card.Meta>game type: {game.game_type}</Card.Meta>
  172. // <Card.Meta>game id: {game.game_id}</Card.Meta>
  173. // <Card.Description>
  174. // {/* Operator : {JSON.stringify(game.operator)} */}
  175. // <strong>Operator:</strong>
  176. // <div><span>Operator Name: </span> {operator[0].operator_name}</div>
  177. // <div><span>IP adress:</span> {operator[0].ip_adress}</div>
  178. // <div><span>Name:</span>{operator[0].name}</div>
  179. // <div><span>Pass:</span>{operator[0].pass}</div>
  180. // <div><span>Portal code:</span>{operator[0].portal_code}</div>
  181. // <div><span>Created:</span>{moment(operator[0].created).format("MMM Do YY")}</div>
  182. // </Card.Description>
  183. // </Card.Content>
  184. // <Card.Content extra>
  185. // <div className='ui two buttons'>
  186. // <Button basic color='green' onClick={()=> this.show_update_modal(game)}>
  187. // Update
  188. // </Button>
  189. // <Button basic color='red' onClick={() => this.show(game)}>
  190. // Destroy
  191. // </Button>
  192. // </div>
  193. // </Card.Content>
  194. // </Card>
  195. // </Card.Group>
  196. // </Grid.Column>
  197. // <Game game={game} operator={operator[0]} show_update_modal={this.show_update_modal} show={this.show} />
  198. <SortableList key={i}onSortEnd={this.onSortEnd}/>
  199. )
  200. })
  201. // .reduce((r, element, index) =>{
  202. // // console.log(index)
  203. // // console.log(r)
  204. // // console.log(element)
  205. // index % row_size === 0 && r.push([]);
  206. // r[r.length - 1].push(element);
  207. // return r;
  208. // }, []).map((row_content) => {
  209.  
  210.  
  211. // return <Grid.Row columns={3} key={Math.random()} >{row_content}</Grid.Row>
  212. // })
  213. // console.log(rows)
  214. return rows
  215. }
  216. //render_games end
  217. handleChange = (e, { value }) => {
  218. //console.log(e)
  219. // console.log(value)
  220. this.setState({
  221. game_type: value })
  222. }
  223. changeValueHandler = (e) => {
  224. // console.log([e.target.name])
  225. this.setState({
  226. [e.target.name]: e.target.value
  227. })
  228. }
  229. op_change = (e ,data) =>{
  230.  
  231. this.setState({
  232. selected_operator: data.value
  233. })
  234. }
  235.  
  236. render() {
  237. //debugger;
  238. if(this.state.operators === undefined || this.state.operators.operators === undefined) return null
  239. const {operators} =this.state.operators
  240. const mapped_ops = operators.map((operator) => {
  241. const {created,ip_adress, name, operator_name, pass,portal_code, url,_id } = operator
  242. return {
  243. text: name,
  244. value: name,
  245. key: _id
  246. }
  247. })
  248.  
  249.  
  250. return (
  251. <div>
  252.  
  253. <Header as='h3' content='Manage Games' textAlign='center' style={style.h3} />
  254. <Grid style={style.grid}>
  255. {this.RENDER_GAMES()}
  256. </Grid>
  257. <Modal size={'small'} open={this.state.modal_delete} onClose={this.close}>
  258. <Modal.Header>Delete This Game</Modal.Header>
  259. <Modal.Content>
  260. <p>Are you sure you want to delete this game ?</p>
  261. </Modal.Content>
  262. <Modal.Actions>
  263. <Button onClick={() => this.close()}negative>No</Button>
  264. <Button onClick={() => this.handle_delete(this.state.selected_game)} positive icon='checkmark' labelPosition='right' content='Yes' />
  265. </Modal.Actions>
  266. </Modal>
  267.  
  268.  
  269. <Modal size={'large'} open={this.state.modal_update} onClose={this.close_update_modal}>
  270. <Modal.Header>Update Game Settings</Modal.Header>
  271. <Modal.Content>
  272. <Form error={this.state.err} autoComplete="off">
  273. <Form.Field>
  274. <label>Game Name:</label>
  275. <input name="game_name" placeholder={this.state.selected_game.game_name} onChange={this.changeValueHandler}/>
  276. </Form.Field>
  277. <Form.Field>
  278. <label>Game ID:</label>
  279. <input name="game_id" placeholder={this.state.selected_game.game_id} onChange={this.changeValueHandler}/>
  280. </Form.Field>
  281. <Form.Group inline>
  282. <label>Game Type:</label>
  283. <Form.Field
  284. control={Radio}
  285. label='5/35'
  286. value='5/35'
  287. checked={this.state.game_type === '5/35'}
  288. onChange={ this.handleChange}
  289. />
  290. <Form.Field
  291. control={Radio}
  292. label='6/42'
  293. value='6/42'
  294. checked={this.state.game_type === '6/42'}
  295. onChange={ this.handleChange}
  296. />
  297. <Form.Field>
  298. <Dropdown placeholder='Select Operator' onChange={this.op_change} search selection options={mapped_ops} />
  299. </Form.Field>
  300. {
  301. this.state.err &&
  302. <Message
  303. error
  304. header='Action Forbidden'
  305. content='Game ID must be a number and you must select a game type, operator and game name.'
  306. />
  307. }
  308. </Form.Group>
  309. </Form>
  310. </Modal.Content>
  311. <Modal.Actions>
  312. <Button onClick={() => this.close_update_modal()}negative content="Go Back" />
  313. <Button onClick={() => this.handle_update(this.state.selected_game)} positive icon='checkmark' labelPosition='right' content='Finish' />
  314. </Modal.Actions>
  315. </Modal>
  316. </div>
  317. )
  318. }
  319. }
  320.  
  321. const style = {
  322. h1: {
  323. marginTop: '3em',
  324. },
  325. h2: {
  326. margin: '4em 0em 2em',
  327. },
  328. h3: {
  329. marginTop: '0em',
  330. padding: '2em 0em',
  331. },
  332. last: {
  333. marginBottom: '300px',
  334. },
  335. grid: {
  336. marginLeft: '50px'
  337. }
  338. }
  339.  
  340. function map_state_to_props(state) {
  341. return {
  342. games: state.games,
  343. operators: state.operators
  344. }
  345. }
  346.  
  347.  
  348. export default connect(map_state_to_props, {fetch_games, fetch_operators})(withAlert(Games))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement