-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathevents.js
More file actions
188 lines (152 loc) · 4.79 KB
/
events.js
File metadata and controls
188 lines (152 loc) · 4.79 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import {all, takeEvery, put, call, take, select} from 'redux-saga/effects'
import {appName} from '../config'
import {Record, OrderedSet, OrderedMap} from 'immutable'
import firebase from 'firebase'
import {createSelector} from 'reselect'
import {fbToEntities} from './utils'
/**
* Constants
* */
export const moduleName = 'events'
const prefix = `${appName}/${moduleName}`
export const FETCH_ALL_REQUEST = `${prefix}/FETCH_ALL_REQUEST`
export const FETCH_ALL_START = `${prefix}/FETCH_ALL_START`
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 = `${prefix}/SELECT`
export const ADD_PERSON_TO_EVENT = `${prefix}/ADD_PERSON_TO_EVENT`
export const REMOVE = `${prefix}/REMOVE`
/**
* Reducer
* */
export const ReducerRecord = Record({
loading: false,
loaded: false,
selected: new OrderedSet(),
entities: new OrderedMap({})
})
export const EventRecord = Record({
uid: null,
month: null,
submissionDeadline: null,
title: null,
url: null,
when: null,
where: null
})
export default function reducer(state = new ReducerRecord(), action) {
const {type, payload} = action
switch (type) {
case FETCH_ALL_START:
case FETCH_LAZY_START:
return state.set('loading', true)
case FETCH_ALL_SUCCESS:
return state
.set('loading', false)
.set('loaded', true)
.set('entities', fbToEntities(payload, EventRecord))
case FETCH_LAZY_SUCCESS:
return state
.set('loading', false)
.mergeIn(['entities'], fbToEntities(payload, EventRecord))
.set('loaded', Object.keys(payload).length < 10)
case SELECT:
return state.update('selected', selected => selected.has(payload.uid)
? selected.remove(payload.uid)
: selected.add(payload.uid)
)
case REMOVE:
return state.update('entities', entities => entities.delete(payload.eventId))
default:
return state
}
}
/**
* Selectors
* */
export const stateSelector = state => state[moduleName]
export const entitiesSelector = createSelector(stateSelector, state => state.entities)
export const loadingSelector = createSelector(stateSelector, state => state.loading)
export const loadedSelector = createSelector(stateSelector, state => state.loaded)
export const eventListSelector = createSelector(entitiesSelector, entities => entities.valueSeq().toArray())
export const selectionSelector = createSelector(stateSelector, state => state.selected)
export const selectedEvents = createSelector(eventListSelector, selectionSelector, (events, selected) =>
events.filter(event => selected.has(event.uid))
)
export const idSelector = (_, props) => props.id
export const eventSelector = createSelector(entitiesSelector, idSelector, (entities, id) => entities.get(id) !== undefined?entities.get(id):EventRecord)
/**
* Action Creators
* */
export function fetchAllEvents() {
return {
type: FETCH_ALL_REQUEST
}
}
export function selectEvent(uid) {
return {
type: SELECT,
payload: { uid }
}
}
export function fetchLazy() {
return {
type: FETCH_LAZY_REQUEST
}
}
export function addPersonToEvent(personId, eventId) {
return {
type: ADD_PERSON_TO_EVENT,
payload: { personId, eventId }
}
}
export function removeEvent(eventId){
return {
type: REMOVE,
payload:{eventId}
}
}
/**
* Sagas
* */
export function* fetchAllSaga() {
const ref = firebase.database().ref('events')
yield put({
type: FETCH_ALL_START
})
const snapshot = yield call([ref, ref.once], 'value')
console.log('---', snapshot)
yield put({
type: FETCH_ALL_SUCCESS,
payload: snapshot.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([
takeEvery(FETCH_ALL_REQUEST, fetchAllSaga),
fetchLazySaga()
])
}