From cc3ad37a56fd8946350f76891f2062d219320c8f Mon Sep 17 00:00:00 2001 From: Chris Veleris Date: Tue, 12 Nov 2024 17:38:14 +0200 Subject: [PATCH] Fix projects dropdown in tasks modal --- app/frontend/Layout.tsx | 26 +++--- app/frontend/components/Projects.tsx | 40 ++++----- .../components/Sidebar/SidebarProjects.tsx | 6 +- app/frontend/components/Task/TaskModal.tsx | 4 + app/frontend/contexts/DataContext.tsx | 37 +++++++-- app/frontend/hooks/useFetchProjects.ts | 81 ++++++++++++++----- app/frontend/hooks/useManageProjects.ts | 23 +++--- public/js/bundle.js | 16 ++-- 8 files changed, 150 insertions(+), 83 deletions(-) diff --git a/app/frontend/Layout.tsx b/app/frontend/Layout.tsx index 783de91a..ccb43d68 100644 --- a/app/frontend/Layout.tsx +++ b/app/frontend/Layout.tsx @@ -46,6 +46,7 @@ const Layout: React.FC = ({ tags, areas, notes, + projects, isLoading, isError, createNote, @@ -172,6 +173,16 @@ const Layout: React.FC = ({ closeProjectModal(); }; + const handleCreateProject = async (name: string): Promise => { + try { + const newProject = await createProject({ name }); + return newProject; + } catch (error) { + console.error("Error creating project:", error); + throw error; + } + }; + const handleSaveArea = async (areaData: Area) => { try { if (areaData.id) { @@ -338,23 +349,16 @@ const Layout: React.FC = ({ task={ newTask || { id: undefined, - name: '', - status: 'not_started', + name: "", + status: "not_started", project_id: undefined, tags: [], } } onSave={handleSaveTask} onDelete={() => {}} - projects={[]} // Provide project list as necessary - onCreateProject={async (name: string) => { - return { - id: Math.random(), - name, - active: true, - pin_to_sidebar: false, - }; // Ensure all required fields are covered - }} + projects={projects} + onCreateProject={handleCreateProject} /> )} diff --git a/app/frontend/components/Projects.tsx b/app/frontend/components/Projects.tsx index 38abdece..11050d24 100644 --- a/app/frontend/components/Projects.tsx +++ b/app/frontend/components/Projects.tsx @@ -26,20 +26,12 @@ const Projects: React.FC = () => { const [projectToDelete, setProjectToDelete] = useState(null); const [isConfirmDialogOpen, setIsConfirmDialogOpen] = useState(false); const [activeDropdown, setActiveDropdown] = useState(null); - const [searchQuery, setSearchQuery] = useState(''); + const [searchQuery, setSearchQuery] = useState(""); const [searchParams, setSearchParams] = useSearchParams(); - - // Set default URL parameter ?active=true if not provided - useEffect(() => { - if (!searchParams.has("active")) { - searchParams.set("active", "true"); - setSearchParams(searchParams); - } - }, [searchParams, setSearchParams]); - const activeFilter = searchParams.get("active") ?? "active"; - const areaFilter = searchParams.get("area_id") ?? ""; + const activeFilter = searchParams.get("active") || "all"; + const areaFilter = searchParams.get("area_id") || ""; const { projects, @@ -47,16 +39,17 @@ const Projects: React.FC = () => { isLoading, isError, mutate, - } = useFetchProjects(activeFilter, areaFilter); + } = useFetchProjects({ activeFilter, areaFilter }); useEffect(() => { - setTaskStatusCounts(fetchedTaskStatusCounts); - }, [fetchedTaskStatusCounts]); + setTaskStatusCounts(fetchedTaskStatusCounts || {}); + }, [fetchedTaskStatusCounts]); const getCompletionPercentage = (projectId: number | undefined) => { if (!projectId) return 0; const taskStatus = taskStatusCounts[projectId] || {}; - const totalTasks = (taskStatus.done || 0) + (taskStatus.not_started || 0) + (taskStatus.in_progress || 0); + const totalTasks = + (taskStatus.done || 0) + (taskStatus.not_started || 0) + (taskStatus.in_progress || 0); if (totalTasks === 0) return 0; @@ -102,11 +95,13 @@ const Projects: React.FC = () => { const handleAreaFilterChange = (e: React.ChangeEvent) => { const newAreaFilter = e.target.value; const params = new URLSearchParams(searchParams); - if (newAreaFilter) { - params.set("area_id", newAreaFilter); - } else { + + if (newAreaFilter === "") { params.delete("area_id"); + } else { + params.set("area_id", newAreaFilter); } + setSearchParams(params); }; @@ -163,7 +158,7 @@ const Projects: React.FC = () => {