App.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import React from 'react'
  2. import XLSX from 'xlsx'
  3. import PlayerList from './player/player-list'
  4. import MatchList from './match/match-list'
  5. import Stats from './stats/stats'
  6. import { SheetFromArray, Workbook, saveAs } from './excel/excel'
  7. class App extends React.Component {
  8. constructor () {
  9. super()
  10. this.state = {
  11. stats: {
  12. matchDates: []
  13. },
  14. players: [],
  15. matches: [],
  16. worksheets: {
  17. 'PlayerList': null,
  18. 'Calendar': null
  19. },
  20. filters: {
  21. date: 'Alle'
  22. }
  23. }
  24. this.handlePlayerList = this.handlePlayerList.bind(this)
  25. this.handleCalendar = this.handleCalendar.bind(this)
  26. this.generatePlayerList = this.generatePlayerList.bind(this)
  27. this.generateCalendar = this.generateCalendar.bind(this)
  28. this.filterData = this.filterData.bind(this)
  29. this.generateSchedule = this.generateSchedule.bind(this)
  30. this.generatePayTable = this.generatePayTable.bind(this)
  31. }
  32. componentDidMount () {
  33. this.generateStats()
  34. }
  35. handlePlayerList (event) {
  36. const file = this.playerList.files[0]
  37. this.readWorkbook(file, this.generatePlayerList)
  38. }
  39. handleCalendar (event) {
  40. const file = this.calendar.files[0]
  41. this.readWorkbook(file, this.generateCalendar)
  42. }
  43. readWorkbook (file, callback) {
  44. const reader = new FileReader()
  45. reader.onload = (e) => {
  46. const data = e.target.result
  47. const workbook = XLSX.read(data, {type: 'binary'})
  48. console.log(workbook)
  49. if (workbook.SheetNames.length !== 1) {
  50. throw Error(`Expected only one worksheet in the file, but found ${workbook.SheetNames.length}.`)
  51. }
  52. const worksheet = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[workbook.SheetNames[0]], { header: 1 })
  53. callback(worksheet)
  54. }
  55. reader.readAsBinaryString(file)
  56. }
  57. generatePlayerList (worksheet) {
  58. console.log('About to read the player list.')
  59. const worksheets = { ...this.state.worksheets }
  60. worksheets['PlayerList'] = worksheet
  61. this.setState({ worksheets })
  62. if (worksheet[4].length !== 32 & worksheet[3][0] !== 'Konkurrenz' & worksheet[3][31] !== 'bezahlt') {
  63. throw Error('Wrong file structure.')
  64. }
  65. const players = worksheet.slice(4, worksheet.length)
  66. this.setState({ players })
  67. this.generateStats()
  68. console.log(this.state)
  69. }
  70. generateCalendar (worksheet) {
  71. console.log('About to read the calendar.')
  72. const worksheets = { ...this.state.worksheets }
  73. worksheets['Calendar'] = worksheet
  74. this.setState({ worksheets })
  75. if (worksheet[2].length !== 8) {
  76. throw Error('Wrong file structure.')
  77. }
  78. const matches = worksheet.slice(2, worksheet.length)
  79. this.setState({ matches })
  80. this.generateStats()
  81. console.log(this.state)
  82. }
  83. generateStats () {
  84. const stats = {
  85. players: [],
  86. playerCategories: [],
  87. matchCategories: [],
  88. matches: [],
  89. matchDates: [],
  90. matchPlaces: []
  91. }
  92. stats.players = this.state.players
  93. this.state.players.forEach((player, key) => {
  94. if (!stats.playerCategories.includes(player[0])) {
  95. stats.playerCategories.push(player[0])
  96. }
  97. })
  98. stats.matches = this.state.matches
  99. this.state.matches.forEach((match, key) => {
  100. if (!!match[1] & !stats.matchDates.includes(match[1])) {
  101. stats.matchDates.push(match[1])
  102. }
  103. if (!!match[3] & !stats.matchCategories.includes(match[3])) {
  104. stats.matchCategories.push(match[3])
  105. }
  106. if (!!match[0] & !stats.matchPlaces.includes(match[0])) {
  107. stats.matchPlaces.push(match[0])
  108. }
  109. })
  110. this.setState({ stats })
  111. }
  112. filterData () {
  113. const filters = {
  114. date: this.date.value
  115. }
  116. this.generateStats()
  117. this.setState({ filters })
  118. }
  119. generateSchedule (event) {
  120. event.preventDefault()
  121. const matchlist = new Workbook()
  122. matchlist.SheetNames = []
  123. matchlist.Sheets = {}
  124. const worksheets = {}
  125. const placeArray = this.state.stats.matchPlaces.concat(['Alle'])
  126. placeArray.forEach((place) => {
  127. let filter = this.state.filters.date
  128. let header = [
  129. [`Spielplan für den ${filter} (${place})`],
  130. [],
  131. ['Ort', 'Zeit', 'Kategorie', 'Spieler 1', '', 'Spieler 2', '', '1. Satz', '2. Satz', '3. Satz', 'WO Grund']
  132. ]
  133. let matchListPerPlace = this.state.matches.filter((match) => {
  134. return (match[1] === filter | filter === 'Alle') & (match[0] === place | place === 'Alle')
  135. }).map((match) =>
  136. [match[0], match[2], match[3], match[4], match[5], match[6], match[7]]
  137. )
  138. console.log(matchListPerPlace)
  139. worksheets[place] = SheetFromArray(header.concat(matchListPerPlace))
  140. matchlist.SheetNames.push(place)
  141. matchlist.Sheets[place] = worksheets[place]
  142. })
  143. console.log(matchlist)
  144. saveAs(matchlist, 'Spielerliste.xlsx')
  145. }
  146. generatePayTable (event) {
  147. event.preventDefault()
  148. const paylist = new Workbook()
  149. paylist.SheetNames = []
  150. paylist.Sheets = {}
  151. const worksheets = {}
  152. const placeArray = this.state.stats.matchPlaces.concat(['Alle'])
  153. const payerList = {}
  154. this.state.matches.forEach((match) => {
  155. if (!!match[4] & !payerList.hasOwnProperty(`${match[3]} ${match[4]}`)) {
  156. let foundPlayer = this.state.players.find((player) => {
  157. if (!!player[24]) {
  158. return (`${player[5]} ${player[6]} / ${player[24]} ${player[25]}` === match[4]) & (match[3] === player[0].replace(/\s+/g, ' '))
  159. } else {
  160. return (`${player[5]} ${player[6]}` === match[4]) & (match[3] === player[0].replace(/\s+/g, ' '))
  161. }
  162. })
  163. if (!foundPlayer) {
  164. console.log(foundPlayer, match)
  165. throw Error('Player 4 not found. This is an error!')
  166. }
  167. payerList[`${match[3]} ${match[4]}`] = [match[0], match[1], match[2], foundPlayer[31]]
  168. }
  169. if (!!match[6] & !payerList.hasOwnProperty(`${match[3]} ${match[6]}`)) {
  170. let foundPlayer = this.state.players.find((player) => {
  171. if (!!player[24]) {
  172. return (`${player[5]} ${player[6]} / ${player[24]} ${player[25]}` === match[6]) & (match[3] === player[0].replace(/\s+/g, ' '))
  173. } else {
  174. return (`${player[5]} ${player[6]}` === match[6]) & (match[3] === player[0].replace(/\s+/g, ' '))
  175. }
  176. })
  177. if (!foundPlayer) {
  178. console.log(foundPlayer, match)
  179. throw Error('Player 6 not found. This is an error!')
  180. }
  181. payerList[`${match[3]} ${match[6]}`] = [match[0], match[1], match[2], foundPlayer[31]]
  182. }
  183. })
  184. console.log(payerList)
  185. placeArray.forEach((place) => {
  186. let filter = this.state.filters.date
  187. let header = [
  188. [`Zahlliste für den ${filter} (${place})`],
  189. [],
  190. ['bereits bez.', 'Kategorie', 'Name', 'Betrag bez.', 'Quittung abgeben']
  191. ]
  192. let payListPerPlace = []
  193. this.state.matches.filter((match) => {
  194. return (match[1] === filter | filter === 'Alle') & (match[0] === place | place === 'Alle')
  195. }).forEach((match) => {
  196. if (!!match[4]) {
  197. payListPerPlace.push(match)
  198. }
  199. if (!!match[6]) {
  200. payListPerPlace.push(match)
  201. }
  202. })
  203. console.log(payListPerPlace)
  204. worksheets[place] = SheetFromArray(header.concat(payListPerPlace))
  205. paylist.SheetNames.push(place)
  206. paylist.Sheets[place] = worksheets[place]
  207. })
  208. console.log(paylist)
  209. saveAs(paylist, 'Zahlliste.xlsx')
  210. }
  211. render () {
  212. return (
  213. <div className='App'>
  214. <form>
  215. <label htmlFor='playerList'>Swisstennis playerList.xls</label>
  216. <input type='file' id='PlayerList' ref={(input) => { this.playerList = input }} accept='.xls' placeholder='playerList File' onChange={this.handlePlayerList} />
  217. <label htmlFor='Calendar'>Swisstennis Calendar.xls</label>
  218. <input type='file' id='Calendar' ref={(input) => { this.calendar = input }} accept='.xls' placeholder='Calendar File' onChange={this.handleCalendar} />
  219. <label htmlFor='Date'>Datum</label>
  220. <select ref={(input) => { this.date = input }} onChange={this.filterData}>
  221. <option>Alle</option>
  222. {this.state.stats.matchDates.map((date, key) => (
  223. <option key={key}>{date}</option>
  224. ))}
  225. </select>
  226. <button onClick={this.generateSchedule} disabled={!this.state.matches.length}>Spielliste generieren</button>
  227. <button onClick={this.generatePayTable} disabled={(!this.state.matches.length | !this.state.players.length)}>Zahlliste generieren</button>
  228. </form>
  229. <Stats stats={this.state.stats} />
  230. <PlayerList players={this.state.players} />
  231. <MatchList matches={this.state.matches} filters={this.state.filters} />
  232. </div>
  233. )
  234. }
  235. }
  236. export default App