-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathevents.js
More file actions
101 lines (85 loc) · 2.13 KB
/
events.js
File metadata and controls
101 lines (85 loc) · 2.13 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
import { appName } from '../config'
import { Record, List } from 'immutable'
import firebase from 'firebase/app'
import { takeEvery, put, call } from 'redux-saga/effects'
import { createSelector } from 'reselect'
/**
* Constants
* */
export const moduleName = 'events'
const prefix = `${appName}/${moduleName}`
export const LOAD_EVENTS_REQUEST = `${prefix}/LOAD_EVENTS_REQUEST`
export const LOAD_EVENTS_SUCCESS = `${prefix}/LOAD_EVENTS_SUCCESS`
export const LOAD_EVENTS_ERROR = `${prefix}/LOAD_EVENTS_ERROR`
/**
* Reducer
* */
export const ReducerRecord = Record({
entities: new List([]),
loading: false,
loaded: false
})
const EventRecord = Record({
id: 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 LOAD_EVENTS_REQUEST:
return state.set('loading', true)
case LOAD_EVENTS_SUCCESS:
return state
.update('entities', (entities) => {
let newEntities = []
for (let id in payload)
newEntities.push(new EventRecord({ ...payload[id], id }))
return new List(newEntities)
})
.set('loading', false)
.set('loaded', true)
case LOAD_EVENTS_ERROR:
return state
default:
return state
}
}
/**
* Selectors
* */
export const stateSelector = (state) => state[moduleName]
export const eventsSelector = createSelector(stateSelector, (state) =>
state.entities.valueSeq().toArray()
)
/**
* Action Creators
* */
export function loadEvents() {
return {
type: LOAD_EVENTS_REQUEST,
payload: {}
}
}
export function* loadEventsSaga(action) {
const databaseEvents = firebase.database().ref('/events/')
try {
const snapshot = yield call([databaseEvents, databaseEvents.once], 'value')
yield put({
type: LOAD_EVENTS_SUCCESS,
payload: snapshot.val()
})
} catch (error) {
yield put({
type: LOAD_EVENTS_ERROR,
error
})
}
}
export function* saga() {
yield takeEvery(LOAD_EVENTS_REQUEST, loadEventsSaga)
}