Last active
January 31, 2016 21:43
-
-
Save natac13/0c367603f0a1130a8a53 to your computer and use it in GitHub Desktop.
Firebase&Redux-thunk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
ADD_RECIPE, | |
BUILD_LIST | |
} from '../constants/'; | |
export { pushPath } from 'redux-simple-router'; | |
export function addRecipe(recipe) { | |
return { | |
type: ADD_RECIPE, | |
recipe | |
}; | |
} | |
/*====================================== | |
= Firebase setup = | |
======================================*/ | |
import Firebase from 'firebase'; | |
import Fireproof from 'fireproof'; | |
const fireRef = new Firebase('https://<PathToDBApp>/'); | |
const fp = new Fireproof(fireRef); | |
const list = fp.child('recipeList'); | |
/*===== End of Firebase setup ======*/ | |
==== End of asyncCreators ======*/ | |
/** | |
* currently the recipeList going through this action creator is in Firebase | |
* format. So the directions and ingredients are strings. The reducer function | |
* will handle the conversion. | |
* @param {object} recipeList From Firebase | |
* @return {object} Action object for the reducer function recipeList | |
*/ | |
export function buildList(recipeList) { | |
return { | |
type: BUILD_LIST, | |
recipeList | |
}; | |
} | |
/** | |
* Will return a function that takes in dispatch and getState and will return a promise. | |
* During this function call I fetch from the database via promises and using .then | |
* dispatch the action to add the fetched data to the local state store. | |
*/ | |
export function getRecipeListFirebase() { | |
return function(dispatch, getState) { | |
dispatch(requestRecipes()); | |
return list.then(function successFB(snap) { | |
dispatch(successfulRequest()); | |
// NOTE: recipeList in firebase format through buildList | |
// Convert in the reducer | |
dispatch(buildList(snap.val())); | |
}, function failFB() { | |
dispatch(failedRequest()); | |
}); | |
}; | |
} | |
import { | |
snakeCase, | |
snakedNameOf | |
} from '../js/core_helpers'; | |
import { | |
recipeExtras, | |
properRecipeFormat | |
} from '../js/core'; | |
/** | |
* This recipe is from the user input and is in the string format for directions | |
* and the ingredients | |
* | |
* Need to add checks for if no name property on the recipe coming in. | |
* If this is the case do not send to firebase or the redux store! | |
*/ | |
export function addRecipeFirebase(recipe) { | |
return (dispatch) => { | |
// create a child Firebase ref for the new recipe | |
const snakedName = snakeCase(recipe.name); | |
const dbPath = list.child(snakedName); | |
// the recipe will get a different uuid for firebase and the store. | |
let buffedRecipe = recipeExtras(recipe).toObject(); | |
return dbPath.set(buffedRecipe).then(function() { | |
// By passing the buffedRecipe to addRecipe I let recipeExtras see | |
// the recipe object already has a created_date and id. Therefore | |
// using them instead of creating new versions. | |
const realFormatRecipe = properRecipeFormat(buffedRecipe); | |
// check that there is a recipe coming back from formatting | |
if (!!realFormatRecipe) dispatch(addRecipe(realFormatRecipe)); | |
}); | |
}; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment