Skip to content

Commit bf06543

Browse files
authored
Merge pull request #2 from coolguyzone/redux
Get adding and removing ingredients to store working on light booze tab
2 parents 96fef23 + 1e01ff0 commit bf06543

File tree

4 files changed

+43
-21
lines changed

4 files changed

+43
-21
lines changed

Screens/IngredientsTabs/LightBoozeTab.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useState } from "react";
2-
import { Button, StyleSheet, Text, View } from "react-native";
2+
import { Button, ScrollView, StyleSheet, Text, View } from "react-native";
33
import CheckBox from "react-native-check-box";
44
import IngredientCheckBox from "../../components/IngredientCheckBox";
55
import { Constants } from "../../util/constants";
@@ -13,29 +13,31 @@ function LightBoozeTab() {
1313
const ingredients = useSelector((state) => state.inventory);
1414
const dispatch = useDispatch();
1515
// dispatch(inventory.addIngredient('bourbon'))
16-
console.log('addIngredient', addIngredient),
17-
console.log('ingredients', ingredients)
18-
19-
16+
console.log("addIngredient", addIngredient),
17+
console.log("ingredients", ingredients);
2018

2119
return (
22-
<>
20+
<ScrollView>
2321
<Text style={{ marginTop: 20 }}>Light Booze</Text>
2422
<View style={styles.IngredientContainer}>
25-
{Constants.clearDrinks.map((drink) => {
26-
return <IngredientCheckBox key={drink}>{drink}</IngredientCheckBox>;
23+
{Constants.clearBooze.map((drink) => {
24+
return (
25+
<IngredientCheckBox key={drink} booze={drink}>
26+
{drink}
27+
</IngredientCheckBox>
28+
);
2729
})}
28-
<Button
30+
{/* <Button
2931
title="test"
3032
onPress={() => {
3133
console.log("clicked");
32-
console.log('ingredients2', ingredients)
34+
console.log("ingredients2", ingredients);
3335
return dispatch(addIngredient("bourbon"));
3436
}}
35-
></Button>
36-
<Text>WHat's up!!! {useSelector(ingredientsArray)}</Text>
37+
></Button>*/}
38+
<Text>WHat's up!!! {useSelector(ingredientsArray)}</Text>
3739
</View>
38-
</>
40+
</ScrollView>
3941
);
4042
}
4143

components/IngredientCheckBox.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
import { useState } from "react";
22
import CheckBox from "react-native-check-box";
3+
import { useSelector, useDispatch } from "react-redux";
4+
import { addIngredient, removeIngredient } from "../util/slices/inventorySlice";
5+
6+
function IngredientCheckBox(props) {
7+
const ingredients = useSelector((state) => state.inventory);
8+
const dispatch = useDispatch();
39

4-
function IngredientCheckBox({ children }) {
510
const [CheckBoxState, setCheckBoxState] = useState(false);
611
return (
712
<CheckBox
813
onClick={() =>
9-
CheckBoxState === true
10-
? setCheckBoxState(false)
11-
: setCheckBoxState(true)
14+
// CheckBoxState === true
15+
// ? setCheckBoxState(false)
16+
// : setCheckBoxState(true)
17+
{
18+
if (CheckBoxState === true) {
19+
setCheckBoxState(false);
20+
dispatch(removeIngredient(props.booze));
21+
} else {
22+
setCheckBoxState(true);
23+
dispatch(addIngredient(props.booze));
24+
}
25+
}
1226
}
1327
isChecked={CheckBoxState}
14-
leftText={children}
28+
leftText={props.children}
1529
/>
1630
);
1731
}

util/constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const Constants = {
2525
"Añejo Rum",
2626
"Benedictine",
2727
],
28-
clearDrinks: [
28+
clearBooze: [
2929
"Vodka",
3030
"Gin",
3131
"Tequila",

util/slices/inventorySlice.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@ export const inventorySlice = createSlice({
1010
console.log('addIngredient')
1111
state.ingredientsArray.push(action.payload);
1212
},
13+
removeIngredient: (state, action) => {
14+
const index = state.ingredientsArray.indexOf(action.payload);
15+
if (index > -1) {
16+
state.ingredientsArray.splice(index, 1)
17+
}
18+
}
1319
},
1420
});
1521

16-
export const { addIngredient } = inventorySlice.actions;
17-
export const ingredientsArray = (state) => state.inventory.ingredientsArray;
22+
export const { addIngredient, removeIngredient } = inventorySlice.actions;
23+
export const ingredientsArray = (state) => state.inventory.ingredientsArray.toString();
1824

1925
export default inventorySlice.reducer;

0 commit comments

Comments
 (0)