import { useQuery } from '@apollo/react-hooks' import { makeStyles, withStyles } from '@material-ui/core' import Table from '@material-ui/core/Table' import TableBody from '@material-ui/core/TableBody' import TableCell from '@material-ui/core/TableCell' import TableContainer from '@material-ui/core/TableContainer' import TableHead from '@material-ui/core/TableHead' import TableRow from '@material-ui/core/TableRow' import classnames from 'classnames' import gql from 'graphql-tag' import * as R from 'ramda' import React from 'react' import { useHistory } from 'react-router-dom' import { Status } from 'src/components/Status' import { Label2, TL2 } from 'src/components/typography' // import { ReactComponent as TxInIcon } from 'src/styling/icons/direction/cash-in.svg' import { ReactComponent as TxOutIcon } from 'src/styling/icons/direction/cash-out.svg' import { ReactComponent as MachineLinkIcon } from 'src/styling/icons/month arrows/right.svg' import { fromNamespace } from 'src/utils/config' import styles from './MachinesTable.styles' // percentage threshold where below this number the text in the cash cassettes percentage turns red const PERCENTAGE_THRESHOLD = 20 const GET_CONFIG = gql` query getConfig { config } ` const useStyles = makeStyles(styles) const StyledCell = withStyles({ root: { borderBottom: '4px solid white', padding: 0, paddingLeft: 15 } })(TableCell) const HeaderCell = withStyles({ root: { borderBottom: '4px solid white', padding: 0, paddingLeft: 15, backgroundColor: 'white' } })(TableCell) const MachinesTable = ({ machines = [], numToRender }) => { const classes = useStyles() const history = useHistory() const { data } = useQuery(GET_CONFIG) const fillingPercentageSettings = fromNamespace( 'notifications', R.path(['config'], data) ?? {} ) const getPercent = (notes, capacity = 500) => { return Math.round((notes / capacity) * 100) } const makePercentageText = (cassetteIdx, notes, capacity = 500) => { const percent = getPercent(notes, capacity) const percentageThreshold = R.pipe( R.path([`fillingPercentageCassette${cassetteIdx}`]), R.defaultTo(PERCENTAGE_THRESHOLD) )(fillingPercentageSettings) return percent < percentageThreshold ? ( {`${percent}%`} ) : ( {`${percent}%`} ) } const redirect = ({ name, deviceId }) => { return history.push(`/machines/${deviceId}`, { selectedMachine: name }) } const maxNumberOfCassettes = Math.max( ...R.map(it => it.numberOfCassettes, machines), 0 ) return ( Machines Status {/* */} {R.map( it => ( {it + 1} ), R.times(R.identity, maxNumberOfCassettes) )} {machines.map((machine, idx) => { if (idx < numToRender) { return ( redirect(machine)} className={classnames(classes.row)} key={machine.deviceId + idx}> {machine.name} redirect(machine)} /> {R.map( it => machine.numberOfCassettes >= it ? ( {makePercentageText(it, machine[`cassette${it}`])} ) : ( {`— %`} ), R.range(1, maxNumberOfCassettes + 1) )} ) } return null })} ) } export default MachinesTable