Guest User

Untitled

a guest
Oct 18th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. const CreatePlayer = (props) => {
  2.  
  3. const form = (data) => {
  4. console.log(data)
  5. firebase.database().ref(`open/${props.open_key}/players`).push({
  6. name: data.name,
  7. club: data.club,
  8. category: data.category,
  9. cabeza_serie: data.cabeza_serie
  10. })
  11. .then(function(response){
  12. console.log(response)
  13. props.clear()
  14. })
  15. .catch(error => console.log(error))
  16. }
  17. return(
  18. <section className="hero">
  19. <div className="hero-body">
  20. <b className="title is-4">Crear Jugador</b>
  21. <PlayerForm onSubmit={form} />
  22. </div>
  23. </section>
  24. )
  25.  
  26. const mapStateToProps = (state) => {
  27. return {
  28. open_key: state.open_key,
  29. created_player : state.created_player
  30. }
  31. }
  32.  
  33. const mapDispatchToProps = (dispatch) => {
  34. return {
  35. clear: () => {
  36. dispatch(reset('syncValidationPlayer'))
  37. },
  38. clear_players: () => {
  39. dispatch({type: 'PLAYER_CLEAR'})
  40. }
  41. }
  42. }
  43.  
  44. import React, { Component } from 'react'
  45. import { connect } from 'react-redux'
  46. import firebase from 'firebase'
  47.  
  48.  
  49. class ListPlayer extends Component {
  50.  
  51. componentDidMount(){
  52. this.props.load_players(this.props.open_key)
  53. }
  54.  
  55. componentWillUnmount(){
  56. this.props.clear_data()
  57. console.log("list : componentWillUnmount")
  58. }
  59.  
  60. list_player = () => {
  61. var list
  62. if(this.props.player.length !== 0){
  63. console.log("n jugadores : " + this.props.player.length)
  64. list = this.props.player.map((player, value) => {
  65. return(
  66. <tbody key={value}>
  67. <tr>
  68. <td>#</td>
  69. <td>{player.name}</td>
  70. <td>{player.club}</td>
  71. <td>{player.cabeza_serie ? "Si" : "No"}</td>
  72. </tr>
  73. </tbody>
  74. )
  75. }).reverse()
  76. }else {
  77. return (
  78. <tbody>
  79. <tr>
  80. <td>No hay Jugadores inscritos.</td>
  81. </tr>
  82. </tbody>
  83. )
  84. }
  85. return list
  86. }
  87.  
  88.  
  89. render(){
  90. return(
  91. <section className="hero">
  92. <div className="hero-body">
  93. <b className="title is-4">Jugadores</b>
  94. <table className="table is-narrow">
  95. <thead>
  96. <tr>
  97. <th><abbr title="Position">Pos</abbr></th>
  98. <th>Nombre</th>
  99. <th><abbr title="club">Club</abbr></th>
  100. <th><abbr title="cabeza_serie">CS</abbr></th>
  101. </tr>
  102. </thead>
  103. {this.list_player()}
  104. </table>
  105. </div>
  106. </section>
  107. )
  108. }
  109. }
  110.  
  111. const mapStateToProps = (state) => {
  112. return {
  113. open_key: state.open_key,
  114. player: state.player
  115. }
  116. }
  117.  
  118. const mapDispatchToProps = (dispatch) => {
  119. return {
  120. load_players: (open_key) => {
  121. console.log('dispatch load players')
  122. var ref = firebase.database().ref("open/"+ open_key +"/players")
  123. ref.on("child_added", function(snapshot, prevChildKey) {
  124. var players = snapshot.val()
  125. dispatch({type: 'PLAYER_LIST', data: players})
  126. })
  127. },
  128. clear_data: () => {
  129. dispatch({type: 'PLAYER_CLEAR'})
  130. },
  131. clear_open_key: () => {
  132. dispatch({type: 'OPEN_KEY_CLEAR'})
  133. }
  134. }
  135. }
  136.  
  137. export default connect(mapStateToProps, mapDispatchToProps)(ListPlayer)
Add Comment
Please, Sign In to add comment