From 463e890e8899e8679e70484776c50511267e6e3e Mon Sep 17 00:00:00 2001 From: Juthatip McDevitt Date: Wed, 17 Jul 2024 23:30:57 -0500 Subject: [PATCH] updated home page and functionality --- todo_app/app/(tabs)/home/index.js | 184 ++++++++++++++++++++++++++++-- 1 file changed, 175 insertions(+), 9 deletions(-) diff --git a/todo_app/app/(tabs)/home/index.js b/todo_app/app/(tabs)/home/index.js index 337617f..3753a48 100644 --- a/todo_app/app/(tabs)/home/index.js +++ b/todo_app/app/(tabs)/home/index.js @@ -1,12 +1,118 @@ -import { Image, Pressable, ScrollView, StyleSheet, Text, TextInput, View } from 'react-native' -import React, { useState } from 'react' +import { Image, Pressable, ScrollView, StyleSheet, Text, TextInput, View } from 'react-native'; +import React, { useEffect, useState } from 'react'; import { AntDesign } from '@expo/vector-icons'; import { BottomModal, ModalContent, ModalTitle, SlideAnimation } from 'react-native-modals'; +import { Entypo } from '@expo/vector-icons'; +import { Feather } from '@expo/vector-icons'; +import axios from "axios" +import moment from 'moment'; +import { Buffer } from 'buffer'; +import AsyncStorage from "@react-native-async-storage/async-storage"; + + const index = () => { - const todo = []; + const [todos, setTodos] = useState([]); const [modalVisible, setModalVisible] = useState(false); - const [addToDo, setAddToDo] = useState(''); + const [todo, setTodo] = useState(''); + const [category, setCategory] = useState('all'); + const [pendingTodos, setPendingTodos] = useState([]); + const [compledTodos, setCompletedTodos] = useState([]); + const [markCompleted, setMarkCompleted] = useState(false); + const todayDate = moment().format("MMM Do YYYY"); + + const getToken = async () => { + try { + let token = await AsyncStorage.getItem('authToken'); // get the authToken stored in AsyncStorage from login.js + let decodeToken = JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString()); + let userId = decodeToken.userId; + return userId; + } catch (e) { + // error reading value + console.log(e); + } + }; + + const addTodo = async() => { + try { + const todoData = { + title:todo, + category:category, + } + + getToken().then((resp) => { + let userId = resp; + axios.post(`http://localhost:3030/todos/${userId}`, todoData).then((res) => { + }) + + }).catch((error) => { + console.log("Error", error); + }) + await getUserTodos(); + setModalVisible(false); + setTodo(''); + + } catch (error) { + console.log("Error", error) + } + } + + useEffect(() => { + getUserTodos(); + }, [markCompleted, modalVisible]) + + const getUserTodos = () => { + try { + getToken().then( async(resp) => { + let userId = resp; + const res = await axios.get(`http://localhost:3030/users/${userId}/todos`); + setTodos(res.data.todos); + + const fetchTodos = res.data.todos || []; + const pending = fetchTodos.filter((todo) => todo.status !== "completed"); + const completed = fetchTodos.filter((todo) => todo.status === "completed"); + + setPendingTodos(pending); + setCompletedTodos(completed); + }) + + } catch (error) { + console.log("Error", error) + } + }; + + const markTodocompleted = async (todoId) => { + try { + setMarkCompleted(true); + + const res = await axios.patch(`http://localhost:3030/todos/${todoId}/complete`); + } catch (error) { + console.log("Error", error) + } + } + //data + const suggestion = [ + { + id: 1, + todo: "Walk the dog" + }, + { + id: 2, + todo: "Go to the gym" + }, + { + id: 3, + todo: "Call mom" + }, + { + id: 4, + todo: "Get goceries" + }, + { + id: 5, + todo: "5 minute meditation" + }, + ] return ( <> @@ -26,8 +132,39 @@ const index = () => { - {todo?.length > 0 ? ( - + {todos?.length > 0 ? ( + + {pendingTodos?.length > 0 && + <> + {todayDate} + You have {pendingTodos.length} tasks to do! + + } + {pendingTodos.map((item, index) => ( + + + markTodocompleted(item?._id)} name="circle" size={15} color="black" /> + {item.title} + + + ))} + + {compledTodos?.length > 0 && ( + + + You have completed {compledTodos.length} tasks + + {compledTodos.map((item, index) => ( + + + + {item.title} + + + ))} + + )} + ) : ( { visible={modalVisible} onTouchOutside={() => setModalVisible(!modalVisible)} > - - - setAddToDo(text)} style={{padding:10, borderColor: "#61677A", borderRadius:5, borderWidth:1, flex:1, outlineStyle: 'none', color: "#31363F"}} placeholder='Add your new task'/> + + + setTodo(text)} style={{padding:10, borderColor: "#61677A", borderRadius:5, borderWidth:1, flex:1, outlineStyle: 'none', color: "#31363F"}} placeholder='Add your new task'/> + + + + Category: + + setCategory('personal')} style={{backgroundColor: "#D8D9DA", padding: 5, borderRadius: 5,}}> + Personal + + + setCategory('work')} style={{backgroundColor: "#D8D9DA", padding: 5, borderRadius: 5}}> + Work + + + setCategory('house')} style={{backgroundColor: "#D8D9DA", padding: 5, borderRadius: 5}}> + House + + + setCategory('social')} style={{backgroundColor: "#D8D9DA", padding: 5, borderRadius: 5}}> + Social + + + + Suggestions: + + {suggestion?.map((item, index) => + setTodo(item?.todo)} key={index} style={{backgroundColor: "#EEEEEE", padding: 5, borderRadius: 5}}> + {item.todo} + + )}