Archived
added context-related data forwarding
This commit is contained in:
1 parent
7961cba168
commit
12611b2158
3 files changed
+49
-18
No files matched your search
+37
-11
@@ -1,3 +1,4 @@
|
|||||||
|
import { useState } from "react";
|
||||||
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
|
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
|
||||||
import { Home } from "./pages/Home";
|
import { Home } from "./pages/Home";
|
||||||
import { About } from "./pages/About";
|
import { About } from "./pages/About";
|
||||||
@@ -5,19 +6,44 @@ import { Users } from "./pages/Users";
|
|||||||
import { NotFound } from "./pages/NotFound";
|
import { NotFound } from "./pages/NotFound";
|
||||||
import Logo from "./components/Logo";
|
import Logo from "./components/Logo";
|
||||||
import { CreateEvent } from "./pages/CreateEvent";
|
import { CreateEvent } from "./pages/CreateEvent";
|
||||||
|
import { Event } from "./pages/Event";
|
||||||
|
import { EVENT_CONTEXT } from "./constants";
|
||||||
|
|
||||||
|
export const App = (props) => {
|
||||||
|
const [context, setContext] = useState({
|
||||||
|
events: props.events,
|
||||||
|
updateEvents: function (events = []) {
|
||||||
|
if (!Array.isArray(events)) throw new TypeError("Events must come in array");
|
||||||
|
setContext({
|
||||||
|
events: events,
|
||||||
|
updateEvents: this.updateEvents,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
export const App = () => {
|
|
||||||
return (
|
return (
|
||||||
<Router>
|
<EVENT_CONTEXT.Provider value={context}>
|
||||||
<Logo />
|
<Router>
|
||||||
<Switch>
|
<Logo />
|
||||||
<Route exact path="/about" component={About} />
|
<Switch>
|
||||||
<Route exact path="/users" component={Users} />
|
<Route exact path="/about" component={About} />
|
||||||
<Route exact path="/" component={Home} />
|
<Route exact path="/users" component={Users} />
|
||||||
<Route exact path="/create" component={CreateEvent} />
|
<Route exact path="/">
|
||||||
<Route component={NotFound} />
|
<Home />
|
||||||
</Switch>
|
</Route>
|
||||||
</Router>
|
<Route exact path="/create" component={CreateEvent} />
|
||||||
|
{Array.isArray(props.events) &&
|
||||||
|
props.events.map((e) => {
|
||||||
|
return (
|
||||||
|
<Route key={e._id} exact path={`/${e._id}`}>
|
||||||
|
<Event id={e._id}></Event>
|
||||||
|
</Route>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
<Route component={NotFound} />
|
||||||
|
</Switch>
|
||||||
|
</Router>
|
||||||
|
</EVENT_CONTEXT.Provider>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -2,5 +2,5 @@ import { createContext } from "react";
|
|||||||
import { APIClient } from "./utils/APIClient";
|
import { APIClient } from "./utils/APIClient";
|
||||||
|
|
||||||
export const BACKEND_URL = "http://localhost:3000";
|
export const BACKEND_URL = "http://localhost:3000";
|
||||||
export const EVENT_CONTEXT = createContext([]);
|
export const EVENT_CONTEXT = createContext();
|
||||||
export const API_CLIENT = new APIClient(BACKEND_URL);
|
export const API_CLIENT = new APIClient(BACKEND_URL);
|
||||||
+11
-6
@@ -3,10 +3,15 @@ import ReactDOM from "react-dom";
|
|||||||
import "./index.scss";
|
import "./index.scss";
|
||||||
import { App } from "./App.js";
|
import { App } from "./App.js";
|
||||||
import "bootstrap/dist/css/bootstrap.min.css";
|
import "bootstrap/dist/css/bootstrap.min.css";
|
||||||
|
import { API_CLIENT } from "./constants";
|
||||||
|
|
||||||
ReactDOM.render(
|
(async () => {
|
||||||
<React.StrictMode>
|
const dbEvents = await API_CLIENT.getEvents();
|
||||||
<App />
|
|
||||||
</React.StrictMode>,
|
ReactDOM.render(
|
||||||
document.getElementById("root")
|
<React.StrictMode>
|
||||||
);
|
<App events={dbEvents} />
|
||||||
|
</React.StrictMode>,
|
||||||
|
document.getElementById("root")
|
||||||
|
);
|
||||||
|
})();
|
||||||
Reference in new issue
Block a user