Spaces:
Sleeping
Sleeping
File size: 6,848 Bytes
5e870e6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | 'use client';
import { useState } from 'react';
import { motion } from 'framer-motion';
import { Check, Trash2, Calendar, Clock, Edit } from 'lucide-react';
import { Task } from '@/lib/types';
import { toggleTaskCompletion, deleteTask } from '@/lib/api';
import { DeleteConfirmModal } from './DeleteConfirmModal';
import { Card } from '@/components/ui/card';
import { useRouter } from 'next/navigation';
import { showToast } from '@/lib/toast';
interface TaskItemProps {
task: Task;
onTaskUpdated: () => void;
onTaskDeleted: () => void;
}
// Custom circular checkbox component with animation
const CircularCheckbox = ({ checked, onChange, disabled }: { checked: boolean; onChange: () => void; disabled?: boolean }) => {
return (
<motion.div
whileHover={{ scale: 1.1 }}
whileTap={{ scale: 0.9 }}
className="relative"
>
<button
type="button"
onClick={onChange}
disabled={disabled}
className={`w-6 h-6 rounded-full border-2 flex items-center justify-center transition-all duration-200 ${checked
? 'bg-gradient-to-r from-green-500 to-emerald-500 border-transparent shadow-lg'
: 'border-gray-600 hover:border-green-500'
} ${disabled ? 'opacity-50 cursor-not-allowed' : 'cursor-pointer'}`}
>
<motion.div
initial={false}
animate={{ scale: checked ? 1 : 0 }}
transition={{ duration: 0.2 }}
>
<Check className="w-4 h-4 text-white" />
</motion.div>
</button>
</motion.div>
);
};
export function TaskItem({ task, onTaskUpdated, onTaskDeleted }: TaskItemProps) {
const router = useRouter();
const [isDeleting, setIsDeleting] = useState(false);
const [showDeleteModal, setShowDeleteModal] = useState(false);
const [localTask, setLocalTask] = useState(task);
// Get user ID from localStorage or context (in a real app, this would come from auth context)
const getUserId = () => {
if (typeof window !== 'undefined') {
const userStr = localStorage.getItem('user');
if (userStr) {
try {
const user = JSON.parse(userStr);
return user.id;
} catch (e) {
console.error('Error parsing user from localStorage:', e);
return null;
}
}
}
return null;
};
const handleToggleCompletion = async () => {
const userId = getUserId();
if (!userId) {
console.error('User ID not found');
return;
}
try {
const updatedTask = await toggleTaskCompletion(userId, localTask.id);
setLocalTask(updatedTask);
onTaskUpdated();
showToast.success(updatedTask.completed ? 'Task completed!' : 'Task reopened');
} catch (error) {
console.error('Error toggling task completion:', error);
showToast.error('Failed to update task status');
}
};
const handleDeleteConfirm = async () => {
const userId = getUserId();
if (!userId) {
console.error('User ID not found');
return;
}
try {
setIsDeleting(true);
await deleteTask(userId, localTask.id);
onTaskDeleted();
showToast.success('Task deleted successfully');
setShowDeleteModal(false);
} catch (error) {
console.error('Error deleting task:', error);
showToast.error('Failed to delete task');
setIsDeleting(false);
}
};
const handleEdit = () => {
router.push(`/tasks/${task.id}`);
};
return (
<>
<Card
className={`bg-gray-900/50 backdrop-blur-xl border-gray-800 p-4 hover:border-gray-700 transition-all duration-300 ${localTask.completed ? 'opacity-70' : 'hover:shadow-lg hover:shadow-indigo-500/10'
}`}
>
<div className="flex items-start justify-between">
<div className="flex items-start space-x-3 flex-1 min-w-0">
<CircularCheckbox
checked={localTask.completed}
onChange={handleToggleCompletion}
/>
<div className="min-w-0 flex-1">
<p
className={`text-base font-medium ${localTask.completed
? 'text-gray-400 line-through'
: 'text-white'
}`}
>
{localTask.title}
{localTask.is_ai_generated && (
<span className="ml-2 inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium bg-indigo-900 text-indigo-200">
AI
</span>
)}
</p>
{localTask.description && (
<p className="text-sm text-gray-400 mt-1 line-clamp-2">
{localTask.description}
</p>
)}
<div className="flex items-center text-xs text-gray-500 mt-3 space-x-4">
<div className="flex items-center">
<Calendar className="w-3 h-3 mr-1" />
<span>Created: {new Date(localTask.created_at).toLocaleDateString()}</span>
</div>
{localTask.due_date && (
<div className={`flex items-center ${new Date(localTask.due_date) < new Date() && !localTask.completed ? 'text-red-400' : 'text-indigo-400'}`}>
<Clock className="w-3 h-3 mr-1" />
<span>Due: {new Date(localTask.due_date).toLocaleDateString()}</span>
</div>
)}
{localTask.updated_at !== localTask.created_at && (
<div className="flex items-center">
<Clock className="w-3 h-3 mr-1" />
<span>Edited</span>
</div>
)}
</div>
</div>
</div>
<div className="flex space-x-1 ml-4">
<motion.button
whileHover={{ scale: 1.1 }}
whileTap={{ scale: 0.9 }}
onClick={handleEdit}
className="p-2 text-gray-400 hover:text-indigo-400 hover:bg-indigo-900/30 rounded-lg transition-colors"
>
<Edit className="w-4 h-4" />
</motion.button>
<motion.button
whileHover={{ scale: 1.1 }}
whileTap={{ scale: 0.9 }}
onClick={() => setShowDeleteModal(true)}
disabled={isDeleting}
className="p-2 text-gray-400 hover:text-red-400 hover:bg-red-900/30 rounded-lg transition-colors"
>
<Trash2 className="w-4 h-4" />
</motion.button>
</div>
</div>
</Card>
<DeleteConfirmModal
isOpen={showDeleteModal}
onClose={() => setShowDeleteModal(false)}
onConfirm={handleDeleteConfirm}
taskTitle={localTask.title}
loading={isDeleting}
/>
</>
);
}
|