Skip to content
Snippets Groups Projects
TraineeTableRow.js 5.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • import React, { Component } from 'react';
    import {
      Comment,
      Table,
      Icon,
      Popup,
      Grid,
      Button,
      Form,
      Dropdown,
    } from 'semantic-ui-react';
    import { connect } from 'react-redux';
    import { visitorChange, submitVisitors } from '../../actions/statistics';
    import { writeNote, clearWrite, postEventNote } from '../../actions/notes';
    
    const visitStates = [
      {
        text: 'Igen',
        value: 'Visitor',
      },
      {
        text: 'Szólt h nem',
        value: 'Absent',
      },
      {
        text: 'Nem',
        value: 'No',
      }
    ]
    
    class TraineeTableRow extends Component {
      constructor(props) {
        super(props);
        this.state = {
          showAddPopup: false,
          showMorePopup: false,
        };
      }
    
    
    Rafael László's avatar
    Rafael László committed
      // Hides and shows the Add and More popup 
    
      triggerAdd = () => this.setState({ ...this.state, showAddPopup: !this.state.showAddPopup})
      triggerMore = () => this.setState({ ...this.state, showMorePopup: !this.state.showMorePopup })
    
      render() {
        const note = this.props.actualNote;
        const { trainee, edit, actualNote, selectedEvent, notes } = this.props;
        const isVisitor = selectedEvent.visitors.includes(trainee.id);
        const isAbsent = selectedEvent.absent.includes(trainee.id);
        return (
          <Table.Row>
    
            <Table.Cell textAlign='center'>
    
              {trainee.full_name}
            </Table.Cell>
    
    Rafael László's avatar
    Rafael László committed
            {/* Show and change Visitors status  */}
    
            {!this.props.edit ?
              <Table.Cell textAlign='center'>
                {
                    isVisitor ?
                      <Icon color='green' name='checkmark' />
                    :
                    isAbsent ?
                      <Icon color='orange' name='minus' />
                      :
                      <Icon color='red' name='cancel' />
                  }
              </Table.Cell>
    
    Rafael László's avatar
    Rafael László committed
            :
    
              <Table.Cell textAlign='center'>
                <Dropdown
                  defaultValue={isVisitor ? 'Visitor' : isAbsent ? 'Absent' : 'No'}
                  selection
                  options={visitStates}
                  onChange={(_, v) => this.props.visitorChange({ id : trainee.id, value: v.value })}
                />
              </Table.Cell>
            }
    
    Rafael László's avatar
    Rafael László committed
            {/* Notes for trainees */}
    
    Rafael László's avatar
    Rafael László committed
                  {/* Note text */}
    
                  <Grid.Column floated='left' width={8}>
    
                    {notes.length > 0 ?
                      <Comment>
                        <Comment.Content>
    
                          <Comment.Author><b>{notes[0].created_by_name}:</b></Comment.Author>
    
    Rafael László's avatar
    Rafael László committed
                          <Comment.Text style={{wordWrap: 'break-word'}}>
    
    Rafael László's avatar
    Rafael László committed
                            {notes[0].note.length > 25 ? 
                              notes[0].note.slice(0, 25).concat('...')
                            :
                              notes[0].note 
                            }
    
                          </Comment.Text>
                        </Comment.Content>
                      </Comment>
    
    Rafael László's avatar
    Rafael László committed
                    :
    
    Rafael László's avatar
    Rafael László committed
                    }
    
    Rafael László's avatar
    Rafael László committed
                  {/* Note buttons */}
    
                  <Grid.Column floated='right' width={6} textAlign='right'>
    
    Rafael László's avatar
    Rafael László committed
                      <Popup basic
    
                        open={this.state.showMorePopup}
    
    Rafael László's avatar
    Rafael László committed
                        trigger={
                          <Button icon='comment alternate outline' onClick={this.triggerMore} />
                        }
    
                        content={notes.map((note) => {
    
                          return (
                            <Comment.Content>
    
    Rafael László's avatar
    Rafael László committed
                              <Comment.Author><b>{note.created_by_name}:</b></Comment.Author>
    
                              <Comment.Text>
                                {note.note}
                              </Comment.Text>
                            </Comment.Content>
                          );
                        })}
    
    Rafael László's avatar
    Rafael László committed
                      null
    
    Rafael László's avatar
    Rafael László committed
                    <Popup basic
                      trigger={
                        <Button icon='plus' onClick={this.triggerAdd}/>
                      }
    
                      open={this.state.showAddPopup}
                      content={
                        <Form reply>
                          <Form.TextArea
                            value={note.note}
                            onChange={e => this.props.writeNote(e)}
                          />
    
    Rafael László's avatar
    Rafael László committed
                          <Button primary
    
                              this.triggerAdd()
                              this.props.postEventNote({ 
                                eventid: selectedEvent.id,
                                userid: trainee.id,
                                note: note.note 
                              })
                              this.props.clearWrite()
                            }}
    
                            content='Megjegyzés hozzáadása'
                            labelPosition='left'
                            icon='edit'
                          />
                        </Form>
    
    Rafael László's avatar
    Rafael László committed
                      }
    
                    />
                  </Grid.Column>
                </Grid.Row>
              </Grid>
            </Table.Cell>
          </Table.Row>
        );
      }
    }
    const mapStateToProps = ({ notes: { actualNote } }) => ({ actualNote })
    
    export default connect(mapStateToProps, { writeNote, clearWrite, postEventNote, visitorChange})(TraineeTableRow)