-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathevents.js
More file actions
156 lines (127 loc) · 3.7 KB
/
events.js
File metadata and controls
156 lines (127 loc) · 3.7 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import {all, take, call, put, select} from 'redux-saga/effects'
import {appName} from '../config'
import {Record, OrderedMap, OrderedSet} from 'immutable'
import firebase from 'firebase'
import {createSelector} from 'reselect'
import {fbDatatoEntities} from './utils'
/**
* Constants
* */
export const moduleName = 'events'
const prefix = `${appName}/${moduleName}`
export const FETCH_ALL_REQUEST = `${prefix}/FETCH_ALL_REQUEST`
export const FETCH_ALL_SUCCESS = `${prefix}/FETCH_ALL_SUCCESS`
export const FETCH_LAZY_REQUEST = `${prefix}/FETCH_LAZY_REQUEST`
export const FETCH_LAZY_START = `${prefix}/FETCH_LAZY_START`
export const FETCH_LAZY_SUCCESS = `${prefix}/FETCH_LAZY_SUCCESS`
export const SELECT_EVENT = `${prefix}/SELECT_EVENT`
/**
* Reducer
* */
export const ReducerRecord = Record({
entities: new OrderedMap({}),
selected: new OrderedSet([]),
loading: false,
loaded: false
})
export const EventRecord = Record({
uid: null,
title: null,
url: null,
where: null,
when: null,
month: null,
submissionDeadline: null
})
export default function reducer(state = new ReducerRecord(), action) {
const {type, payload} = action
switch (type) {
case FETCH_ALL_REQUEST:
case FETCH_LAZY_START:
return state.set('loading', true)
case FETCH_ALL_SUCCESS:
return state
.set('loading', false)
.set('loaded', true)
.set('entities', fbDatatoEntities(payload, EventRecord))
case FETCH_LAZY_SUCCESS:
return state
.set('loading', false)
.mergeIn(['entities'], fbDatatoEntities(payload, EventRecord))
.set('loaded', Object.keys(payload).length < 10)
case SELECT_EVENT:
return state.selected.contains(payload.uid)
? state.update('selected', selected => selected.remove(payload.uid))
: state.update('selected', selected => selected.add(payload.uid))
default:
return state
}
}
/**
* Selectors
* */
export const stateSelector = state => state[moduleName]
export const entitiesSelector = createSelector(stateSelector, state => state.entities)
export const eventListSelector = createSelector(entitiesSelector, entities => (
entities.valueSeq().toArray()
))
/**
* Action Creators
* */
export function fetchAll() {
return {
type: FETCH_ALL_REQUEST
}
}
export function selectEvent(uid) {
return {
type: SELECT_EVENT,
payload: {uid}
}
}
export function fetchLazy() {
return {
type: FETCH_LAZY_REQUEST
}
}
/**
* Sagas
* */
export const fetchAllSaga = function * () {
while (true) {
yield take(FETCH_ALL_REQUEST)
const ref = firebase.database().ref('events')
const data = yield call([ref, ref.once], 'value')
yield put({
type: FETCH_ALL_SUCCESS,
payload: data.val()
})
}
}
export const fetchLazySaga = function * () {
while (true) {
yield take(FETCH_LAZY_REQUEST)
const state = yield select(stateSelector)
if (state.loading || state.loaded) continue
// if (state.loaded) return
yield put({
type: FETCH_LAZY_START
})
const lastEvent = state.entities.last()
const ref = firebase.database().ref('events')
.orderByKey()
.limitToFirst(10)
.startAt(lastEvent ? lastEvent.uid : '')
const data = yield call([ref, ref.once], 'value')
yield put({
type: FETCH_LAZY_SUCCESS,
payload: data.val()
})
}
}
export function* saga() {
yield all([
fetchAllSaga(),
fetchLazySaga()
])
}