-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: started to add react-query - Ref gestion-de-projet#2507
- Loading branch information
Showing
17 changed files
with
276 additions
and
78 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useMutation, useQueryClient } from '@tanstack/react-query' | ||
import services from 'services/aphp' | ||
import { ProjectType } from 'types' | ||
|
||
const useCreateProject = () => { | ||
const queryClient = useQueryClient() | ||
|
||
return useMutation({ | ||
mutationFn: async (newProjectData: Omit<ProjectType, 'uuid'>) => await services.projects.addProject(newProjectData), | ||
onSuccess: () => { | ||
queryClient.invalidateQueries({ queryKey: ['projects'] }) | ||
} | ||
}) | ||
} | ||
|
||
export default useCreateProject |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useMutation, useQueryClient } from '@tanstack/react-query' | ||
import services from 'services/aphp' | ||
import { ProjectType } from 'types' | ||
|
||
const useDeleteProject = () => { | ||
const queryClient = useQueryClient() | ||
|
||
return useMutation({ | ||
mutationFn: async (project: ProjectType) => await services.projects.deleteProject(project), | ||
onSuccess: () => { | ||
queryClient.invalidateQueries({ queryKey: ['projects'] }) | ||
} | ||
}) | ||
} | ||
|
||
export default useDeleteProject |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useMutation, useQueryClient } from '@tanstack/react-query' | ||
import services from 'services/aphp' | ||
import { ProjectType } from 'types' | ||
|
||
const useEditProject = () => { | ||
const queryClient = useQueryClient() | ||
|
||
return useMutation({ | ||
mutationFn: async (newProjectData: ProjectType) => await services.projects.editProject(newProjectData), | ||
onSuccess: () => { | ||
queryClient.invalidateQueries({ queryKey: ['projects'] }) | ||
} | ||
}) | ||
} | ||
|
||
export default useEditProject |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import services from 'services/aphp' | ||
import { useQuery } from '@tanstack/react-query' | ||
|
||
const useProject = (projectId?: string) => { | ||
const { data, isLoading, isError, error, refetch } = useQuery({ | ||
queryKey: ['project', projectId], | ||
enabled: !!projectId, // ça veut dire qu'on fait pas l'appel si pas de projectId | ||
queryFn: async () => { | ||
if (!projectId) return null | ||
const projectData = await services.projects.fetchProject(projectId) | ||
return projectData | ||
} | ||
}) | ||
|
||
return { | ||
project: data, | ||
projectLoading: isLoading, | ||
projectIsError: isError, | ||
error, | ||
refetch | ||
} | ||
} | ||
|
||
export default useProject |
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,26 +1,28 @@ | ||
import React, { useEffect, useState } from 'react' | ||
import services from 'services/aphp' | ||
import { useQuery } from '@tanstack/react-query' | ||
|
||
const useProjects = (searchTerm: string, startDate?: string, endDate?: string) => { | ||
const [projectsList, setProjectsList] = useState<any[]>([]) | ||
const [total, setTotal] = useState(0) | ||
const [loading, setLoading] = useState(false) | ||
|
||
// TODO: à externaliser | ||
const useProjects = (searchInput: string, startDate?: string, endDate?: string) => { | ||
const fetchProjectsList = async () => { | ||
// TODO: modifier le service de sorte à ajouter les filtres + try/catch | ||
const projectsList = await services.projects.fetchProjectsList() | ||
console.log('test projectsList', projectsList) | ||
setProjectsList(projectsList.results) | ||
setTotal(projectsList.count) | ||
const projectsList = await services.projects.fetchProjectsList(searchInput, startDate, endDate) | ||
return projectsList | ||
} | ||
|
||
useEffect(() => { | ||
// setLoading(true) | ||
console.log('test fetchPRoejctsList()', fetchProjectsList()) | ||
}, [searchTerm, startDate, endDate]) | ||
const { data, isLoading, isError, error, refetch } = useQuery({ | ||
queryKey: ['projects', searchInput, startDate, endDate], | ||
queryFn: fetchProjectsList | ||
}) | ||
|
||
const projectsList = data?.results ?? [] | ||
const total = data?.count ?? 0 | ||
|
||
return { projectsList, total, loading } | ||
return { | ||
projectsList, | ||
total, | ||
loading: isLoading, | ||
isError, | ||
error, | ||
refetch | ||
} | ||
} | ||
|
||
export default useProjects |
Oops, something went wrong.