-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
99 lines (86 loc) · 2.36 KB
/
App.js
File metadata and controls
99 lines (86 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { StatusBar } from "expo-status-bar";
import { StyleSheet, Text, View } from "react-native";
import { AddMealBtn } from "./AddMealBtn";
import { MealTextInput } from "./MealTextInput";
import { MealCaloriesInput } from "./MealCaloriesInput";
import { useState, useEffect } from "react";
import { ClearButton } from "./ClearButton";
import { MealCaloriesTotal } from "./MealCaloriesTotal";
import { MealsList } from "./MealsList";
/**
* @function App
* @returns {App} App component instance
*/
export default function App() {
//state
const [show, setShow] = useState(false);
const [name, setName] = useState("");
const [cals, setCals] = useState("");
const [meals, setMeals] = useState([]);
const [total, setTotal] = useState(0);
//handlers
const getTotalCals = (newMeals) => {
let totalCals = newMeals
.map((item) => parseInt(item.cals))
.reduce((prev, curr) => prev + curr, 0);
setTotal(totalCals);
};
const clearMeals = () => {
setMeals([]);
setTotal(0);
};
const setMealNameHandler = (event) => {
setName(event);
};
const setMealCalsHandler = (val) => {
setCals(val);
};
const setShowTrue = () => {
setShow(true);
};
const grabMealHandler = () => {
const meal = {
name,
cals,
};
const newMeals = [...meals, meal];
setMeals(newMeals);
getTotalCals(newMeals);
setName("");
setCals(0);
};
return (
<View style={styles.container} data-test="main-app-container">
{meals.length > 0 && <ClearButton variant="blue" handler={clearMeals} />}
<View style={styles.fieldsContainer}>
<MealTextInput
id="name"
onChangeMeal={setMealNameHandler}
handler={() => setShowTrue()}
valProp={name}
/>
<MealCaloriesInput
onChangeCals={setMealCalsHandler}
id="cals"
handler={() => setShowTrue()}
calProp={cals.toString()}
/>
</View>
<StatusBar style="auto" />
{meals.length > 0 && <MealsList itemsList={meals} />}
{show && <AddMealBtn grabMealHandler={grabMealHandler} />}
<MealCaloriesTotal total={total} />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
fieldsContainer: {
flexDirection: "row",
},
});