Skip to content

Instantly share code, notes, and snippets.

@pfulop
Created August 30, 2016 13:14
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 pfulop/2a93ea97c89d5aff0e7793872f914130 to your computer and use it in GitHub Desktop.
Save pfulop/2a93ea97c89d5aff0e7793872f914130 to your computer and use it in GitHub Desktop.
export const CHANGED_ITEMS="CHANGED_ITEMS";
export function pushItem(item){
return (dispatch,_,firebaseApp)=>{
const itemsRef = getItemsRef(firebaseApp);
itemsRef.push({title:item});
};
}
export function removeItem(_key){
return (dispatch,_,firebaseApp)=>{
const itemsRef = getItemsRef(firebaseApp);
let child = itemsRef.child(_key);
child.remove();
};
}
export function updateItem(_key,item){
return (dispatch,_,firebaseApp)=>{
const itemsRef = getItemsRef(firebaseApp);
let child = itemsRef.child(_key);
child.update({title:item});
};
}
export function watchItems(){
return (dispatch,_,firebaseApp)=>{
const itemsRef = getItemsRef(firebaseApp);
itemsRef.on('value',(snap)=>{
const items = [];
snap.forEach((child)=>{
items.push({
title: child.val().title,
_key:child.key
});
});
dispatch(changedItems(items));
});
};
}
export function unWatchItems(){
return (dispatch,_,firebaseApp)=>{
const itemsRef = getItemsRef(firebaseApp);
itemsRef.off('value');
};
}
function changedItems(items){
return {
type: CHANGED_ITEMS,
data: items
};
}
const getItemsRef = (firebaseApp)=>firebaseApp.database().ref('items');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment