{"size": 6153, "ext": "js", "max_stars_repo_path": "src/components/Account/Table.js", "max_stars_repo_name": "zsevic/upis", "max_stars_repo_stars_event_min_datetime": "2019-03-10T18:24:59.000Z", "max_stars_repo_stars_event_max_datetime": "2019-03-10T18:24:59.000Z", "max_issues_repo_path": "src/components/Account/Table.js", "max_issues_repo_name": "zsevic/upis", "max_issues_repo_issues_event_min_datetime": "2021-05-07T04:31:44.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-26T10:03:00.000Z", "max_forks_repo_path": "src/components/Account/Table.js", "max_forks_repo_name": "zsevic/upis", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 6, "max_forks_count": null, "avg_line_length": 34.7627118644, "max_line_length": 87, "alphanum_fraction": 0.2941654477}
import React, { Component } from 'react'
import { Badge, Table } from 'reactstrap'
import { Mutation } from 'react-apollo'

import { ArrowDropUp, ArrowDropDown } from '@material-ui/icons'

import * as queries from './queries'

export default class TableComponent extends Component {
  constructor(props) {
    super(props)

    this.state = {
      counter: this.props.faculty.counter,
      departments: [...this.props.faculty.departments],
    }
  }

  incrementCounter = async upPlaceMutation => {
    await upPlaceMutation()

    this.setState({ counter: this.state.counter + 1 })
  }

  downPlace = async (index, key, downPlaceMutation) => {
    const { departments } = this.state
    if (departments[index][key] <= 0) return

    const { data } = await downPlaceMutation()
    const { downPlace } = data

    departments[index] = { ...downPlace }

    this.setState({ departments, counter: this.state.counter + 1 })
  }

  render() {
    return (
      <div>
        <Badge color="primary" pill>
          {this.props.faculty.name}
        </Badge>
        <Badge color="danger" pill>
          {this.state.counter}{' '}
          {this.props.session.me ? (
            <Mutation
              mutation={queries.INCREMENT_COUNTER}
              variables={{
                facultyId: this.props.faculty.id,
              }}
            >
              {(upPlaceMutation, { data, loading, error }) => (
                <ArrowDropUp
                  onClick={() =>
                    this.incrementCounter(upPlaceMutation)
                  }
                />
              )}
            </Mutation>
          ) : (
            ''
          )}
        </Badge>
        <Table responsive hover>
          <thead>
            <tr>
              <th>#</th>
              <th>Department</th>
              <th>Total</th>
              <th>Budget</th>
              <th>Self-financing</th>
            </tr>
          </thead>
          <tbody>
            {this.props.faculty.departments.map(
              (department, index) => {
                return (
                  <tr key={department.id}>
                    <th scope="row">{index + 1}</th>
                    {Object.keys(department).map(key => {
                      switch (key) {
                        case 'name':
                        case 'total':
                          return (
                            <td
                              key={key}
                              className={`department${key[0].toUpperCase()}${key.slice(
                                1,
                              )}`}
                            >
                              {this.state.departments[index][key]}
                            </td>
                          )
                        case 'budget':
                        case 'selfFinancing':
                          return (
                            <td
                              key={key}
                              className={`department${key[0].toUpperCase()}${key.slice(
                                1,
                              )}`}
                            >
                              {/*                               {this.props.session &&
                              this.props.session.me ? (
                                <Mutation
                                  mutation={queries.UP_PLACE}
                                  variables={{
                                    id: department.id,
                                    attribute: key,
                                    facultyId: this.props.faculty.id,
                                  }}
                                >
                                  {(
                                    upPlaceMutation,
                                    { data, loading, error },
                                  ) => (
                                    <ArrowDropUp
                                      onClick={() =>
                                        this.upPlace(
                                          index,
                                          upPlaceMutation,
                                        )
                                      }
                                    />
                                  )}
                                </Mutation>
                              ) : (
                                ''
                              )} */}

                              {this.state.departments[index][key]}

                              {this.props.session &&
                              this.props.session.me ? (
                                <Mutation
                                  mutation={queries.DOWN_PLACE}
                                  variables={{
                                    id: department.id,
                                    attribute: key,
                                    facultyId: this.props.faculty.id,
                                  }}
                                >
                                  {(
                                    downPlaceMutation,
                                    { data, loading, error },
                                  ) => (
                                    <ArrowDropDown
                                      onClick={() =>
                                        this.downPlace(
                                          index,
                                          key,
                                          downPlaceMutation,
                                        )
                                      }
                                    />
                                  )}
                                </Mutation>
                              ) : (
                                ''
                              )}
                            </td>
                          )
                        default:
                          return ''
                      }
                    })}
                  </tr>
                )
              },
            )}
          </tbody>
        </Table>
      </div>
    )
  }
}
