From 526fc55e9d58cedfe5a04037616a12152380aa7a Mon Sep 17 00:00:00 2001 From: sarL3y Date: Tue, 21 Jan 2020 14:20:10 -0800 Subject: [PATCH] Dashboard nextEvent widget working // Add setTimeout for magicLink page --- client/src/pages/AdminDashboard.js | 154 ++++++++++++++++------------- client/src/pages/MagicLink.js | 16 +-- client/src/sass/Home.scss | 4 + 3 files changed, 95 insertions(+), 79 deletions(-) diff --git a/client/src/pages/AdminDashboard.js b/client/src/pages/AdminDashboard.js index 17e8898c8..cedcb4175 100644 --- a/client/src/pages/AdminDashboard.js +++ b/client/src/pages/AdminDashboard.js @@ -10,54 +10,64 @@ import '../sass/Dashboard.scss'; const AdminDashboard = (props) => { const [events, setEvents] = useState([]); + const [nextEvent, setNextEvent] = useState([]); + const [isCheckInReady, setIsCheckInReady] = useState(); const [users, setUsers] = useState([]); - const [user, setUser] = useState([]); const [tabSelected, setTabSelected] = useState(); const [optionSelected, setOptionSelected] = useState("left"); const [eventsIsSelected, setEventsIsSelected] = useState(false); const [usersIsSelected, setUsersIsSelected] = useState(false); const [isLoading, setIsLoading] = useState(false); - const [event, setEvent] = useState([]); - const [isCheckInReady, setIsCheckInReady] = useState(false); const auth = useAuth(); - async function setCheckInReady(e) { - e.preventDefault(); - - // try { - // // const payload = { checkInReady: true }; - - // await fetch(`/api/events/${props.match.params.id}`, { - // method: 'PATCH', - // headers: { - // "Content-Type": "application/json" - // }, - // // body: JSON.stringify(payload) - // }) - // .then(response => { - // if (response.ok) { - // setEvent(event); - // setIsCheckInReady(!isCheckInReady); - // } - // }); - - // } catch(error) { - // // setIsError(error); - // setIsLoading(!isLoading); - // } + async function getNextEvent() { + + try { + const events = await fetch('/api/events'); + const eventsJson = await events.json(); + + const dates = eventsJson.map(event => { + return Date.parse(event.date); + }); + + const nextDate = new Date(Math.max.apply(null, dates)); + const nextDateUtc = new Date(nextDate).toISOString(); + + const nextEvent = eventsJson.filter(event => { + const eventDate = new Date(event.date).toISOString(); + return eventDate === nextDateUtc; + }); + + setIsCheckInReady(nextEvent[0].checkInReady); + setNextEvent(nextEvent); + + } catch(error) { + // setIsError(error); + // setIsLoading(!isLoading); + console.log(error); + } } - async function isAccessLevelAdmin(e) { + async function setCheckInReady(e, nextEventId) { e.preventDefault(); try { - const res = await fetch(`/api/users/${props.match.params.id}`); - const resJson = await res.json(); + await fetch(`/api/events/${nextEventId}`, { + method: 'PATCH', + headers: { + "Content-Type": "application/json" + } + }) + .then(response => { + if (response.ok) { + setIsCheckInReady(!isCheckInReady); + } + }); - setUser(resJson); } catch(error) { - alert(error); + // setIsError(error); + // setIsLoading(!isLoading); } } @@ -92,52 +102,54 @@ const AdminDashboard = (props) => { } useEffect(() => { - // console.log(`Admin dashboard useffect isLoggedIn: ${isLoggedIn}`); - console.log(`Admin dashboard says auth is: ${auth.user}`); - // if(!isLoggedIn) { - // props.history.replace("/login"); - // } + getNextEvent(); }, []); return ( auth.user ? (
-
-
-

Hi, {auth.user.name.firstName}

-
- -
-

You have an event coming up:

-
- -
-
-

HackforLA Westside

-

Monday, January 20th

-
-
- {event && isCheckInReady === false ? - ( - setCheckInReady(e)}> - OPEN CHECK-IN - - ) : ( - setCheckInReady(e)}> - CLOSE CHECK-IN - - ) - } +
+
+

Hi, {auth.user.name.firstName}

- -
+ + {nextEvent[0] ? ( + <> +
+

You have an event coming up:

+
+ +
+
+

{nextEvent[0].name}

+

{nextEvent[0].date}

+
+
+ {nextEvent[0] && isCheckInReady === false ? + ( + setCheckInReady(e, nextEvent[0]._id)}> + OPEN CHECK-IN + + ) : ( + setCheckInReady(e, nextEvent[0]._id)}> + CLOSE CHECK-IN + + ) + } +
+
+ + ) : ( +
No events coming up!
+ )} +
{events && users ? (
diff --git a/client/src/pages/MagicLink.js b/client/src/pages/MagicLink.js index 8b8dc2817..d258f61a0 100644 --- a/client/src/pages/MagicLink.js +++ b/client/src/pages/MagicLink.js @@ -20,18 +20,18 @@ const MagicLink = (props) => { // } // } - // function forwardRequest() { - // return props.history.push('/user'); - // } + function forwardRequest() { + + + } useEffect(() => { // fetchData(); + let timer = setTimeout(() => { + props.history.push('/'); + }, 6000); - // setTimeout(forwardRequest - // , 5000); - - - + return () => clearTimeout(timer); }, []); return ( diff --git a/client/src/sass/Home.scss b/client/src/sass/Home.scss index 9d3f4ce63..cc32c4592 100644 --- a/client/src/sass/Home.scss +++ b/client/src/sass/Home.scss @@ -77,4 +77,8 @@ // z-index: 0; // overflow: hidden; // } + + .home-buttons { + width: 90%; + } } \ No newline at end of file