Skip to content

Commit

Permalink
Dashboard nextEvent widget working // Add setTimeout for magicLink page
Browse files Browse the repository at this point in the history
  • Loading branch information
sarL3y committed Jan 21, 2020
1 parent aee9d15 commit 526fc55
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 79 deletions.
154 changes: 83 additions & 71 deletions client/src/pages/AdminDashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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 ? (
<div className="flex-container">
<div className="dashboard">
<div className="dashboard-headers">
<h3>Hi, {auth.user.name.firstName}</h3>
</div>

<div className="dashboard-warning">
<p>You have an event coming up:</p>
</div>

<div className="warning-event">
<div className="warning-event-headers">
<h4>HackforLA Westside</h4>
<p>Monday, January 20th</p>
</div>
<div className="warning-event-toggle">
{event && isCheckInReady === false ?
(
<Link
// to={`/events/${event._id}`}
className="dashboard-nav-button fill-green"
onClick={e => setCheckInReady(e)}>
OPEN CHECK-IN
</Link>
) : (
<Link
// to={`/events/${event._id}`}
className="dashboard-nav-button fill-red"
onClick={e => setCheckInReady(e)}>
CLOSE CHECK-IN
</Link>
)
}
<div className="dashboard">
<div className="dashboard-headers">
<h3>Hi, {auth.user.name.firstName}</h3>
</div>

</div>

{nextEvent[0] ? (
<>
<div className="dashboard-warning">
<p>You have an event coming up:</p>
</div>

<div className="warning-event">
<div className="warning-event-headers">
<h4>{nextEvent[0].name}</h4>
<p>{nextEvent[0].date}</p>
</div>
<div className="warning-event-toggle">
{nextEvent[0] && isCheckInReady === false ?
(
<Link
to={`/events/${nextEvent[0]._id}`}
className="dashboard-nav-button fill-green"
onClick={e => setCheckInReady(e, nextEvent[0]._id)}>
OPEN CHECK-IN
</Link>
) : (
<Link
to={`/events/${nextEvent[0]._id}`}
className="dashboard-nav-button fill-red"
onClick={e => setCheckInReady(e, nextEvent[0]._id)}>
CLOSE CHECK-IN
</Link>
)
}
</div>
</div>
</>
) : (
<div>No events coming up!</div>
)}

<div className="dashboard-nav">
{events && users ? (
<div className="dashboard-nav-row">
Expand Down
16 changes: 8 additions & 8 deletions client/src/pages/MagicLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
4 changes: 4 additions & 0 deletions client/src/sass/Home.scss
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,8 @@
// z-index: 0;
// overflow: hidden;
// }

.home-buttons {
width: 90%;
}
}

0 comments on commit 526fc55

Please sign in to comment.