|
| 1 | +// import R from 'ramda' |
| 2 | + |
| 3 | +import { |
| 4 | + makeDebugger, |
| 5 | + $solver, |
| 6 | + asyncRes, |
| 7 | + asyncErr, |
| 8 | + holdPage, |
| 9 | + unholdPage, |
| 10 | + ERR, |
| 11 | + EVENT, |
| 12 | +} from '../../utils' |
| 13 | +import SR71 from '../../utils/network/sr71' |
| 14 | + |
| 15 | +import S from './schema' |
| 16 | + |
| 17 | +const sr71$ = new SR71({ |
| 18 | + resv_event: [EVENT.USER_LISTER_OPEN], |
| 19 | +}) |
| 20 | + |
| 21 | +let sub$ = null |
| 22 | + |
| 23 | +/* eslint-disable no-unused-vars */ |
| 24 | +const debug = makeDebugger('L:UserLister') |
| 25 | +/* eslint-enable no-unused-vars */ |
| 26 | + |
| 27 | +let store = null |
| 28 | + |
| 29 | +export function onClose() { |
| 30 | + store.markState({ show: false }) |
| 31 | + unholdPage() |
| 32 | +} |
| 33 | + |
| 34 | +const loadUsers = () => { |
| 35 | + sr71$.query(S.pagedUsers, { filter: { page: 1, size: 20 } }) |
| 36 | +} |
| 37 | + |
| 38 | +// ############################### |
| 39 | +// Data & Error handlers |
| 40 | +// ############################### |
| 41 | + |
| 42 | +const DataSolver = [ |
| 43 | + { |
| 44 | + match: asyncRes(EVENT.USER_LISTER_OPEN), |
| 45 | + action: () => { |
| 46 | + loadUsers() |
| 47 | + store.markState({ show: true }) |
| 48 | + holdPage() |
| 49 | + }, |
| 50 | + }, |
| 51 | + { |
| 52 | + match: asyncRes('pagedUsers'), |
| 53 | + action: ({ pagedUsers }) => store.markState({ pagedUsers }), |
| 54 | + }, |
| 55 | +] |
| 56 | +const ErrSolver = [ |
| 57 | + { |
| 58 | + match: asyncErr(ERR.CRAPHQL), |
| 59 | + action: ({ details }) => { |
| 60 | + debug('ERR.CRAPHQL -->', details) |
| 61 | + }, |
| 62 | + }, |
| 63 | + { |
| 64 | + match: asyncErr(ERR.TIMEOUT), |
| 65 | + action: ({ details }) => { |
| 66 | + debug('ERR.TIMEOUT -->', details) |
| 67 | + }, |
| 68 | + }, |
| 69 | + { |
| 70 | + match: asyncErr(ERR.NETWORK), |
| 71 | + action: ({ details }) => { |
| 72 | + debug('ERR.NETWORK -->', details) |
| 73 | + }, |
| 74 | + }, |
| 75 | +] |
| 76 | + |
| 77 | +export function init(_store) { |
| 78 | + if (store) return false |
| 79 | + store = _store |
| 80 | + |
| 81 | + debug(store) |
| 82 | + if (sub$) sub$.unsubscribe() |
| 83 | + sub$ = sr71$.data().subscribe($solver(DataSolver, ErrSolver)) |
| 84 | +} |
0 commit comments