summaryrefslogtreecommitdiff
path: root/opendc-web/opendc-web-ui/src/data/project.js
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2021-07-07 16:27:49 +0200
committerFabian Mastenbroek <mail.fabianm@gmail.com>2021-07-07 16:28:25 +0200
commite5e5d2c65e583493870bc0b62fb185c5e757c13f (patch)
treedc9bc25517ce36e155932212f598bf2375519d34 /opendc-web/opendc-web-ui/src/data/project.js
parentaa788a3ad18badfac8beaabdaffc88b9e52f9306 (diff)
ui: Migrate project APIs to React Query
This change updates the OpenDC frontend to use React Query for fetching and mutating project data. Previously, this state was tracked and synchronized via Redux. Migrating to React Query greatly simplifies the state synchronization logic necessary in the frontend.
Diffstat (limited to 'opendc-web/opendc-web-ui/src/data/project.js')
-rw-r--r--opendc-web/opendc-web-ui/src/data/project.js22
1 files changed, 13 insertions, 9 deletions
diff --git a/opendc-web/opendc-web-ui/src/data/project.js b/opendc-web/opendc-web-ui/src/data/project.js
index 30b36efa..308930e5 100644
--- a/opendc-web/opendc-web-ui/src/data/project.js
+++ b/opendc-web/opendc-web-ui/src/data/project.js
@@ -21,39 +21,43 @@
*/
import { useSelector } from 'react-redux'
+import { useQuery } from 'react-query'
+import { fetchProject, fetchProjects } from '../api/projects'
+import { useAuth } from '../auth'
import { useRouter } from 'next/router'
/**
* Return the available projects.
*/
export function useProjects() {
- return useSelector((state) => state.projects)
+ const auth = useAuth()
+ return useQuery('projects', () => fetchProjects(auth))
}
/**
* Return the project with the specified identifier.
*/
export function useProject(projectId) {
- return useSelector((state) => state.projects[projectId])
+ const auth = useAuth()
+ return useQuery(`projects/${projectId}`, () => fetchProject(auth, projectId), { enabled: !!projectId })
}
/**
- * Return the current active project.
+ * Return the current active project identifier.
*/
-export function useActiveProject() {
+export function useActiveProjectId() {
const router = useRouter()
- const { project: projectId } = router.query
- return useSelector((state) => state.objects.project[projectId])
+ const { project } = router.query
+ return project
}
/**
* Return the portfolios for the specified project id.
*/
export function usePortfolios(projectId) {
+ const { data: project } = useProject(projectId)
return useSelector((state) => {
- let portfolios = state.objects.project[projectId]
- ? state.objects.project[projectId].portfolioIds.map((t) => state.objects.portfolio[t])
- : []
+ let portfolios = project?.portfolioIds?.map((t) => state.objects.portfolio[t]) ?? []
if (portfolios.filter((t) => !t).length > 0) {
portfolios = []
}