-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.coffee
87 lines (70 loc) · 2.47 KB
/
index.coffee
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
import React from "react"
import { render } from "react-dom"
import axios from 'axios'
import throttle from 'underscore-es/throttle'
import './styles'
import './election/locales'
# provide GTM fallback
window.gtag ?= -> window.dataLayer?.push arguments
import { parties, date } from "/election"
# routes
import Application from '/routes/application'
do ->
if dsn = process.env.SENTRY_DSN_FRONTEND
Sentry = await import('@sentry/browser')
Sentry.init { dsn, environment: process.env.NODE_ENV, allowUrls: [ /votewell\.(ca|co\.uk)/ ] }
polls = await import('/election/polls.json')
# fuzzy riding name matcher
# removes weird dash / apostrophe mis-matches
matchRiding = (name='')->
ridings = (poll.riding for poll in polls)
match_name = name.replace /[^A-Za-z]/g, ''
for riding in ridings
return riding if match_name is riding.replace /[^A-Za-z]/g, ''
ridings[0]
getLocation = ->
position = await new Promise (resolve, reject)->
navigator.geolocation.getCurrentPosition resolve, reject, timeout: 5000
{latitude, longitude} = position.coords
{latitude, longitude}
getRiding = (lat, lng)->
{ data } = await axios.get "#{process.env.RIDING_URL}/#{lat.toFixed 2},#{lng.toFixed 2}"
data
fixVh = ->
document.documentElement.style.setProperty '--vh', "#{window.innerHeight * 0.01}px"
class App extends React.Component
constructor: ->
super arguments...
@state =
riding: null
locating: false
geoError: null
componentDidMount: ->
@autoLocate()
fixVh()
autoLocate: =>
try
@setState locating: true
{latitude, longitude} = await getLocation()
if latitude and longitude and riding = await getRiding latitude, longitude
gtag 'event', 'riding-select-auto', event_category: 'engagement', event_label: riding
@setRiding matchRiding riding
catch err
# retry a timeout once
setTimeout @autoLocate.bind(@), 0 if err.TIMEOUT unless @state.geoError
@setState geoError: err.message
@setRiding polls?[0]?.riding unless @state.riding
finally
@setState locating: false
setRiding: (riding)=>
@setState { riding }
render: ->
<Application
polls={polls}
parties={parties}
date={date}
riding={@state.riding}
setRiding={@setRiding}
locating={@state.locating}
/>
render <App />, document.getElementById "Application"