-
Notifications
You must be signed in to change notification settings - Fork 0
Feature handle api status #220
Merged
Justin Tyler Campbell (campb303)
merged 8 commits into
staging
from
feature-handle-api-status
Jul 14, 2021
+64
−22
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
74d139c
Fixed missing bracket
wrigh393 dc4a4cf
Implemented code for getting response info when error occurs
wrigh393 1931506
Implemented UI for showing error code and status message on failed AP…
wrigh393 305d694
Added appropriate icon for error UI; reformatted error UI text to emp…
wrigh393 1fad3bd
Remove unused state variable
campb303 759b164
Revert "Remove unused state variable"
campb303 99e8086
Refactor error message into its own component and cleanup ternary
campb303 861aea2
Refactor error message state variable to support dynamic error messag…
campb303 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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/"; | ||
|
|
@@ -9,47 +9,50 @@ import ItemViewAppBar from "../ItemViewAppBar/"; | |
| import ItemView from "../ItemView/"; | ||
| import QueueSelector from "../QueueSelector/"; | ||
| import { useToken } from "../AuthProvider/"; | ||
| import ErrorOutlineIcon from '@material-ui/icons/ErrorOutline'; | ||
|
|
||
| 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 +61,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) | ||
| } | ||
|
Collaborator
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 +110,51 @@ export default function AppView({ setDarkMode }){ | |
| width: "40vw", | ||
| } | ||
| }, | ||
| errorContainer: { | ||
| display: "flex", | ||
| flexDirection: "column", | ||
| alignItems: "center", | ||
| justifyContent: "center", | ||
| marginTop: theme.spacing(6) | ||
| }, | ||
| errorIcon: { | ||
| fontSize: "10rem" | ||
| } | ||
| }); | ||
| 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 }}> | ||
| <ErrorOutlineIcon classes={{ root: classes.errorIcon }} /> | ||
| <Typography align="center" variant="h3"> | ||
| {`${response.message}`} | ||
| </Typography> | ||
| <Typography align="center" variant="h5"> | ||
| {`Error Code:${response.status}`} | ||
| </Typography> | ||
| </Box> : <ItemTable data={items} rowCanBeSelected={sidebarOpen} loading={isLoading} />} | ||
| </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} | ||
| /> | ||
| </> | ||
| )} | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
errorMessageand 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.