forked from kieran/vaxme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.coffee
84 lines (68 loc) · 2.39 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
GEO_TIMEOUT = 5000
import React from "react"
import { render } from "react-dom"
import axios from 'axios'
import throttle from 'underscore-es/throttle'
import './styles'
# provide GTM fallback
window.gtag ?= -> window.dataLayer?.push arguments
import providers from '/data/providers'
# 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 }
getLocation = ->
position = await new Promise (resolve, reject)->
navigator.geolocation.getCurrentPosition resolve, reject, timeout: GEO_TIMEOUT
{latitude, longitude} = position.coords
{latitude, longitude}
getPosalCode = (lat, lng)->
{ data } = await axios.get "#{process.env.API_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 =
locating: false
geoError: null
postal_code: null
birth_year: null
# postal_code: 'M6K'
# birth_year: 1979
componentDidMount: ->
@autoLocate()
fixVh()
window.addEventListener 'resize', throttle fixVh, 200
autoLocate: =>
try
@setState locating: true
{latitude, longitude} = await getLocation()
if latitude and longitude and postal_code = await getPosalCode latitude, longitude
gtag 'event', 'postal_code-select-auto', event_category: 'engagement', event_label: postal_code
@setPostalCode postal_code
catch err
# retry a timeout once
setTimeout @autoLocate.bind(@), 0 if err.TIMEOUT unless @state.geoError
@setState geoError: err.message
finally
@setState locating: false
setPostalCode: (postal_code)=>
@setState { postal_code }
setBirthYear: (birth_year)=>
birth_year = parseFloat birth_year
@setState { birth_year }
render: ->
<Application
providers={providers}
postal_code={@state.postal_code}
birth_year={@state.birth_year}
setPostalCode={@setPostalCode}
setBirthYear={@setBirthYear}
autoLocate={@autoLocate}
locating={@state.locating}
/>
render <App />, document.getElementById "Application"