Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split pass and upcoming events #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/core/util.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import fecha from 'fecha'

export function truncate(text, len = 100, elipsis = '...') {
if (text.length > len) {
return text.substr(0, len - elipsis.length) + elipsis
Expand All @@ -7,3 +9,11 @@ export function truncate(text, len = 100, elipsis = '...') {
}

export const quote = text => `“${text.trim()}”`

export function dateParser(dateObj) {
if (!dateObj || !dateObj.date || !dateObj.month || !dateObj.year) return null
return fecha.parse(
`${dateObj.date} ${dateObj.month} ${dateObj.year}`,
'D M YYYY',
)
}
25 changes: 24 additions & 1 deletion src/ducks/events.selector.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {createSelector} from 'reselect'
import Fuse from 'fuse.js'

import { dateParser } from '../core/util'

const searchOptions = {
shouldSort: true,
keys: [
Expand Down Expand Up @@ -50,6 +52,27 @@ function filterByFavorites(events, isFavOnly, favorites) {
return events
}

function splitEvents(events) {
const startOfToday = new Date()
startOfToday.setHours(0, 0, 0, 0)
const upcomingEvents = events
.filter(event => dateParser(event.start) - startOfToday >= 0)
.sort((a, b) => {
const aDate = dateParser(a.start)
const bDate = dateParser(b.start)
if (aDate - bDate > 0) return 1
else if (aDate - bDate < 0) return -1
return 0
})
const pastEvents = events.filter(
event => dateParser(event.start) - startOfToday < 0,
)
return {
pastEvents,
upcomingEvents,
}
}

const eventsSelector = createSelector(
state => state.app.events,
state => state.app.search,
Expand All @@ -60,7 +83,7 @@ const eventsSelector = createSelector(
events = filterByTags(events, tagFilters)
events = filterBySearch(events, query)
events = filterByFavorites(events, isFavOnly, favs)

events = splitEvents(events)
return events
},
)
Expand Down
16 changes: 12 additions & 4 deletions src/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import React from 'react'
import {connect} from 'react-redux'
import styled from 'react-emotion'
import fecha from 'fecha'

import Search from '../components/Search'
import TagList from '../components/TagList'
import EventCard from '../components/EventCard'

import eventsSelector from '../ducks/events.selector'
import { dateParser } from '../core/util'

const Container = styled.div`
display: flex;
Expand All @@ -27,9 +29,10 @@ const Container = styled.div`
const Title = styled.h1`
margin: 0;

color: white;
color: black;
font-weight: 300;
font-size: 1.38em;
margin-bottom: 20px;
`

const List = styled.div`
Expand All @@ -41,16 +44,21 @@ const List = styled.div`
max-width: 1000px;
`

const Landing = ({events}) => (
const Landing = ({upcomingEvents, pastEvents}) => (
<Container>
<Title>Upcoming Events</Title>
<List>
{events.map(event => <EventCard key={event.id} data={event} />)}
{upcomingEvents.map(event => <EventCard key={event.id} data={event} />)}
</List>
<Title>Past Events</Title>
<List>
{pastEvents.map(event => <EventCard key={event.id} data={event} />)}
</List>
</Container>
)

const mapStateToProps = state => ({
events: eventsSelector(state),
...eventsSelector(state),
})

const enhance = connect(mapStateToProps)
Expand Down