All files / add-report/components/AddReport index.jsx

64.29% Statements 9/14
50% Branches 1/2
40% Functions 2/5
64.29% Lines 9/14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87                  1x                   1x 1x     1x 1x 1x                                               2x                                       1x       1x                          
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import styled from 'styled-components';
import Text from '@bufferapp/components/Text';
 
import { Button } from '@bufferapp/analyze-shared-components';
 
import Modal from '../Modal';
 
const ClickCatcher = styled.div`
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
`;
 
class AddReport extends Component {
  constructor(props) {
    super(props);
    this.state = {
      open: false,
    };
    this.toggleMenu = this.toggleMenu.bind(this);
    this.addReport = this.addReport.bind(this);
    this.selectReport = this.selectReport.bind(this);
  }
 
  toggleMenu() {
    this.setState({
      open: !this.state.open,
    });
  }
 
  addReport(name) {
    this.setState({
      open: false,
    });
    this.props.createReport(name);
  }
 
  selectReport(id) {
    this.setState({
      open: false,
    });
    this.props.addToReport(id);
  }
 
  render() {
    return (
      <span>
        <Button onClick={this.toggleMenu}>
          <Text color="outerSpace" size="small" weight="medium">{this.props.translations.addReport}</Text>
        </Button>
        <Modal
          translations={this.props.translations}
          open={this.state.open}
          toggle={this.toggleMenu}
          addReport={this.addReport}
          selectReport={this.selectReport}
          reports={this.props.reports}
        />
        {this.state.open && <ClickCatcher onClick={this.toggleMenu} />}
      </span>
    );
  }
}
 
 
AddReport.defaultProps = {
  reports: [],
};
 
AddReport.propTypes = {
  translations: PropTypes.shape({
    addReport: PropTypes.string,
  }).isRequired,
  createReport: PropTypes.func.isRequired,
  addToReport: PropTypes.func.isRequired,
  reports: PropTypes.arrayOf(PropTypes.shape({
    updated_at: PropTypes.number,
    name: PropTypes.string,
  })),
};
 
export default AddReport;