diff --git a/src/components/TaskBox.tsx b/src/components/TaskBox.tsx index f67f7c6..ee637e1 100644 --- a/src/components/TaskBox.tsx +++ b/src/components/TaskBox.tsx @@ -1,26 +1,47 @@ -import { useState } from 'react'; -import { ModalBody, ModalContent, Modal, ModalHeader, ModalCloseButton, ModalOverlay, Box, Stack, Checkbox, Flex, Text } from '@chakra-ui/react'; - +import { + ModalBody, + ModalContent, + Modal, + ModalHeader, + ModalCloseButton, + ModalOverlay, + Box, + Stack, + Checkbox, + Flex, + Text, +} from '@chakra-ui/react'; import { updateDoc, doc } from 'firebase/firestore'; +import { useEffect, useState } from 'react'; + import { db } from '../../firebase.config'; import { Task } from '../models/Task'; +import { TaskFormData, TaskForm } from './TaskForm'; + interface CustomBoxProps { task: Task; } -import { TaskFormData, TaskForm } from './TaskForm'; const TaskBox: React.FC = ({ task }) => { - const [isModalOpen, setIsModalOpen] = useState(false); - const [defaultFormValues] = useState({ + const [defaultFormValues, setDefaultFormValues] = useState({ name: task.name, deadline: task.deadline, description: task.description, importance: task.importance, }); + useEffect(() => { + setDefaultFormValues({ + name: task.name, + deadline: task.deadline, + description: task.description, + importance: task.importance, + }); + }, [task]); + const handleUpdateTask = async (newTask: TaskFormData) => { const taskDoc = doc(db, 'tasks', task.id); const newFields = { @@ -33,24 +54,18 @@ const TaskBox: React.FC = ({ task }) => { setIsModalOpen(false); }; - const handleCheckboxChange = async () => { - - - const taskDoc = doc(db, 'tasks', task.id); const newFields = { - status: !task.status + status: !task.status, }; await updateDoc(taskDoc, newFields); - }; const handleBoxClick = () => { setIsModalOpen(true); - } - + }; const getBorderColor = () => { switch (task.importance) { @@ -75,19 +90,15 @@ const TaskBox: React.FC = ({ task }) => { width="100%" height="200px" bg="background" - opacity={task.status ? "50%" : "100%"} + opacity={task.status ? '50%' : '100%'} display="flex" alignItems="center" justifyContent="center" > - + - + {task.name} @@ -96,8 +107,14 @@ const TaskBox: React.FC = ({ task }) => { - - + + setIsModalOpen(false)}> diff --git a/src/components/TaskForm.tsx b/src/components/TaskForm.tsx index 52d9fb3..68b330a 100644 --- a/src/components/TaskForm.tsx +++ b/src/components/TaskForm.tsx @@ -1,12 +1,11 @@ -import { Button, HStack, Heading, Input, Radio, RadioGroup, Stack } from '@chakra-ui/react'; import { DeleteIcon } from '@chakra-ui/icons'; +import { Button, HStack, Heading, Input, Radio, RadioGroup, Stack } from '@chakra-ui/react'; +import { deleteDoc, doc, Timestamp } from 'firebase/firestore'; import { useState } from 'react'; import CustomCalendar from './CustomCalendar'; -import { deleteDoc, doc, Timestamp } from 'firebase/firestore'; import { db } from '../../firebase.config'; - -import { Task } from '../models/Task' +import { Task } from '../models/Task'; export interface TaskFormData { name: string; @@ -18,11 +17,10 @@ export interface TaskFormData { interface ITaskForm { defaultValues?: TaskFormData; onSubmit: (newTask: TaskFormData) => void; - isUpdate: boolean; - task: Task + taskId?: string; } -export function TaskForm({ onSubmit, defaultValues, isUpdate, task }: ITaskForm) { +export function TaskForm({ onSubmit, defaultValues, taskId }: ITaskForm) { const [name, setName] = useState(defaultValues?.name ?? ''); const [deadline, setDeadline] = useState(defaultValues?.deadline ?? null); const [description, setDescription] = useState(defaultValues?.description ?? ''); @@ -96,7 +94,7 @@ export function TaskForm({ onSubmit, defaultValues, isUpdate, task }: ITaskForm) width="50%" size="sm" bg="secondarytext" - _hover={{ bg: "secondary", color: "secondarytext" }} + _hover={{ bg: 'secondary', color: 'secondarytext' }} colorScheme="green" onClick={() => { onSubmit({ name, deadline, description, importance }); @@ -106,18 +104,17 @@ export function TaskForm({ onSubmit, defaultValues, isUpdate, task }: ITaskForm) setImportance(1); }} > - {isUpdate ? 'Update' : 'Add'} + {taskId ? 'Update' : 'Add'} - {isUpdate ? - < Button size="sm" colorScheme="red" onClick={() => deleteTask(task.id)}> + {taskId ? ( + - : - <> - - } + ) : ( + <> + )} - + ); } diff --git a/src/models/Task.ts b/src/models/Task.ts index e57b8db..5c6adc7 100644 --- a/src/models/Task.ts +++ b/src/models/Task.ts @@ -4,7 +4,7 @@ export interface Task { name: string; description: string; importance: number; - deadline: Timestamp; + deadline: Timestamp | null; archived: boolean; status: boolean; userId: string;