Repository navigation
Expand file tree
/
Copy pathpeople.js
More file actions
146 lines (126 loc) · 3.29 KB
/
Copy pathpeople.js
File metadata and controls
146 lines (126 loc) · 3.29 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
import firebase from 'firebase/app'
import { appName } from '../config'
import { Record, OrderedMap } from 'immutable'
import { reset } from 'redux-form'
import { createSelector } from 'reselect'
import { put, takeEvery, call, all } from 'redux-saga/effects'
import { generateId, fbToEntities } from './utils'
/**
* Constants
* */
export const moduleName = 'people'
const prefix = `${appName}/${moduleName}`
export const ADD_PERSON = `${prefix}/ADD_PERSON`
export const ADD_PERSON_SUCCESS = `${prefix}/ADD_PERSON_SUCCESS`
export const FETCH_PERSONS = `${prefix}/FETCH_PERSONS`
export const FETCH_PERSONS_SUCCESS = `${prefix}/FETCH_PERSONS_SUCCESS`
export const DELETE_PERSON = `${prefix}/DELETE_PERSON`
export const DELETE_PERSON_SUCCESS = `${prefix}/DELETE_PERSON_SUCCESS`
export const ADD_EVENT = `${prefix}/ADD_EVENT`
/**
* Reducer
* */
const PersonRecord = Record({
uid: null,
firstName: null,
lastName: null,
email: null,
events: []
})
const ReducerState = Record({
entities: new OrderedMap()
})
export default function reducer(state = new ReducerState(), action) {
const { type, payload } = action
switch (type) {
case ADD_PERSON:
return state.mergeIn(['entities'], fbToEntities(payload, PersonRecord))
case FETCH_PERSONS_SUCCESS:
return state.mergeIn(['entities'], fbToEntities(payload, PersonRecord))
case DELETE_PERSON_SUCCESS:
return state.removeIn(['entities', action.payload])
default:
return state
}
}
/**
* Selectors
* */
export const stateSelector = (state) => state[moduleName]
export const peopleSelector = createSelector(stateSelector, (state) =>
state.entities.valueSeq().toArray()
)
export const uidSelector = (_, props) => props.uid
export const personSelector = createSelector(
stateSelector,
uidSelector,
(state, uid) => state.entities.find((person) => person.uid === uid)
)
/**
* Action Creators
* */
export function addPerson(person) {
return {
type: ADD_PERSON,
payload: { person }
}
}
export function addEventToPerson(eventUid, personUid) {
return {
type: ADD_EVENT,
payload: {
eventUid,
personUid
}
}
}
export function fetchPersons() {
return {
type: FETCH_PERSONS
}
}
export function deletePerson(personId) {
return {
type: DELETE_PERSON,
payload: { personId }
}
}
/**
* Sagas
*/
export function* addPersonSaga(action) {
// const uid = yield call(generateId)
const successAction = {
type: ADD_PERSON_SUCCESS,
payload: action.payload.person
}
const ref = firebase.database().ref('/peoples')
yield call([ref, ref.push], successAction.payload)
yield put(successAction)
yield put(reset('person'))
}
export function* fetchPersonsSaga() {
const ref = firebase.database().ref('/peoples')
const resp = yield call([ref, ref.once], 'value')
if (resp.val()) {
yield put({
type: FETCH_PERSONS_SUCCESS,
payload: resp.val()
})
}
}
export function* deletePersonSaga(action) {
const ref = firebase.database().ref(`/peoples/${action.payload.personId}`)
yield call([ref, ref.remove])
yield put({
type: DELETE_PERSON_SUCCESS,
payload: action.payload
})
}
export function* saga() {
yield all([
takeEvery(ADD_PERSON, addPersonSaga),
takeEvery(FETCH_PERSONS, fetchPersonsSaga),
takeEvery(DELETE_PERSON, deletePersonSaga)
])
}