This repository was archived by the owner on May 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.tsx
More file actions
139 lines (127 loc) · 4.06 KB
/
index.tsx
File metadata and controls
139 lines (127 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { Component, h } from 'preact'
import { getEvents } from '../../api'
import Button from '../../components/button'
import DateDisplay from '../../components/date-display'
import Header from '../../components/header'
import Icon from '../../components/icon'
import List from '../../components/list'
import SearchInput, { SearchInputEvent } from '../../components/search-input'
import Spinner from '../../components/spinner'
import FRCEvent from '../../models/frc-event'
import Resolver from '../../resolver'
import {
eventTypeName,
getCoords,
getJWT,
hasValidJWT,
sortEvents
} from '../../utils'
import style from './style.css'
interface HomeProps {
events: FRCEvent[]
}
interface HomeState {
query: string
loggedIn: boolean
coords?: { lat: number; long: number }
}
const eventTypeClassMap = new Map<number, string>([
[5, style.dcmp],
[2, style.dcmp],
[3, style.cmp],
[4, style.cmp],
[99, style.off],
[100, style.pre]
])
const Home = () => (
<Resolver
data={{
events: getEvents()
}}
render={
class Home extends Component<HomeProps, HomeState> {
constructor() {
super()
this.state = { query: '', loggedIn: false }
}
componentWillMount() {
this.setState({ loggedIn: hasValidJWT(getJWT()) })
if ('geolocation' in navigator) {
getCoords((coords: { lat: number; long: number }) => {
this.setState({ coords })
})
}
}
queryChanged = (e: SearchInputEvent) => {
this.setState({ query: e.target.value })
}
logout = () => {
localStorage.removeItem('jwt')
this.setState({ loggedIn: false })
}
eventTypeClass = (eventType: number) => {
return eventTypeClassMap.get(eventType)
}
render({ events }: HomeProps, { query, loggedIn, coords }: HomeState) {
const matchingEvents = (events !== undefined ? events : []).filter(
e => e.name.toLowerCase().includes(query.toLowerCase())
)
const sortedEvents = sortEvents(matchingEvents, coords).slice(0, 25)
return (
<div class={style.home}>
<Header
contents={
<div class={style.headerContents}>
<span class={style.navigationDrawerButtonContainer}>
<a
class={style.navigationDrawerButton}
href="/leaderboard"
aria-label="Leaderboard"
>
<Icon fill="#FFF" icon="trophy" />
</a>
</span>
<SearchInput
onInput={this.queryChanged}
placeholder="Search for events"
value={query}
/>
{loggedIn ? (
<Button onClick={this.logout}>Log Out</Button>
) : (
<Button href="/login">Login</Button>
)}
</div>
}
/>
{events === undefined ? (
<Spinner />
) : events.length === 0 ? (
'No matching events'
) : (
<List>
{sortedEvents.map(e => (
<li key={e.key}>
<a href={`/events/${e.key}`}>
{e.shortName || e.name}
<div class={style.info}>
{eventTypeName(e.eventType) ? (
<span class={this.eventTypeClass(e.eventType)}>
{eventTypeName(e.eventType)}
</span>
) : null}
<DateDisplay date={e.parsedDate} />
</div>
</a>
</li>
))}
</List>
)}
</div>
)
}
}
}
/>
)
export default Home