-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix projects dropdown in tasks modal
- Loading branch information
Showing
8 changed files
with
150 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,76 @@ | ||
import useSWR from 'swr'; | ||
// app/frontend/hooks/useFetchProjects.ts | ||
|
||
import { useState, useEffect } from 'react'; | ||
import { Project } from '../entities/Project'; | ||
import { fetcher } from '../utils/fetcher'; | ||
|
||
interface ProjectsAPIResponse { | ||
interface UseFetchProjectsOptions { | ||
activeFilter?: string; | ||
areaFilter?: string; | ||
} | ||
|
||
interface UseFetchProjectsResult { | ||
projects: Project[]; | ||
task_status_counts: Record<number, any>; | ||
taskStatusCounts?: any; | ||
isLoading: boolean; | ||
isError: boolean; | ||
mutate: () => void; | ||
} | ||
|
||
import { useMemo } from 'react'; | ||
const useFetchProjects = (options?: UseFetchProjectsOptions): UseFetchProjectsResult => { | ||
const [projects, setProjects] = useState<Project[]>([]); | ||
const [taskStatusCounts, setTaskStatusCounts] = useState<any>(); | ||
const [isLoading, setIsLoading] = useState<boolean>(true); | ||
const [isError, setIsError] = useState<boolean>(false); | ||
|
||
const useFetchProjects = (activeFilter: string, areaFilter: string) => { | ||
const url = useMemo(() => { | ||
const queryParams = new URLSearchParams(); | ||
const fetchProjects = async () => { | ||
setIsLoading(true); | ||
setIsError(false); | ||
try { | ||
let url = '/api/projects'; | ||
const params = new URLSearchParams(); | ||
|
||
if (activeFilter !== 'all') queryParams.append('active', activeFilter); | ||
if (areaFilter) queryParams.append('area_id', areaFilter); | ||
if (options?.activeFilter !== undefined) { | ||
params.append('active', String(options.activeFilter)); | ||
} | ||
if (options?.areaFilter !== undefined) { | ||
params.append('area', options.areaFilter); | ||
} | ||
|
||
return `/api/projects?${queryParams.toString()}`; | ||
}, [activeFilter, areaFilter]); | ||
if (params.toString()) { | ||
url += `?${params.toString()}`; | ||
} | ||
|
||
const { data, error, mutate } = useSWR<ProjectsAPIResponse>(url, fetcher); | ||
const response = await fetch(url, { | ||
credentials: 'include', | ||
headers: { Accept: 'application/json' }, | ||
}); | ||
|
||
return { | ||
projects: data?.projects || [], | ||
taskStatusCounts: data?.task_status_counts || {}, | ||
isLoading: !error && !data, | ||
isError: error, | ||
mutate, | ||
if (response.ok) { | ||
const data = await response.json(); | ||
|
||
if (data.projects) { | ||
setProjects(data.projects); | ||
setTaskStatusCounts(data.taskStatusCounts); | ||
} else { | ||
setProjects(data); | ||
setTaskStatusCounts(undefined); | ||
} | ||
} else { | ||
throw new Error('Failed to fetch projects.'); | ||
} | ||
} catch (error) { | ||
console.error('Error fetching projects:', error); | ||
setIsError(true); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
fetchProjects(); | ||
}, [options?.activeFilter, options?.areaFilter]); | ||
|
||
return { projects, taskStatusCounts, isLoading, isError, mutate: fetchProjects }; | ||
}; | ||
|
||
export default useFetchProjects; |
Oops, something went wrong.