Merge pull request #1150 from chaotixkilla/chore-cleanup-analytics-page-for-8.0

Cleanup analytics page
This commit is contained in:
Rafael Taranto 2022-03-10 13:33:18 +00:00 committed by GitHub
commit f027d3185d
3 changed files with 56 additions and 31 deletions

View file

@ -3,7 +3,7 @@ import { Box } from '@material-ui/core'
import { makeStyles } from '@material-ui/core/styles' import { makeStyles } from '@material-ui/core/styles'
import classnames from 'classnames' import classnames from 'classnames'
import { endOfToday } from 'date-fns' import { endOfToday } from 'date-fns'
import { subDays } from 'date-fns/fp' import { subDays, format, add, startOfWeek } from 'date-fns/fp'
import gql from 'graphql-tag' import gql from 'graphql-tag'
import * as R from 'ramda' import * as R from 'ramda'
import React, { useState } from 'react' import React, { useState } from 'react'
@ -43,6 +43,16 @@ const TIME_OPTIONS = {
month: MONTH month: MONTH
} }
const DAY_OPTIONS = R.map(
it => ({
code: R.toLower(it),
display: it
}),
Array.from(Array(7)).map((_, i) =>
format('EEEE', add({ days: i }, startOfWeek(new Date())))
)
)
const GET_TRANSACTIONS = gql` const GET_TRANSACTIONS = gql`
query transactions( query transactions(
$from: Date $from: Date
@ -138,6 +148,9 @@ const Analytics = () => {
const [representing, setRepresenting] = useState(REPRESENTING_OPTIONS[0]) const [representing, setRepresenting] = useState(REPRESENTING_OPTIONS[0])
const [period, setPeriod] = useState(PERIOD_OPTIONS[0]) const [period, setPeriod] = useState(PERIOD_OPTIONS[0])
const [machine, setMachine] = useState(MACHINE_OPTIONS[0]) const [machine, setMachine] = useState(MACHINE_OPTIONS[0])
const [selectedDay, setSelectedDay] = useState(
R.equals(representing.code, 'hourOfTheDay') ? DAY_OPTIONS[0] : null
)
const loading = txLoading || configLoading const loading = txLoading || configLoading
@ -179,15 +192,27 @@ const Analytics = () => {
const filteredData = timeInterval => ({ const filteredData = timeInterval => ({
current: current:
machineTxs.filter( machineTxs.filter(d => {
d => new Date(d.created) >= Date.now() - TIME_OPTIONS[timeInterval] const txDay = new Date(d.created)
) ?? [], const isSameWeekday = !R.isNil(selectedDay)
? R.equals(R.toLower(format('EEEE', txDay)), selectedDay.code)
: true
return isSameWeekday && txDay >= Date.now() - TIME_OPTIONS[timeInterval]
}) ?? [],
previous: previous:
machineTxs.filter( machineTxs.filter(d => {
d => const txDay = new Date(d.created)
new Date(d.created) < Date.now() - TIME_OPTIONS[timeInterval] && const isSameWeekday = !R.isNil(selectedDay)
new Date(d.created) >= Date.now() - 2 * TIME_OPTIONS[timeInterval] ? R.equals(R.toLower(format('EEEE', txDay)), selectedDay.code)
) ?? [] : true
return (
isSameWeekday &&
txDay < Date.now() - TIME_OPTIONS[timeInterval] &&
txDay >= Date.now() - 2 * TIME_OPTIONS[timeInterval]
)
}) ?? []
}) })
const txs = { const txs = {
@ -224,6 +249,13 @@ const Analytics = () => {
) )
} }
const handleRepresentationChange = newRepresentation => {
setRepresenting(newRepresentation)
setSelectedDay(
R.equals(newRepresentation.code, 'hourOfTheDay') ? DAY_OPTIONS[0] : null
)
}
const getGraphInfo = representing => { const getGraphInfo = representing => {
switch (representing.code) { switch (representing.code) {
case 'overTime': case 'overTime':
@ -264,6 +296,9 @@ const Analytics = () => {
machines={machineOptions} machines={machineOptions}
selectedMachine={machine} selectedMachine={machine}
handleMachineChange={setMachine} handleMachineChange={setMachine}
selectedDay={selectedDay}
dayOptions={DAY_OPTIONS}
handleDayChange={setSelectedDay}
timezone={timezone} timezone={timezone}
currency={fiatLocale} currency={fiatLocale}
/> />
@ -296,7 +331,7 @@ const Analytics = () => {
<div className={classes.dropdowns}> <div className={classes.dropdowns}>
<Select <Select
label="Representing" label="Representing"
onSelectedItemChange={setRepresenting} onSelectedItemChange={handleRepresentationChange}
items={REPRESENTING_OPTIONS} items={REPRESENTING_OPTIONS}
default={REPRESENTING_OPTIONS[0]} default={REPRESENTING_OPTIONS[0]}
selectedItem={representing} selectedItem={representing}

View file

@ -1,11 +1,10 @@
import { Box } from '@material-ui/core' import { Box } from '@material-ui/core'
import { makeStyles } from '@material-ui/core/styles' import { makeStyles } from '@material-ui/core/styles'
import { getTimezoneOffset } from 'date-fns-tz' import { getTimezoneOffset } from 'date-fns-tz'
import { format, add, startOfWeek } from 'date-fns/fp'
import * as R from 'ramda' import * as R from 'ramda'
import React, { useState } from 'react' import React, { useState } from 'react'
import { RadioGroup, Select } from 'src/components/inputs' import { Select } from 'src/components/inputs'
import { H2 } from 'src/components/typography' import { H2 } from 'src/components/typography'
import { MINUTE } from 'src/utils/time' import { MINUTE } from 'src/utils/time'
@ -20,16 +19,6 @@ const options = [
{ code: 'hourOfDayVolume', display: 'Volume' } { code: 'hourOfDayVolume', display: 'Volume' }
] ]
const dayOptions = R.map(
it => ({
code: R.toLower(it),
display: it
}),
Array.from(Array(7)).map((_, i) =>
format('EEEE', add({ days: i }, startOfWeek(new Date())))
)
)
const HourOfDayBarGraphHeader = ({ const HourOfDayBarGraphHeader = ({
title, title,
period, period,
@ -37,13 +26,15 @@ const HourOfDayBarGraphHeader = ({
machines, machines,
selectedMachine, selectedMachine,
handleMachineChange, handleMachineChange,
selectedDay,
dayOptions,
handleDayChange,
timezone, timezone,
currency currency
}) => { }) => {
const classes = useStyles() const classes = useStyles()
const [graphType, setGraphType] = useState(options[0].code) const [graphType /*, setGraphType */] = useState(options[0].code)
const [selectedDay, setSelectedDay] = useState(dayOptions[0])
const legend = { const legend = {
cashIn: <div className={classes.cashInIcon}></div>, cashIn: <div className={classes.cashInIcon}></div>,
@ -100,18 +91,18 @@ const HourOfDayBarGraphHeader = ({
</Box> </Box>
</div> </div>
<div className={classes.graphHeaderRight}> <div className={classes.graphHeaderRight}>
<RadioGroup {/* <RadioGroup
options={options} options={options}
className={classes.topMachinesRadio} className={classes.topMachinesRadio}
value={graphType} value={graphType}
onChange={e => setGraphType(e.target.value)} onChange={e => setGraphType(e.target.value)}
/> /> */}
<Select <Select
label="Day of the week" label="Day of the week"
items={dayOptions} items={dayOptions}
default={dayOptions[0]} default={dayOptions[0]}
selectedItem={selectedDay} selectedItem={selectedDay}
onSelectedItemChange={setSelectedDay} onSelectedItemChange={handleDayChange}
/> />
<Select <Select
label="Machines" label="Machines"

View file

@ -3,7 +3,6 @@ import { makeStyles } from '@material-ui/core/styles'
import * as R from 'ramda' import * as R from 'ramda'
import React, { useState } from 'react' import React, { useState } from 'react'
import { RadioGroup } from 'src/components/inputs'
import { H2 } from 'src/components/typography' import { H2 } from 'src/components/typography'
import styles from '../../Analytics.styles' import styles from '../../Analytics.styles'
@ -28,7 +27,7 @@ const TopMachinesBarGraphHeader = ({
}) => { }) => {
const classes = useStyles() const classes = useStyles()
const [graphType, setGraphType] = useState(options[0].code) const [graphType /*, setGraphType */] = useState(options[0].code)
const legend = { const legend = {
cashIn: <div className={classes.cashInIcon}></div>, cashIn: <div className={classes.cashInIcon}></div>,
@ -46,12 +45,12 @@ const TopMachinesBarGraphHeader = ({
</Box> </Box>
</div> </div>
<div className={classes.graphHeaderRight}> <div className={classes.graphHeaderRight}>
<RadioGroup {/* <RadioGroup
options={options} options={options}
className={classes.topMachinesRadio} className={classes.topMachinesRadio}
value={graphType} value={graphType}
onChange={e => setGraphType(e.target.value)} onChange={e => setGraphType(e.target.value)}
/> /> */}
</div> </div>
</div> </div>
<Graph <Graph