Página de criação de hábito
import React, { useEffect, useRef, useState } from "react";
import { useNavigation } from "@react-navigation/native";
import SelectHabit from "../../Components/HabitPage/SelectHabit";
import SelectFrequency from "../../Components/HabitPage/SelectFrequency";
import Notification from "../../Components/HabitPage/Notification";
import TimeDatePicker from "../../Components/HabitPage/TimeDataPicker";
import {
View,
Text,
StyleSheet,
Image,
TouchableOpacity,
ScrollView,
Alert,
} from "react-native";
export default function HabitPage({ route }) {
const navigation = useNavigation();
const [habitInput, setHabitInput] = useState();
const [frequencyInput, setFrequencyInput] = useState();
const [notificationToggle, setNotificationToggle] = useState();
const [dayNotification, setDayNotification] = useState();
const [timeNotification, setTimeNotification] = useState();
const { create, habit } = route.params;
function handleCreateHabit() {
if (
habitInput === undefined ||
frequencyInput === undefined
) {
Alert.alert(
"Você precisa selecionar um hábito e frequência para continuar"
);
} else if (
notificationToggle === true &&
frequencyInput === "Diário" &&
timeNotification === undefined
) {
Alert.alert("Você precisa dizer o horário da notificação!");
} else if (
notificationToggle === true &&
frequencyInput === "Diário" &&
dayNotification === undefined &&
timeNotification === undefined
) {
Alert.alert(
"Você precisa dizer a frequência e o horário da notificação!"
);
} else {
navigation.navigate("Home", {
createdHabit: `Created in ${habit?.habitArea}`,
});
}
}
function handleUpdateHabit() {
if (notificationToggle === true && !dayNotification && !timeNotification) {
Alert.alert("Você precisa colocar a frequência e horário da notificação");
} else {
navigation.navigate("Home", {
updatedHabit: `Updated in ${habit?.habitArea}`,
});
}
}
return (
<View style={styles.container}>
<ScrollView>
<View>
<TouchableOpacity
style={styles.bakcPageBtn}
onPress={() => navigation.goBack()}
>
<Image
source={require("../../assets/icons/arrowBack.png")}
style={styles.arrowBack}
/>
</TouchableOpacity>
<View style={styles.mainContent}>
<Text style={styles.title}>Configurações {"\\n"} de hábito</Text>
<Text style={styles.inputText}>Área</Text>
<View style={styles.inputContainer}>
<Text style={styles.area}>{habitArea}</Text>
</View>
<Text style={styles.inputText}>Hábito</Text>
<SelectHabit habit={habit} habitInput={setHabitInput} />
<Text style={styles.inputText}>Frequência</Text>
<SelectFrequency
habitFrequency={habit?.habitFrequency}
frequencyInput={setFrequencyInput}
/>
{frequencyInput === "Mensal" ? null : (
<Notification
notificationToggle={notificationToggle}
setNotificationToggle={setNotificationToggle}
/>
)}
{notificationToggle ? (
frequencyInput === "Mensal" ? null : (
<TimeDatePicker
frequency={frequencyInput}
dayNotification={dayNotification}
timeNotification={timeNotification}
setDayNotification={setDayNotification}
setTimeNotification={setTimeNotification}
/>
)
) : null}
</View>
</View>
</ScrollView>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "rgba(21, 21, 21, 0.98)",
},
bakcPageBtn: {
width: 40,
height: 40,
margin: 25,
},
arrowBack: {
width: 40,
height: 40,
},
mainContent: {
width: 250,
alignSelf: "center",
},configButton: {
alignItems: "center",
},
title: {
fontWeight: "bold",
textAlign: "center",
color: "white",
fontSize: 30,
},
inputText: {
color: "white",
fontSize: 16,
marginTop: 35,
marginBottom: 10,
marginLeft: 5,
},
inputContainer: {
borderWidth: 1,
borderColor: "#FFFFFF",
borderRadius: 10,
paddingHorizontal: 20,
paddingVertical: 15,
},
area: {
color: "#BBBBBB",
fontSize: 15,
},
});
Componentes
Select Habit
import React, { useEffect, useState } from "react";
import { Image, StyleSheet } from "react-native";
import { SelectList } from "react-native-dropdown-select-list";
import HabitsData from "../../../Database/HabitsData";
export default function SelectHabit ({ habit, habitInput }) {
const [selected, setSelected] = useState(habit?.habitName ? habit?.habitName : "-");
const [data, setData] = useState();
useEffect(() => {
if (area === "Mente") {
setData(HabitsData.dataMind);
}
if (area === "Financeiro") {
setData(HabitsData.dataMoney);
}
if (area === "Corpo") {
setData(HabitsData.dataBody);
}
if (area === "Humor") {
setData(HabitsData.dataFun);
}
habitInput(habit?.habitName ? habit?.habitName : undefined);
}, []);
return (
<>
<SelectList
setSelected={setSelected}
data={data}
search={false}
onSelect={() => {
habitInput(selected);
}}
placeholder={selected}
boxStyles={styles.boxStyle}
inputStyles={styles.inputStyle}
dropdownStyles={styles.dropdownStyle}
dropdownItemStyles={styles.dropdownItemStyle}
dropdownTextStyles={styles.dropdownTextStyle}
arrowicon={
<Image
source={require("../../../assets/icons/arrowDropdown.png")}
style={styles.arrow}
/>
}
/>
</>
);
};
const styles = StyleSheet.create({
boxStyle: {
borderWidth: 1,
borderColor: "white",
paddingHorizontal: 20,
paddingVertical: 15,
},
inputStyle: {
color: "white",
},
dropdownStyle: {
borderWidth: 0,
},
dropdownItemStyle: {
borderWidth: 1,
borderColor: "#BBBB",
borderRadius: 10,
marginBottom: 15,
},
dropdownTextStyle: {
color: "#BBBBBB",
},
arrow: {
width: 20,
height: 20,
},
});
Select Frequency
import React, { useEffect, useState } from "react";
import { Image, View, StyleSheet } from "react-native";
import { SelectList } from "react-native-dropdown-select-list";
export default function SelectFrequency ({ habitFrequency, frequencyInput }) {
const [selected, setSelected] = useState(
habitFrequency ? habitFrequency : "-"
);
const data = [
{ key: "Diário", value: "Diário" },
{ key: "Semanal", value: "Semanal" },
{ key: "Mensal", value: "Mensal" },
];
useEffect(() => {
frequencyInput(habitFrequency ? habitFrequency : undefined);
}, []);
return (
<View style={{ marginBottom: 20 }}>
<SelectList
data={data}
search={false}
setSelected={setSelected}
onSelect={() => {
alert(selected);
frequencyInput(selected);
}}
placeholder={selected}
boxStyles={styles.boxStyle}
inputStyles={styles.inputStyle}
dropdownStyles={styles.dropdownStyle}
dropdownItemStyles={styles.dropdownItemStyle}
dropdownTextStyles={styles.dropdownTextStyle}
arrowicon={
<Image
source={require("../../../assets/icons/arrowDropdown.png")}
style={styles.arrow}
/>
}
/>
</View>
);
};
const styles = StyleSheet.create({
boxStyle: {
borderWidth: 1,
borderColor: "white",
paddingHorizontal: 20,
paddingVertical: 15,
},
inputStyle: {
color: "white",
},
dropdownStyle: {
borderWidth: 0,
},
dropdownItemStyle: {
borderWidth: 1,
borderColor: "#BBBB",
borderRadius: 10,
marginBottom: 15,
},
dropdownTextStyle: {
color: "#BBBBBB",
},
arrow: {
width: 20,
height: 20,
},
});
Notifcation
import React from "react";
import { StyleSheet, Switch } from "react-native";
import { View, Text } from "react-native";
export default function Notification({
notificationToggle,
setNotificationToggle,
}) {
const toggleSwitch = () => {
setNotificationToggle((previousState) => !previousState);
};
return (
<>
<View style={styles.container}>
<Text style={styles.title}>Notificação</Text>
<Switch
trackColor={{ false: "#FF0044", true: "#2DBE56" }}
thumbColor={"#FFFFFF"}
ios_backgroundColor="#3e3e3e"
onValueChange={toggleSwitch}
value={notificationToggle}
/>
</View>
</>
);
}
const styles = StyleSheet.create({
container: {
flexDirection: "row",
alignItems: "center",
marginBottom: 20,
},
titleDisabled: {
color: "#BBBB",
fontSize: 20,
marginRight: 10,
},
title: {
color: "white",
fontSize: 20,
marginRight: 10,
},
});
TimePicker
import React, { useEffect, useRef, useState } from "react";
import {
View,
Text,
StyleSheet,
Image,
TouchableOpacity,
ScrollView,
} from "react-native";
import SelectHabit from "../../components/HabitPage/SelectHabit";
import SelectFrequency from "../../components/HabitPage/SelectFrequency";
import Notification from "../../components/HabitPage/Notification";
import TimeDatePicker from "../../components/HabitPage/TimeDatePicker";
export default function HabitPage({ route }) {
const navigation = useNavigation();
const [habitInput, setHabitInput] = useState();
const [frequencyInput, setFrequencyInput] = useState();
const [notificationToggle, setNotificationToggle] = useState();
const [dayNotification, setDayNotification] = useState();
const [timeNotification, setTimeNotification] = useState();
const {
create,
habitArea,
habitName,
habitFrequency,
habitHasNotification,
habitNotificationFrequency,
habitNotificationTime,
} = route.params;
return (
<View style={styles.container}>
<ScrollView>
<View>
<TouchableOpacity
style={styles.bakcPageBtn}
onPress={() => navigation.goBack()}
>
<Image
source={require("../../assets/icons/arrowBack.png")}
style={styles.arrowBack}
/>
</TouchableOpacity>
<View style={styles.mainContent}>
<Text style={styles.title}>Configurações {"\\n"} de hábito</Text>
<Text style={styles.inputText}>Área</Text>
<View style={styles.inputContainer}>
<Text style={styles.area}>{habitArea}</Text>
</View>
<Text style={styles.inputText}>Hábito</Text>
<SelectHabit habit={habit} habitInput={setHabitInput} />
<Text style={styles.inputText}>Frequência</Text>
<SelectFrequency
habitFrequency={habit?.habitFrequency}
frequencyInput={setFrequencyInput}
/>
{frequencyInput === "Mensal" ? null : (
<Notification
notificationToggle={notificationToggle}
setNotificationToggle={setNotificationToggle}
/>
)}
{notificationToggle ? (
frequencyInput === "Mensal" ? null : (
<TimeDatePicker
frequency={frequencyInput}
dayNotification={dayNotification}
timeNotification={timeNotification}
setDayNotification={setDayNotification}
setTimeNotification={setTimeNotification}
/>
)
) : null}
</View>
</View>
</ScrollView>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "rgba(21, 21, 21, 0.98)",
},
bakcPageBtn: {
width: 40,
height: 40,
margin: 25,
},
arrowBack: {
width: 40,
height: 40,
},
mainContent: {
width: 250,
alignSelf: "center",
},configButton: {
alignItems: "center",
},
title: {
fontWeight: "bold",
textAlign: "center",
color: "white",
fontSize: 30,
},
inputText: {
color: "white",
fontSize: 16,
marginTop: 35,
marginBottom: 10,
marginLeft: 5,
},
inputContainer: {
borderWidth: 1,
borderColor: "#FFFFFF",
borderRadius: 10,
paddingHorizontal: 20,
paddingVertical: 15,
},
area: {
color: "#BBBBBB",
fontSize: 15,
},
});
Update & Exclude Button
import React from "react";
import {
View,
Text,
TouchableOpacity,
StyleSheet,
Image,
Alert,
} from "react-native";
import { useNavigation } from "@react-navigation/native";
export default function UpdateExcludeButtons({
habitInput,
handleUpdate,
habitArea,
}) {
const navigation = useNavigation();
function handleDeleteHabit() {
navigation.navigate("Home", {
excludeArea: `${habitArea}`,
});
}
return (
<View style={styles.container}>
<TouchableOpacity
style={styles.updateButton}
activeOpacity={0.8}
onPress={() => {
Alert.alert(
"Ao prosseguir você vai atualizar o hábito, tem certeza?",
"Ao fazer isso você pode mudar o hábito, frequência e notificação.",
[
{
text: "Cancelar",
},
{
text: "Atualizar",
onPress: handleUpdate,
},
]
);
}}
>
<Text style={styles.updateButtonText}>Atualizar</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.trashButton}
activeOpacity={0.8}
onPress={() => {
Alert.alert(
`Você tem certeza que quer excluir o hábito?`,
"Ao fazer isso perderá todo o progresso ou falha do hábito.",
[
{
text: "Cancelar",
onPress: () => {
Alert.alert("Exclusão cancelada com sucesso!");
},
},
{
text: "Excluir",
onPress: handleDeleteHabit,
},
]
);
}}
>
<Image
source={require("../../../assets/icons/trash.png")}
style={styles.trashIcon}
/>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
container: {
flexDirection: "row",
marginBottom: 20,
},
updateButton: {
borderWidth: 1,
borderColor: "white",
width: 150,
height: 50,
marginRight: 10,
alignItems: "center",
justifyContent: "center",
borderRadius: 10,
},
updateButtonText: {
color: "white",
fontWeight: "bold",
fontSize: 18,
},
trashButton: {
borderWidth: 1,
borderColor: "#FF0044",
borderRadius: 10,
justifyContent: "center",
alignItems: "center",
width: 90,
},
trashIcon: {
width: 25,
height: 25,
},
});