summaryrefslogtreecommitdiff
path: root/opendc-web/opendc-web-ui/src/data
diff options
context:
space:
mode:
Diffstat (limited to 'opendc-web/opendc-web-ui/src/data')
-rw-r--r--opendc-web/opendc-web-ui/src/data/map.js4
-rw-r--r--opendc-web/opendc-web-ui/src/data/project.js52
-rw-r--r--opendc-web/opendc-web-ui/src/data/topology.js10
3 files changed, 27 insertions, 39 deletions
diff --git a/opendc-web/opendc-web-ui/src/data/map.js b/opendc-web/opendc-web-ui/src/data/map.js
index 6aef6ac5..348a6664 100644
--- a/opendc-web/opendc-web-ui/src/data/map.js
+++ b/opendc-web/opendc-web-ui/src/data/map.js
@@ -35,7 +35,3 @@ export function useMapScale() {
export function useMapPosition() {
return useSelector((state) => state.map.position)
}
-
-export function useMapDimensions() {
- return useSelector((state) => state.map.dimensions)
-}
diff --git a/opendc-web/opendc-web-ui/src/data/project.js b/opendc-web/opendc-web-ui/src/data/project.js
index 9bdcfb93..9dcd8532 100644
--- a/opendc-web/opendc-web-ui/src/data/project.js
+++ b/opendc-web/opendc-web-ui/src/data/project.js
@@ -20,9 +20,8 @@
* SOFTWARE.
*/
-import { useQueries, useQuery } from 'react-query'
+import { useQuery } from 'react-query'
import { addProject, deleteProject, fetchProject, fetchProjects } from '../api/projects'
-import { useRouter } from 'next/router'
import { addPortfolio, deletePortfolio, fetchPortfolio, fetchPortfoliosOfProject } from '../api/portfolios'
import { addScenario, deleteScenario, fetchScenario, fetchScenariosOfPortfolio } from '../api/scenarios'
@@ -38,6 +37,7 @@ export function configureProjectClient(queryClient, auth) {
mutationFn: (data) => addProject(auth, data),
onSuccess: async (result) => {
queryClient.setQueryData('projects', (old = []) => [...old, result])
+ queryClient.setQueryData(['projects', result._id], result)
},
})
queryClient.setMutationDefaults('deleteProject', {
@@ -61,6 +61,7 @@ export function configureProjectClient(queryClient, auth) {
...old,
portfolioIds: [...old.portfolioIds, result._id],
}))
+ queryClient.setQueryData(['project-portfolios', result.projectId], (old = []) => [...old, result])
queryClient.setQueryData(['portfolios', result._id], result)
},
})
@@ -71,6 +72,9 @@ export function configureProjectClient(queryClient, auth) {
...old,
portfolioIds: old.portfolioIds.filter((id) => id !== result._id),
}))
+ queryClient.setQueryData(['project-portfolios', result.projectId], (old = []) =>
+ old.filter((portfolio) => portfolio._id !== result._id)
+ )
queryClient.removeQueries(['portfolios', result._id])
},
})
@@ -86,6 +90,7 @@ export function configureProjectClient(queryClient, auth) {
onSuccess: async (result) => {
// Register updated scenario in cache
queryClient.setQueryData(['scenarios', result._id], result)
+ queryClient.setQueryData(['portfolio-scenarios', result.portfolioId], (old = []) => [...old, result])
// Add scenario id to portfolio
queryClient.setQueryData(['portfolios', result.portfolioId], (old) => ({
@@ -101,6 +106,9 @@ export function configureProjectClient(queryClient, auth) {
...old,
scenarioIds: old.scenarioIds.filter((id) => id !== result._id),
}))
+ queryClient.setQueryData(['portfolio-scenarios', result.portfolioId], (old = []) =>
+ old.filter((scenario) => scenario._id !== result._id)
+ )
queryClient.removeQueries(['scenarios', result._id])
},
})
@@ -109,54 +117,34 @@ export function configureProjectClient(queryClient, auth) {
/**
* Return the available projects.
*/
-export function useProjects() {
- return useQuery('projects')
+export function useProjects(options = {}) {
+ return useQuery('projects', options)
}
/**
* Return the project with the specified identifier.
*/
-export function useProject(projectId) {
- return useQuery(['projects', projectId], { enabled: !!projectId })
+export function useProject(projectId, options = {}) {
+ return useQuery(['projects', projectId], { enabled: !!projectId, ...options })
}
/**
* Return the portfolio with the specified identifier.
*/
-export function usePortfolio(portfolioId) {
- return useQuery(['portfolios', portfolioId], { enabled: !!portfolioId })
+export function usePortfolio(portfolioId, options = {}) {
+ return useQuery(['portfolios', portfolioId], { enabled: !!portfolioId, ...options })
}
/**
* Return the portfolios of the specified project.
*/
-export function useProjectPortfolios(projectId) {
- return useQuery(['project-portfolios', projectId], { enabled: !!projectId })
-}
-
-/**
- * Return the scenarios with the specified identifiers.
- */
-export function useScenarios(scenarioIds) {
- return useQueries(
- scenarioIds.map((scenarioId) => ({
- queryKey: ['scenarios', scenarioId],
- }))
- )
+export function useProjectPortfolios(projectId, options = {}) {
+ return useQuery(['project-portfolios', projectId], { enabled: !!projectId, ...options })
}
/**
* Return the scenarios of the specified portfolio.
*/
-export function usePortfolioScenarios(portfolioId) {
- return useQuery(['portfolio-scenarios', portfolioId], { enabled: !!portfolioId })
-}
-
-/**
- * Return the current active project identifier.
- */
-export function useActiveProjectId() {
- const router = useRouter()
- const { project } = router.query
- return project
+export function usePortfolioScenarios(portfolioId, options = {}) {
+ return useQuery(['portfolio-scenarios', portfolioId], { enabled: !!portfolioId, ...options })
}
diff --git a/opendc-web/opendc-web-ui/src/data/topology.js b/opendc-web/opendc-web-ui/src/data/topology.js
index 8db75877..14bd7562 100644
--- a/opendc-web/opendc-web-ui/src/data/topology.js
+++ b/opendc-web/opendc-web-ui/src/data/topology.js
@@ -21,7 +21,7 @@
*/
import { useSelector } from 'react-redux'
-import { useQueries, useQuery } from 'react-query'
+import { useQuery } from 'react-query'
import { addTopology, deleteTopology, fetchTopologiesOfProject, fetchTopology, updateTopology } from '../api/topologies'
/**
@@ -40,6 +40,7 @@ export function configureTopologyClient(queryClient, auth) {
...old,
topologyIds: [...old.topologyIds, result._id],
}))
+ queryClient.setQueryData(['project-topologies', result.projectId], (old = []) => [...old, result])
queryClient.setQueryData(['topologies', result._id], result)
},
})
@@ -54,6 +55,9 @@ export function configureTopologyClient(queryClient, auth) {
...old,
topologyIds: old.topologyIds.filter((id) => id !== result._id),
}))
+ queryClient.setQueryData(['project-topologies', result.projectId], (old = []) =>
+ old.filter((topology) => topology._id !== result._id)
+ )
queryClient.removeQueries(['topologies', result._id])
},
})
@@ -69,6 +73,6 @@ export function useActiveTopology() {
/**
* Return the topologies of the specified project.
*/
-export function useProjectTopologies(projectId) {
- return useQuery(['project-topologies', projectId], { enabled: !!projectId })
+export function useProjectTopologies(projectId, options = {}) {
+ return useQuery(['project-topologies', projectId], { enabled: !!projectId, ...options })
}