fix: redirect to m-status on invalid machine ID

This commit is contained in:
Nikola Ubavic 2021-12-16 11:31:03 +01:00
parent 5c5d956d2c
commit eb6e090936
2 changed files with 37 additions and 7 deletions

View file

@ -7,7 +7,7 @@ import classnames from 'classnames'
import gql from 'graphql-tag' import gql from 'graphql-tag'
import * as R from 'ramda' import * as R from 'ramda'
import React from 'react' import React from 'react'
import { Link, useLocation } from 'react-router-dom' import { Link, useLocation, useHistory } from 'react-router-dom'
import { TL1, TL2, Label3 } from 'src/components/typography' import { TL1, TL2, Label3 } from 'src/components/typography'
@ -50,13 +50,43 @@ const GET_INFO = gql`
} }
` `
const GET_MACHINES = gql`
{
machines {
name
deviceId
}
}
`
const getMachineID = path => path.slice(path.lastIndexOf('/') + 1) const getMachineID = path => path.slice(path.lastIndexOf('/') + 1)
const Machines = () => { const MachineRoute = () => {
const location = useLocation() const location = useLocation()
const history = useHistory()
const id = getMachineID(location.pathname)
const { loading } = useQuery(GET_MACHINES, {
onCompleted: data => {
const machines = data.machines
const machineFound = machines.map(m => m.deviceId).includes(id)
if (!machineFound) return history.push('/maintenance/machine-status')
}
})
const reload = () => {
return history.push(location.pathname)
}
return !loading && <Machines id={id} reload={reload}></Machines>
}
const Machines = ({ id, reload }) => {
const { data, loading, refetch } = useQuery(GET_INFO, { const { data, loading, refetch } = useQuery(GET_INFO, {
variables: { variables: {
deviceId: getMachineID(location.pathname) deviceId: id
} }
}) })
const classes = useStyles() const classes = useStyles()
@ -85,7 +115,7 @@ const Machines = () => {
{machineName} {machineName}
</TL2> </TL2>
</Breadcrumbs> </Breadcrumbs>
<Overview data={machine} onActionSuccess={refetch} /> <Overview data={machine} onActionSuccess={reload} />
</div> </div>
</Grid> </Grid>
</Grid> </Grid>
@ -119,4 +149,4 @@ const Machines = () => {
) )
} }
export default Machines export default MachineRoute

View file

@ -17,7 +17,7 @@ import Register from 'src/pages/Authentication/Register'
import Reset2FA from 'src/pages/Authentication/Reset2FA' import Reset2FA from 'src/pages/Authentication/Reset2FA'
import ResetPassword from 'src/pages/Authentication/ResetPassword' import ResetPassword from 'src/pages/Authentication/ResetPassword'
import Dashboard from 'src/pages/Dashboard' import Dashboard from 'src/pages/Dashboard'
import Machines from 'src/pages/Machines' import MachineRoute from 'src/pages/Machines'
import Wizard from 'src/pages/Wizard' import Wizard from 'src/pages/Wizard'
import PrivateRoute from './PrivateRoute' import PrivateRoute from './PrivateRoute'
@ -140,7 +140,7 @@ const Routes = () => {
} }
/> />
</PrivateRoute> </PrivateRoute>
<PrivateRoute path="/machines" component={Machines} /> <PrivateRoute path="/machines" component={MachineRoute} />
<PrivateRoute path="/wizard" component={Wizard} /> <PrivateRoute path="/wizard" component={Wizard} />
<PublicRoute path="/register" component={Register} /> <PublicRoute path="/register" component={Register} />
{/* <PublicRoute path="/configmigration" component={ConfigMigration} /> */} {/* <PublicRoute path="/configmigration" component={ConfigMigration} /> */}