Skip to content

Commit f0c0304

Browse files
authored
client: Read preferences (#33)
1 parent 704bc85 commit f0c0304

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

src/v2/components/PotentialFireList.jsx

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
// TODO:
1818
// - Handle updates to existing fires.
1919
// - Handle fires aging out (on a timer?).
20-
// - Implement user preferences.
21-
// - Implement search params.
22-
// - Implement notifications.
20+
// - Write user preferences.
2321
// - Implement camera ID pinning.
2422
// - Implement adaptive/mobile version.
2523

@@ -33,6 +31,7 @@ import getEventSource from '../modules/getEventSource.mjs'
3331
import hasAngleOfFire from '../modules/hasAngleOfFire.mjs'
3432
import isPolygonWithinRegion from '../modules/isPolygonWithinRegion.mjs'
3533
import parseRegion from '../modules/parseRegion.mjs'
34+
import query from '../modules/query.mjs'
3635

3736
import FireList from './FireList.jsx'
3837

@@ -156,7 +155,7 @@ export default function PotentialFireList(props) {
156155
return window.location.reload()
157156
}
158157

159-
const {current: allFires} = allFiresRef
158+
const {current: allFires} = allFiresRefx
160159
const {current: firesByKey} = firesByKeyRef
161160
const key = getCameraKey(fire)
162161

@@ -186,24 +185,47 @@ export default function PotentialFireList(props) {
186185
}, [handleNotification, includesAllFires, region, updateFires])
187186

188187
useEffect(() => {
189-
const searchParams = new URLSearchParams(window.location.search)
190-
const notifyParam = searchParams.get('notify')
191-
const regionParam = searchParams.get('latLong')
188+
// NOTE: We expect /api/getPreferences to 401 so we set `prefs` if the
189+
// request is successful, but ignore it otherwise.
190+
let prefs
191+
query.get('/api/getPreferences').then((r) => (prefs = r.data)).finally(() => {
192+
const searchParams = new URLSearchParams(window.location.search)
193+
const notifyParam = searchParams.get('notify')
194+
const regionParam = searchParams.get('latLong')
195+
const nextState = {region: null, shouldNotify: null}
196+
197+
if (regionParam != null) {
198+
try {
199+
nextState.region = parseRegion(regionParam)
200+
} catch (error) {
201+
report(error)
202+
}
203+
}
192204

193-
if (notifyParam != null) {
194-
if (/true|false/.test(notifyParam)) {
195-
setShouldNotify(notifyParam === 'true')
205+
if (notifyParam != null) {
206+
if (/true|false/.test(notifyParam)) {
207+
nextState.shouldNotify = notifyParam === 'true'
208+
}
196209
}
197-
}
198210

199-
if (regionParam != null) {
200-
try {
201-
setRegion(parseRegion(regionParam))
202-
} catch (error) {
203-
report(error)
204-
setRegion(null)
211+
if (prefs != null) {
212+
if (nextState.region == null) {
213+
nextState.region = {
214+
north: prefs.region.topLat,
215+
south: prefs.region.bottomLat,
216+
west: prefs.region.leftLong,
217+
east: prefs.region.rightLong
218+
}
219+
}
220+
221+
if (nextState.shouldNotify == null) {
222+
nextState.shouldNotify = prefs.webNotify
223+
}
205224
}
206-
}
225+
226+
setRegion(nextState.region)
227+
setShouldNotify(nextState.shouldNotify)
228+
})
207229
}, [])
208230

209231
useEffect(() => {

0 commit comments

Comments
 (0)