-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from FCCColumbus/react-cal-poc-kl
React cal poc kl
- Loading branch information
Showing
9 changed files
with
4,131 additions
and
6,380 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,26 @@ | ||
import PropTypes from "prop-types"; | ||
import PropTypes from 'prop-types'; | ||
|
||
function Event({ event }) { | ||
// add id to event | ||
// const href = `/coding-${id + 1}.jpg` | ||
const formattedDateTime = new Intl.DateTimeFormat('en-US', { | ||
dateStyle: 'medium', | ||
timeStyle: 'short', | ||
}).format(event.dtstart.value); | ||
|
||
function Event({ event, id }) { | ||
const href = `/coding-${id + 1}.jpg` | ||
const formattedDateTime = new Intl.DateTimeFormat('en-US', { dateStyle: 'medium', timeStyle: 'short' }).format(event.time); | ||
/* | ||
We replace the "narrow non-breaking space" that appears before the AM/PM in some environments | ||
with a regular space to help prevent snapshot test failures. | ||
fwiw, it appears that this is a somewhat recent change in Node: https://www.reddit.com/r/webdev/comments/1054ve6/psa_node_1813_changed_datetime_formatting/ | ||
- [email protected], 2023 May 22 | ||
*/ | ||
const replacedNonBreakingSpace = formattedDateTime.replace(/\u202f/g, ' '); | ||
|
||
return ( | ||
<div className="event"> | ||
<img src={href} alt='people coding' className="event-img" /> | ||
<div className="info"> | ||
<h5>{event.name}</h5> | ||
{/* <img src={href} alt='people coding' className="event-img" /> */} | ||
<div className="event-info"> | ||
<h5>{event.summary.value}</h5> | ||
<table> | ||
<tbody> | ||
<tr> | ||
|
@@ -24,25 +29,34 @@ function Event({ event, id }) { | |
</tr> | ||
<tr> | ||
<th>location:</th> | ||
<td>{event.location}</td> | ||
{event.location && <td>{event.location.value}</td>} | ||
</tr> | ||
</tbody> | ||
</table> | ||
<p>{event.description}</p> | ||
<p>{event.description.value}</p> | ||
<div className="meet-up-register"> | ||
<a | ||
target="_blank" | ||
rel="noreferrer" | ||
href="https://www.meetup.com/register/" | ||
> | ||
Join Meetup.com | ||
</a> | ||
</div> | ||
</div> | ||
</div> | ||
) | ||
); | ||
} | ||
|
||
Event.propTypes = { | ||
event: PropTypes.shape({ | ||
name: PropTypes.string.isRequired, | ||
description: PropTypes.string.isRequired, | ||
time: PropTypes.objectOf(Date), | ||
location : PropTypes.string.isRequired, | ||
id : PropTypes.string.isRequired, | ||
summary: PropTypes.objectOf(PropTypes.string.isRequired), | ||
description: PropTypes.objectOf(PropTypes.string.isRequired), | ||
dtstart: PropTypes.objectOf(Date), | ||
location: PropTypes.objectOf(PropTypes.string.isRequired), | ||
uid: PropTypes.objectOf(PropTypes.string.isRequired), | ||
url: PropTypes.objectOf(PropTypes.string.isRequired), | ||
}).isRequired, | ||
id: PropTypes.number.isRequired, | ||
}; | ||
|
||
export default Event | ||
export default Event; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { useState, useEffect, useMemo, useCallback } from 'react'; | ||
|
||
const useCalendar = (events) => { | ||
const [selectedDate, setSelectedDate] = useState(new Date()); | ||
|
||
const dateOnly = (date) => | ||
new Date(date.getFullYear(), date.getMonth(), date.getDate()); | ||
|
||
const eventsByDateString = useMemo(() => { | ||
// Key: date string of date object | ||
// Value: Array of event objects | ||
const eventsDict = {}; | ||
events.forEach((event) => { | ||
const dateString = event.dtstart.value.toDateString(); | ||
if (!eventsDict[dateString]) { | ||
eventsDict[dateString] = []; | ||
} | ||
eventsDict[dateString].push(event); | ||
}); | ||
return eventsDict; | ||
}, [events]); | ||
|
||
const displayedEvents = useMemo( | ||
() => eventsByDateString[selectedDate.toDateString()] ?? [], | ||
[eventsByDateString, selectedDate] | ||
); | ||
|
||
useEffect(() => { | ||
const nearestFutureEventDate = (currentDate) => { | ||
// Binary search for nearest future date with an event | ||
// when there's no event today. | ||
// Assumes events is sorted chronologically. | ||
|
||
let left = 0; | ||
let right = events.length - 1; | ||
while (left < right) { | ||
const mid = Math.floor((left + right) / 2); | ||
const midDate = dateOnly(events[mid].dtstart.value); | ||
|
||
if (midDate.getTime() > currentDate.getTime()) { | ||
right = mid; | ||
} else { | ||
left = mid + 1; | ||
} | ||
} | ||
return dateOnly(events[left].dtstart.value); | ||
}; | ||
|
||
const initialSelectedDate = () => { | ||
// Returns today's date if there is an event. | ||
// If not, returns the nearest future date with an event. | ||
// If no future events, returns the last date with an event. | ||
|
||
const currentDate = dateOnly(new Date()); | ||
|
||
if (events.length === 0) { | ||
return currentDate; | ||
} | ||
// Check if today's date has an event | ||
if (eventsByDateString[currentDate.toDateString()]) { | ||
return currentDate; | ||
} | ||
|
||
// Check if today's date is after the last event date | ||
const lastEventDate = dateOnly(events[events.length - 1].dtstart.value); | ||
if (lastEventDate.getTime() < currentDate.getTime()) { | ||
return lastEventDate; | ||
} | ||
|
||
return nearestFutureEventDate(currentDate); | ||
}; | ||
setSelectedDate(initialSelectedDate()); | ||
}, [events, eventsByDateString]); | ||
|
||
const tileClassName = useCallback( | ||
({ date, view }) => | ||
view === 'month' && eventsByDateString[date.toDateString()] | ||
? 'event-tile' | ||
: '', | ||
[eventsByDateString] | ||
); | ||
|
||
return { selectedDate, setSelectedDate, displayedEvents, tileClassName }; | ||
}; | ||
|
||
export default useCalendar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { useState, useEffect } from 'react'; | ||
import ical from 'cal-parser'; | ||
|
||
const useEvents = () => { | ||
const [events, setEvents] = useState([]); | ||
|
||
const fetchData = async () => { | ||
try { | ||
const response = await fetch( | ||
'https://docs.google.com/document/d/1OcnWWt1qHaJWE-8v_FPtNO8DKviRQ7DMaqylupuvPXE/export?format=txt' | ||
); | ||
|
||
const content = await response.text(); | ||
return content; | ||
} catch (error) { | ||
return null; | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
const eventsData = async () => { | ||
try { | ||
const fetchedData = await fetchData(); | ||
const parsedData = ical.parseString(fetchedData); | ||
return parsedData.events; | ||
} catch (error) { | ||
return []; | ||
} | ||
}; | ||
eventsData().then((data) => { | ||
setEvents(data); | ||
}); | ||
}, []); | ||
|
||
return { events }; | ||
}; | ||
|
||
export default useEvents; |
Oops, something went wrong.