From 12611b2158e32374a79ffef8826f34de1ed3b70c Mon Sep 17 00:00:00 2001 From: GRZ4NA Date: Sun, 3 Oct 2021 16:45:35 +0200 Subject: [PATCH] added context-related data forwarding --- src/App.js | 48 +++++++++++++++++++++++++++++++++++++----------- src/constants.js | 2 +- src/index.js | 17 +++++++++++------ 3 files changed, 49 insertions(+), 18 deletions(-) diff --git a/src/App.js b/src/App.js index 65bf2d3..083322d 100644 --- a/src/App.js +++ b/src/App.js @@ -1,3 +1,4 @@ +import { useState } from "react"; import { BrowserRouter as Router, Switch, Route } from "react-router-dom"; import { Home } from "./pages/Home"; import { About } from "./pages/About"; @@ -5,19 +6,44 @@ import { Users } from "./pages/Users"; import { NotFound } from "./pages/NotFound"; import Logo from "./components/Logo"; 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 ( - - - - - - - - - - + + + + + + + + + + + {Array.isArray(props.events) && + props.events.map((e) => { + return ( + + + + ); + })} + + + + ); }; diff --git a/src/constants.js b/src/constants.js index 419ca5a..ce05f87 100644 --- a/src/constants.js +++ b/src/constants.js @@ -2,5 +2,5 @@ import { createContext } from "react"; import { APIClient } from "./utils/APIClient"; 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); diff --git a/src/index.js b/src/index.js index fb6f005..74823c3 100644 --- a/src/index.js +++ b/src/index.js @@ -3,10 +3,15 @@ import ReactDOM from "react-dom"; import "./index.scss"; import { App } from "./App.js"; import "bootstrap/dist/css/bootstrap.min.css"; +import { API_CLIENT } from "./constants"; -ReactDOM.render( - - - , - document.getElementById("root") -); +(async () => { + const dbEvents = await API_CLIENT.getEvents(); + + ReactDOM.render( + + + , + document.getElementById("root") + ); +})();