Skip to content

Instantly share code, notes, and snippets.

@szastupov
Created February 15, 2017 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save szastupov/3e3688cd05988d5485a3cdc2dcd13396 to your computer and use it in GitHub Desktop.
Save szastupov/3e3688cd05988d5485a3cdc2dcd13396 to your computer and use it in GitHub Desktop.
import { deleteAt, updateObjectAt, swap } from '../../lib/farr'
export function defaultDataReducer(name, initialData = null) {
const initialState = {
[name]: initialData,
isFetching: true,
isFetched: false,
isRequested: false
}
const NAME = name.toUpperCase()
return function reducer(state = initialState, action) {
switch (action.type) {
case `${NAME}_LOAD`:
return {
...state,
isFetching: true
}
case `${NAME}_RECEIVE`:
return {
...state,
isFetching: false,
isFetched: true,
isRequested: true,
[name]: action.data || []
}
default:
return state
}
}
}
import db from '../db'
export const updateChild = (res) => (name, data) => (dispatch) => {
let RES = res.toUpperCase()
dispatch({type: `${RES}_UPDATE`, name, data})
db.ref(res)
.child(name)
.update(data)
.catch(err => console.log(err))
}
export const deleteChild = (res) => (uid) => (dispatch) => {
let RES = res.toUpperCase()
dispatch({type: `${RES}_DELETE`, uid})
db.ref(res)
.child(uid)
.remove()
.catch(err => console.log(err))
}
export const newChild = (res, template) => () => (dispatch) => {
let RES = res.toUpperCase()
let ref = db.ref(res).push()
let uid = ref.key
dispatch({type: `${RES}_NEW`, uid})
ref.set({...template, uid})
.catch(err => console.log(err))
}
export function createActions(res, template) {
return {
create: newChild(res, template),
update: updateChild(res),
delete: deleteChild(res)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment