|
| 1 | +import { useState, useEffect } from "react"; |
| 2 | +import { View, StyleSheet, Alert } from "react-native"; |
| 3 | +import { TextInput, Button, Modal } from "react-native-paper"; |
| 4 | +import DropdownMenu from "./DropdownMenu"; |
| 5 | +import { db } from "../db/dbConnection"; |
| 6 | + |
| 7 | +export default AddTaskModal = ({ |
| 8 | + modalVisible, |
| 9 | + addTaskTag, |
| 10 | + setModalVisible, |
| 11 | +}) => { |
| 12 | + const [taskTitle, setTaskTitle] = useState(""); |
| 13 | + const [tagsList, setTagsList] = useState([]); |
| 14 | + const [selectedTag, setSelectedTag] = useState({}); |
| 15 | + |
| 16 | + const closeModal = () => { |
| 17 | + setModalVisible(false); |
| 18 | + }; |
| 19 | + |
| 20 | + const handleAddTask = () => { |
| 21 | + if (taskTitle.trim()) { |
| 22 | + addTaskTag({ title: taskTitle.trim(), isCompleted: false }, selectedTag); |
| 23 | + setTaskTitle(""); |
| 24 | + setSelectedTag({}); |
| 25 | + closeModal(); |
| 26 | + } else { |
| 27 | + Alert.alert("Please add a new task."); |
| 28 | + } |
| 29 | + }; |
| 30 | + |
| 31 | + const getTags = async () => { |
| 32 | + try { |
| 33 | + const tags = await db.execute("SELECT * FROM tags"); |
| 34 | + setTagsList(tags.rows); |
| 35 | + } catch (error) { |
| 36 | + console.error("Error getting tags", error); |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + useEffect(() => { |
| 41 | + getTags(); |
| 42 | + }, []); |
| 43 | + |
| 44 | + return ( |
| 45 | + <Modal |
| 46 | + style={styles.modalContainer} |
| 47 | + visible={modalVisible} |
| 48 | + onDismiss={closeModal} |
| 49 | + > |
| 50 | + <View> |
| 51 | + <View style={styles.newTaskBox}> |
| 52 | + <TextInput |
| 53 | + mode="flat" |
| 54 | + style={[ |
| 55 | + styles.textInput |
| 56 | + ]} |
| 57 | + contentStyle={styles.textInputContent} |
| 58 | + underlineColor="transparent" |
| 59 | + activeUnderlineColor="#6BA2EA" |
| 60 | + value={taskTitle} |
| 61 | + onChangeText={setTaskTitle} |
| 62 | + label="Enter a new task" |
| 63 | + keyboardType="default" |
| 64 | + /> |
| 65 | + </View> |
| 66 | + <DropdownMenu tagsList={tagsList} setSelectedTag={setSelectedTag} /> |
| 67 | + <Button |
| 68 | + style={styles.addTaskButton} |
| 69 | + textColor="black" |
| 70 | + onPress={handleAddTask} |
| 71 | + > |
| 72 | + Add task |
| 73 | + </Button> |
| 74 | + </View> |
| 75 | + </Modal> |
| 76 | + ); |
| 77 | +}; |
| 78 | + |
| 79 | +const styles = StyleSheet.create({ |
| 80 | + modalContainer: { |
| 81 | + flex: 1, |
| 82 | + backgroundColor: "white", |
| 83 | + padding: 10, |
| 84 | + }, |
| 85 | + newTaskBox: { |
| 86 | + flexDirection: "row", |
| 87 | + alignItems: "center", |
| 88 | + justifyContent: "center", |
| 89 | + borderWidth: 1, |
| 90 | + borderColor: "lightgray", |
| 91 | + backgroundColor: "#f0f5fd", |
| 92 | + marginBottom: 10, |
| 93 | + }, |
| 94 | + textInput: { |
| 95 | + width: "100%", |
| 96 | + backgroundColor: "transparent", |
| 97 | + height: 50, |
| 98 | + }, |
| 99 | + textInputContent: { |
| 100 | + backgroundColor: "transparennt", |
| 101 | + borderWidth: 0, |
| 102 | + paddingLeft: 10, |
| 103 | + }, |
| 104 | + button: { |
| 105 | + height: 50, |
| 106 | + width: 50, |
| 107 | + justifyContent: "center", |
| 108 | + alignItems: "center", |
| 109 | + }, |
| 110 | + closeButton: { |
| 111 | + alignItems: "flex-start", |
| 112 | + bottom: 180, |
| 113 | + left: -10, |
| 114 | + zIndex: 1, |
| 115 | + }, |
| 116 | + addTaskButton: { |
| 117 | + backgroundColor: "#b2cae9", |
| 118 | + marginTop: 10, |
| 119 | + }, |
| 120 | +}); |
0 commit comments