-
Notifications
You must be signed in to change notification settings - Fork 0
Feature handle api status #220
Changes from 3 commits
74d139c
dc4a4cf
1931506
305d694
1fad3bd
759b164
99e8086
861aea2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import PropTypes from "prop-types"; | ||
import { Box, makeStyles, Paper, useTheme } from "@material-ui/core"; | ||
import { Box, makeStyles, Paper, Typography, useTheme } from "@material-ui/core"; | ||
import { Route } from "react-router-dom"; | ||
import clsx from "clsx"; | ||
import ItemTableAppBar from "../ItemTableAppBar/"; | ||
|
@@ -10,46 +10,48 @@ import ItemView from "../ItemView/"; | |
import QueueSelector from "../QueueSelector/"; | ||
import { useToken } from "../AuthProvider/"; | ||
|
||
export default function AppView({ setDarkMode }){ | ||
export default function AppView({ setDarkMode }) { | ||
// Create stateful variables. | ||
const [sidebarOpen, setSidebarOpen] = useState(false); | ||
const [queues, setQueues] = useState([]); | ||
const [items, setItems] = useState([]); | ||
const [selectedQueues, setSelectedQueues] = useState([]); | ||
const [queueSelectorOpen, setQueueSelectorOpen] = useState(false); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [error, setError] = useState(false) | ||
const [response, setResponse] = useState({ status: '', message: '' }) | ||
|
||
const access_token = useToken(); | ||
|
||
// Get Queues from API. | ||
useEffect( _ => { | ||
(async function getQueues(){ | ||
if (access_token === null){ | ||
useEffect(_ => { | ||
(async function getQueues() { | ||
if (access_token === null) { | ||
return undefined; | ||
} | ||
|
||
if (queueSelectorOpen){ | ||
if (queueSelectorOpen) { | ||
return undefined; | ||
} | ||
|
||
if (selectedQueues.length === 0){ | ||
if (selectedQueues.length === 0) { | ||
setQueues([]) | ||
return undefined; | ||
} | ||
|
||
setIsLoading(true); | ||
let queuesToLoad = ""; | ||
|
||
if (selectedQueues.length === 1){ | ||
if (selectedQueues.length === 1) { | ||
queuesToLoad = selectedQueues[0].name; | ||
} | ||
else { | ||
selectedQueues.forEach( (queue, index) => ( | ||
selectedQueues.forEach((queue, index) => ( | ||
index === 0 | ||
? queuesToLoad += queue.name | ||
: queuesToLoad += `+${queue.name}` | ||
)); | ||
} | ||
} | ||
|
||
let myHeaders = new Headers(); | ||
myHeaders.append("Authorization", `Bearer ${access_token}`); | ||
|
@@ -58,15 +60,20 @@ export default function AppView({ setDarkMode }){ | |
const apiResponse = await fetch(`${process.env.PUBLIC_URL}/api/data/${queuesToLoad}`, requestOptions); | ||
const queueJson = await apiResponse.json(); | ||
|
||
setQueues(queueJson); | ||
setIsLoading(false) | ||
if (apiResponse.ok) { | ||
setQueues(queueJson); | ||
setIsLoading(false) | ||
} else { | ||
setResponse({ status: apiResponse.status, message: apiResponse.statusText }) | ||
setError(true) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code checks for errors after a bad request is received. This should run before line 61. It should also be replaced with a guard statement fo |
||
})(); | ||
}, [selectedQueues, access_token, queueSelectorOpen]); | ||
|
||
// Populate items array. | ||
useEffect( _ => { | ||
useEffect(_ => { | ||
let tempItems = []; | ||
for (let queue of queues){ | ||
for (let queue of queues) { | ||
tempItems = tempItems.concat(queue.items); | ||
} | ||
setItems(tempItems); | ||
|
@@ -102,34 +109,50 @@ export default function AppView({ setDarkMode }){ | |
width: "40vw", | ||
} | ||
}, | ||
errorContainer: { | ||
display: "flex", | ||
flexDirection: "column", | ||
alignItems: "center", | ||
justifyContent: "center", | ||
marginTop: theme.spacing(6) | ||
} | ||
}); | ||
const classes = useStyles(); | ||
|
||
return( | ||
return ( | ||
<Box component={Paper} display="flex" square elevation={0}> | ||
<Box className={classes.leftCol}> | ||
<ItemTableAppBar title="webqueue2" setDarkMode={setDarkMode} /> | ||
<QueueSelector | ||
<QueueSelector | ||
open={queueSelectorOpen} | ||
setOpen={setQueueSelectorOpen} | ||
value={selectedQueues} | ||
setValue={setSelectedQueues} | ||
/> | ||
<ItemTable data={items} rowCanBeSelected={sidebarOpen} loading={isLoading}/> | ||
/>{error ? <Box classes={{ root: classes.errorContainer }}> | ||
<Typography variant="h1"> | ||
{response.status} | ||
</Typography> | ||
<Typography variant="h3"> | ||
Something went wrong. | ||
</Typography> | ||
<Typography variant="h5"> | ||
{response.message} | ||
</Typography> | ||
</Box> : <ItemTable data={items} rowCanBeSelected={sidebarOpen} loading={isLoading} />} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The information hierarchy emphasises the wrong information. On error, the API will send a JSON object with a |
||
</Box> | ||
<Box className={clsx(classes.rightCol, sidebarOpen && classes.rightColShift)}> | ||
{items.length === 0 ? null : | ||
<Route | ||
path="/:queue/:number" | ||
render={({ match }) => ( | ||
<> | ||
<ItemViewAppBar | ||
title={`${match.params.queue} ${match.params.number}`} | ||
<ItemViewAppBar | ||
title={`${match.params.queue} ${match.params.number}`} | ||
setSidebarOpen={setSidebarOpen} | ||
/> | ||
<ItemView | ||
queue={match.params.queue} | ||
number={match.params.number} | ||
<ItemView | ||
queue={match.params.queue} | ||
number={match.params.number} | ||
/> | ||
</> | ||
)} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generic variable name. Replace with
errorMessage
and set to an initial value of empty string. Populate the message later if need be.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To clarify, do you mean to replace both of these lines with one variable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After sleeping on it, methinks line 22 can be nixed.