From eb6e0909361d42ba46160f25e0339fe93259c990 Mon Sep 17 00:00:00 2001 From: Nikola Ubavic <53820106+ubavic@users.noreply.github.com> Date: Thu, 16 Dec 2021 11:31:03 +0100 Subject: [PATCH] fix: redirect to m-status on invalid machine ID --- .../src/pages/Machines/Machines.js | 40 ++++++++++++++++--- new-lamassu-admin/src/routing/routes.js | 4 +- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/new-lamassu-admin/src/pages/Machines/Machines.js b/new-lamassu-admin/src/pages/Machines/Machines.js index 89c13f24..19381f20 100644 --- a/new-lamassu-admin/src/pages/Machines/Machines.js +++ b/new-lamassu-admin/src/pages/Machines/Machines.js @@ -7,7 +7,7 @@ import classnames from 'classnames' import gql from 'graphql-tag' import * as R from 'ramda' 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' @@ -50,13 +50,43 @@ const GET_INFO = gql` } ` +const GET_MACHINES = gql` + { + machines { + name + deviceId + } + } +` + const getMachineID = path => path.slice(path.lastIndexOf('/') + 1) -const Machines = () => { +const MachineRoute = () => { 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 && +} + +const Machines = ({ id, reload }) => { const { data, loading, refetch } = useQuery(GET_INFO, { variables: { - deviceId: getMachineID(location.pathname) + deviceId: id } }) const classes = useStyles() @@ -85,7 +115,7 @@ const Machines = () => { {machineName} - + @@ -119,4 +149,4 @@ const Machines = () => { ) } -export default Machines +export default MachineRoute diff --git a/new-lamassu-admin/src/routing/routes.js b/new-lamassu-admin/src/routing/routes.js index 18876686..5aa939bb 100644 --- a/new-lamassu-admin/src/routing/routes.js +++ b/new-lamassu-admin/src/routing/routes.js @@ -17,7 +17,7 @@ import Register from 'src/pages/Authentication/Register' import Reset2FA from 'src/pages/Authentication/Reset2FA' import ResetPassword from 'src/pages/Authentication/ResetPassword' 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 PrivateRoute from './PrivateRoute' @@ -140,7 +140,7 @@ const Routes = () => { } /> - + {/* */}