Skip to content

Commit

Permalink
Merge branch 'Feature-Load-Item-Headers-Only' of github.itap.purdue.e…
Browse files Browse the repository at this point in the history
…du:ECN/webqueue2 into Feature-Load-Item-Headers-Only
  • Loading branch information
wrigh393 committed Feb 23, 2021
2 parents c73fb0e + 27f9fba commit 4b12f9e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/components/AppView/AppView.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import ItemViewAppBar from "../ItemViewAppBar/";
import ItemView from "../ItemView/";
import QueueSelector from "../QueueSelector/";
import { useToken } from "../AuthProvider/";
import { useItem, useItemSetter } from "../ItemProvider/";

export default function AppView({ setDarkMode }){
const [activeItem, setActiveItem] = useState({});
const activeItem = useItem();
const setActiveItem = useItemSetter();
const [sidebarOpen, setSidebarOpen] = useState(false);
const [queues, setQueues] = useState([]);
const [items, setItems] = useState([]);
Expand Down
29 changes: 29 additions & 0 deletions src/components/ItemProvider/ItemProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React, { useState, createContext, useContext, useEffect } from "react";

const ItemContext = createContext();
const ItemSetterContext = createContext();

export const useItem = () => useContext(ItemContext);
export const useItemSetter = () => useContext(ItemSetterContext);



export default function ItemProvider({ children }) {
const [item, setItem] = useState( {} );

useEffect( _ => console.log("Current Item is: ", item));

const setItemWrapper = (newItem) => {
console.log("Old Item: ", item);
setItem(newItem);
console.log("Set new item.");
}

return (
<ItemContext.Provider value={item}>
<ItemSetterContext.Provider value={setItemWrapper}>
{children}
</ItemSetterContext.Provider>
</ItemContext.Provider>
);
};
41 changes: 41 additions & 0 deletions src/components/ItemProvider/ItemProvider.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Utility component that uses [React Contexts](https://reactjs.org/docs/context.html) [React Stateful Variables](https://reactjs.org/docs/hooks-state.html) to provide global access to the active item object.

Two functions are exported:

Function | Descrioption
- | -
`useItem` | Returns a reference to the state variable holding the current Item. Defaults to `false`.
`useItemSetter` | Returns a reference to the state variable update function.

For an in depth explanation of this pattern, see [this GitHub comment](https://github.itap.purdue.edu/ECN/webqueue2/issues/15#issuecomment-341).


## Usage
```jsx static
// App
<ItemProvider>
<SomeComponent />
</ItemProvider>
```

```jsx static
// SomeComponent
import { useEffect } from "react";
import { useItem, useItemSetter } from "ItemProvider";

const activeItem = useItem();
const setActiveItem = useItemSetter();

useEffect(
let item = someFuncToGetItem();
setActiveItem(item);
);

return(
{
item
? <p>{`${activeItem.queue} ${activeItem.number} was last updated ${activeItem.lastUpdated}.`}</p>
: <p>No item is currently loaded.</p>
}
);
```
1 change: 1 addition & 0 deletions src/components/ItemProvider/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {default, useItem, useItemSetter } from "./ItemProvider";
10 changes: 7 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { CssBaseline } from '@material-ui/core';
import { BrowserRouter as Router } from 'react-router-dom';
import { CookiesProvider } from "react-cookie";
import AuthProvider from "./components/AuthProvider/";
import ItemProvider from "./components/ItemProvider/";


export const history = createBrowserHistory({
basename: process.env.PUBLIC_URL
Expand All @@ -17,9 +19,11 @@ ReactDOM.render(
<CssBaseline />
<CookiesProvider>
<AuthProvider>
<Router>
<App />
</Router>
<ItemProvider>
<Router>
<App />
</Router>
</ItemProvider>
</AuthProvider>
</CookiesProvider>
</React.StrictMode>,
Expand Down

0 comments on commit 4b12f9e

Please sign in to comment.