/
ScheduleTZFilter.js
66 lines (60 loc) · 1.61 KB
/
ScheduleTZFilter.js
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
import React from 'react'
import p from 'prop-types'
import { urlParamSelector } from '../selectors'
import { setURLParam } from '../actions'
import gql from 'graphql-tag'
import { FormControlLabel, Switch } from '@material-ui/core'
import { oneOfShape } from '../util/propTypes'
import { useQuery } from 'react-apollo'
import { useSelector, useDispatch } from 'react-redux'
const tzQuery = gql`
query($id: ID!) {
schedule(id: $id) {
id
timeZone
}
}
`
export function ScheduleTZFilter(props) {
const params = useSelector(urlParamSelector)
const zone = params('tz', 'local')
const dispatch = useDispatch()
const setZone = value => dispatch(setURLParam('tz', value, 'local'))
const { data, loading, error } = useQuery(tzQuery, {
pollInterval: 0,
variables: { id: props.scheduleID },
})
let label, tz
if (error) {
label = 'Error: ' + (error.message || error)
} else if (loading) {
label = 'Fetching timezone information...'
} else {
tz = data.schedule.timeZone
label = props.label ? props.label(tz) : `Show times in ${tz}`
}
return (
<FormControlLabel
control={
<Switch
checked={zone !== 'local'}
onChange={e => setZone(e.target.checked ? tz : 'local')}
value={tz}
disabled={Boolean(loading || error)}
/>
}
label={label}
/>
)
}
ScheduleTZFilter.propTypes = {
label: p.func,
// one of scheduleID or scheduleTimeZone must be specified
_tz: oneOfShape({
scheduleID: p.string,
scheduleTimeZone: p.string,
}),
// provided by connect
zone: p.string,
setZone: p.func,
}