on fbcd6b6 help sign in
bridge/src/store/lib/useDetailsStore.js
51 lines 1.2 kB history raw
1import { useState, useCallback } from 'react';
2import Maybe from 'folktale/maybe';
3import * as azimuth from 'azimuth-js';
5import { useNetwork } from '../network';
7export default function useDetailsStore() {
8 const { contracts } = useNetwork();
9 const [detailsCache, _setDetailsCache] = useState({});
11 const addToDetails = useCallback(
12 entry =>
13 _setDetailsCache(cache => ({
14 ...cache,
15 ...entry,
16 })),
17 [_setDetailsCache]
18 );
20 // TODO: refactor detailsCache access to use accessor like bithday
21 // Maybe<{}>
22 const getDetails = useCallback(
23 point =>
24 point in detailsCache ? Maybe.Just(detailsCache[point]) : Maybe.Nothing(),
25 [detailsCache]
26 );
28 const syncDetails = useCallback(
29 async point => {
30 const _contracts = contracts.getOrElse(null);
31 if (!_contracts) {
32 return;
33 }
35 // fetch point details
36 const details = await azimuth.azimuth.getPoint(_contracts, point);
37 addToDetails({
38 [point]: details,
39 });
40 },
41 [contracts, addToDetails]
42 );
44 return {
45 // TODO: refactor accessors to use getDetails instead of
46 // touching the cache directly
47 pointCache: detailsCache,
48 getDetails,
49 syncDetails,
50 };