diff --git a/src/components/Home/Calendar/AddTaskButton.tsx b/src/components/Home/Calendar/AddTaskButton.tsx deleted file mode 100644 index 9547817..0000000 --- a/src/components/Home/Calendar/AddTaskButton.tsx +++ /dev/null @@ -1,118 +0,0 @@ -import type { Dayjs } from 'dayjs' -import { useScheduleStore } from '@/stores' -import { Button, Dialog, DialogActions, DialogContent, DialogTitle, FormControl, InputLabel, MenuItem, Select, TextField } from '@mui/material' -import { generateUniqueId, useToggle } from '@zl-asica/react' -import { useState } from 'react' - -interface AddTaskButtonProps { - selectedDate: Dayjs | null -} - -const AddTaskButton = ({ selectedDate }: AddTaskButtonProps) => { - const addTask = useScheduleStore(state => state.addTask) - - const [date, setDate] = useState(selectedDate?.format('YYYY-MM-DD') ?? '') - const [open, toggleOpen] = useToggle() - const [title, setTitle] = useState('') - const [description, setDescription] = useState('') - const [category, setCategory] = useState('work') - const [priority, setPriority] = useState('medium') - - return ( - <> - - - - Add Task - - setDate(event_.target.value)} - slotProps={{ - inputLabel: { shrink: true }, - }} - fullWidth - margin="normal" - /> - setTitle(event_.target.value)} - fullWidth - margin="normal" - /> - setDescription(event_.target.value)} - fullWidth - margin="normal" - /> - - Category - - - - Priority - - - - - - - - - - ) -} - -export default AddTaskButton diff --git a/src/components/Home/Calendar/AddTaskButton/CustomInputField.tsx b/src/components/Home/Calendar/AddTaskButton/CustomInputField.tsx new file mode 100644 index 0000000..5d2a3ad --- /dev/null +++ b/src/components/Home/Calendar/AddTaskButton/CustomInputField.tsx @@ -0,0 +1,32 @@ +import { TextField } from '@mui/material' + +interface CustomInputFieldProps { + label: string + type: 'date' | 'time' | 'text' + value: string + onChange: (value: string) => void + required?: boolean +} + +const CustomInputField = ({ + label, + type, + value, + onChange, + required = false, +}: CustomInputFieldProps) => ( + onChange(event_.target.value)} + required={required} + fullWidth + margin="normal" + slotProps={{ + inputLabel: { shrink: true }, + }} + /> +) + +export default CustomInputField diff --git a/src/components/Home/Calendar/AddTaskButton/index.tsx b/src/components/Home/Calendar/AddTaskButton/index.tsx new file mode 100644 index 0000000..afe41d2 --- /dev/null +++ b/src/components/Home/Calendar/AddTaskButton/index.tsx @@ -0,0 +1,159 @@ +import type { Dayjs } from 'dayjs' +import { SmallLoadingCircle } from '@/components/common' +import { useScheduleStore } from '@/stores' +import { + Button, + Dialog, + DialogActions, + DialogContent, + DialogTitle, + FormControl, + InputLabel, + MenuItem, + Select, +} from '@mui/material' +import { generateUniqueId, useToggle } from '@zl-asica/react' +import { useCallback, useEffect, useState } from 'react' +import CustomInputField from './CustomInputField' + +interface AddTaskButtonProps { + selectedDate: Dayjs | null +} + +const AddTaskButton = ({ selectedDate }: AddTaskButtonProps) => { + const addTask = useScheduleStore(state => state.addTask) + + const [open, toggleOpen] = useToggle() + const [title, setTitle] = useState('') + const [description, setDescription] = useState('') + const [category, setCategory] = useState('work') + const [priority, setPriority] = useState('medium') + const [date, setDate] = useState( + selectedDate?.format('YYYY-MM-DD') ?? new Date().toISOString().split('T')[0], + ) + const [startTime, setStartTime] = useState('') + const [endTime, setEndTime] = useState('') + const [loading, setLoading] = useState(false) + + useEffect(() => { + setDate(selectedDate?.format('YYYY-MM-DD') ?? new Date().toISOString().split('T')[0]) + }, [selectedDate]) + + const isTimeRangeValid = !startTime || !endTime || startTime < endTime + + const resetStates = useCallback(() => { + setTitle('') + setDescription('') + setCategory('work') + setPriority('medium') + setDate(selectedDate?.format('YYYY-MM-DD') ?? new Date().toISOString().split('T')[0]) + setStartTime('') + setEndTime('') + }, [selectedDate]) + + const handleAddTask = useCallback(async () => { + setLoading(true) + const randomId = await generateUniqueId([title, description, category, priority]) + const newTask = { + taskId: randomId, + title, + description, + category, + priority, + date, + timeRange: startTime && endTime ? { start: startTime, end: endTime } : undefined, + status: 'pending' as const, + } + + try { + await addTask(newTask) + toggleOpen() + resetStates() + } + catch (error_) { + console.error('Error while adding task:', error_) + } + finally { + setLoading(false) + } + }, [title, description, category, priority, date, startTime, endTime, addTask, resetStates, toggleOpen]) + + return ( + <> + + + { + toggleOpen() + resetStates() + }} + > + Add Task + + + + + + + + + Category * + + + + Priority * + + + + + + + + + + ) +} + +export default AddTaskButton diff --git a/src/types/user.d.ts b/src/types/user.d.ts index 510a335..ee8ceae 100644 --- a/src/types/user.d.ts +++ b/src/types/user.d.ts @@ -13,12 +13,18 @@ type TaskCategory = 'work' | 'personal' | 'health' | 'learning' | 'other' | 'sch type TaskPriority = 'low' | 'medium' | 'high' +interface TaskTimeRange { + start: string + end: string +} + interface Task { taskId: string title: string description: string category: TaskCategory date: string + timeRange?: TaskTimeRange priority: TaskPriority status: 'pending' | 'completed' }