-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated app to work with Redux (Basic)
- Loading branch information
1 parent
610ce55
commit 52ad453
Showing
6 changed files
with
124 additions
and
133 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const ADD_INGREDIENT = 'ADD_INGREDIENT'; | ||
export const REMOVE_INGREDIENT = 'REMOVE_INGREDIENT'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as actionTypes from './actions'; | ||
|
||
const INGREDIENT_PRICES = { | ||
salad: 0.5, | ||
cheese: 0.4, | ||
bacon: 0.7, | ||
meat: 1.3 | ||
} | ||
|
||
const initialState = { | ||
ingredients: { | ||
salad: 0, | ||
cheese: 0, | ||
bacon: 0, | ||
meat: 0 | ||
}, | ||
totalPrice: 5 | ||
}; | ||
|
||
const reducer = (state = initialState, action) => { | ||
let updatedState = { | ||
...state, | ||
ingredients: { | ||
...state.ingredients | ||
} | ||
}; | ||
|
||
const ingredient = action.ingredientName; | ||
|
||
switch ( action.type ) { | ||
case actionTypes.ADD_INGREDIENT: | ||
updatedState.ingredients[ingredient] = state.ingredients[ingredient] + 1; | ||
updatedState.totalPrice = state.totalPrice + INGREDIENT_PRICES[ingredient]; | ||
break; | ||
case actionTypes.REMOVE_INGREDIENT: | ||
updatedState.ingredients[ingredient] = state.ingredients[ingredient] - 1; | ||
updatedState.totalPrice = state.totalPrice - INGREDIENT_PRICES[ingredient]; | ||
break; | ||
default: | ||
console.log("Action default case"); | ||
break; | ||
} | ||
|
||
return updatedState; | ||
}; | ||
|
||
export default reducer; |