Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState }  from 'react'
  2. import { connect } from 'react-redux'
  3. import { bindActionCreators } from 'redux'
  4. import { Button, Icon } from 'semantic-ui-react'
  5. import { Table } from 'semantic-ui-react'
  6. import _ from 'lodash'
  7.  
  8. import { removeWorkout } from './workOutActions'
  9.  
  10.  
  11. const WorkOutList = props => {
  12.  
  13.     const handleSort = (clickedColumn) => () => {
  14.  
  15.         const [column, data, direction] = useState(0);
  16.    
  17.         if (column !== clickedColumn) {
  18.           this.setState({
  19.             column: clickedColumn,
  20.             data: _.sortBy(data, [clickedColumn]),
  21.             direction: 'ascending',
  22.           })
  23.    
  24.           return
  25.         }
  26.    
  27.         this.setState({
  28.           data: data.reverse(),
  29.           direction: direction === 'ascending' ? 'descending' : 'ascending',
  30.         })
  31.     }
  32.  
  33.     const renderRows = () => {
  34.         const list = props.list || []
  35.         return list.map(workout => (
  36.  
  37.             <Table.Row key={workout.id}>
  38.                 <Table.Cell>{workout.tempoGasto}h</Table.Cell>
  39.                 <Table.Cell>{workout.tipoTarefa}</Table.Cell>
  40.                 <Table.Cell>{workout.data}</Table.Cell>
  41.                 <Table.Cell>
  42.                     <Button
  43.                     animated='vertical'
  44.                     onClick={() => props.removeWorkout(workout)}
  45.                     >
  46.                         <Button.Content hidden>Deletar</Button.Content>
  47.                         <Button.Content visible>
  48.                             <Icon name='trash' />
  49.                         </Button.Content>
  50.                     </Button>
  51.                 </Table.Cell>
  52.             </Table.Row>
  53.         ))
  54.     }
  55.  
  56.     return (
  57.         <Table sortable celled fixed>
  58.             <Table.Header>
  59.                 <Table.Row>
  60.                     <Table.HeaderCell
  61.                      sorted={props.column === 'tempoGasto' ? direction : null}
  62.                      onClick={handleSort('tempoGasto')}
  63.                     >
  64.                         Tempo
  65.                     </Table.HeaderCell>
  66.                     <Table.HeaderCell
  67.                      sorted={props.column === 'tipoTarefa' ? direction : null}
  68.                      onClick={handleSort('tipoTarefa')}
  69.                     >
  70.                         Tipo
  71.                     </Table.HeaderCell>
  72.                     <Table.HeaderCell
  73.                      sorted={props.column === 'data' ? direction : null}
  74.                      onClick={handleSort('data')}
  75.                     >
  76.                         Data
  77.                     </Table.HeaderCell>
  78.                     <Table.HeaderCell>
  79.                         Ações
  80.                     </Table.HeaderCell>
  81.                 </Table.Row>
  82.             </Table.Header>
  83.  
  84.             <Table.Body>
  85.                 {renderRows()}
  86.             </Table.Body>
  87.         </Table>
  88.     )
  89. }
  90.  
  91. const mapStateToProps = state => ({list: state.workout.list})
  92. const mapDispatchToProps = dispatch =>
  93.     bindActionCreators({ removeWorkout }, dispatch)
  94. export default connect(mapStateToProps, mapDispatchToProps)(WorkOutList)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement