Chore: remove unneeded useEffect

This commit is contained in:
Cesar 2020-12-15 21:21:56 +00:00 committed by Josh Harvey
parent 6994303069
commit bb9da7ff80

View file

@ -3,7 +3,7 @@ import { Grid, Divider } from '@material-ui/core'
import { makeStyles } from '@material-ui/core/styles' import { makeStyles } from '@material-ui/core/styles'
import gql from 'graphql-tag' import gql from 'graphql-tag'
import moment from 'moment' import moment from 'moment'
import React, { useState, useEffect } from 'react' import React, { useState } from 'react'
import { ConfirmDialog } from 'src/components/ConfirmDialog' import { ConfirmDialog } from 'src/components/ConfirmDialog'
import { Status } from 'src/components/Status' import { Status } from 'src/components/Status'
@ -96,50 +96,36 @@ const Item = ({ children, ...props }) => (
</Grid> </Grid>
) )
const getState = machineEventsLazy =>
JSON.parse(machineEventsLazy.machine.latestEvent?.note ?? '{"state": null}')
.state
const MachineDetailsRow = ({ it: machine, onActionSuccess }) => { const MachineDetailsRow = ({ it: machine, onActionSuccess }) => {
const [action, setAction] = useState({ command: null }) const [action, setAction] = useState({ command: null })
const [errorMessage, setErrorMessage] = useState(null) const [errorMessage, setErrorMessage] = useState(null)
const classes = useMDStyles() const classes = useMDStyles()
const [ const warningMessage = (
fetchMachineEvents, <span className={classes.warning}>
{ loading: loadingEvents, data: machineEventsLazy } A user may be in the middle of a transaction and they could lose their
] = useLazyQuery(MACHINE, { funds if you continue.
variables: { </span>
deviceId: machine.deviceId )
}
})
useEffect(() => { const [fetchMachineEvents, { loading: loadingEvents }] = useLazyQuery(
if (action.command === 'restartServices') { MACHINE,
fetchMachineEvents() {
} variables: {
}, [action.command, fetchMachineEvents]) deviceId: machine.deviceId
},
useEffect(() => { onCompleted: machineEventsLazy => {
if (machineEventsLazy && action.command === 'restartServices') { const message = !isStaticState(getState(machineEventsLazy))
const state = JSON.parse( ? warningMessage
machineEventsLazy.machine.latestEvent?.note ?? '{"state": null}' : null
).state setAction(action => ({ ...action, message }))
if (!isStaticState(state)) {
setAction(action => ({
...action,
message: (
<span className={classes.warning}>
A user may be in the middle of a transaction and they could lose
their funds if you continue.
</span>
)
}))
} else {
// clear message from object when state goes from not static to static
setAction(action => ({
...action,
message: null
}))
} }
} }
}, [action.command, classes.warning, machineEventsLazy]) )
const [machineAction, { loading }] = useMutation(MACHINE_ACTION, { const [machineAction, { loading }] = useMutation(MACHINE_ACTION, {
onError: ({ message }) => { onError: ({ message }) => {
@ -301,12 +287,13 @@ const MachineDetailsRow = ({ it: machine, onActionSuccess }) => {
Icon={RebootIcon} Icon={RebootIcon}
InverseIcon={RebootReversedIcon} InverseIcon={RebootReversedIcon}
disabled={loading} disabled={loading}
onClick={() => onClick={() => {
fetchMachineEvents()
setAction({ setAction({
command: 'restartServices', command: 'restartServices',
display: 'Restart services for' display: 'Restart services for'
}) })
}> }}>
Restart Services Restart Services
</ActionButton> </ActionButton>
</div> </div>