summaryrefslogtreecommitdiff
path: root/opendc-web/opendc-web-ui/src/data/topology.js
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2021-07-07 20:13:30 +0200
committerFabian Mastenbroek <mail.fabianm@gmail.com>2021-07-08 10:53:23 +0200
commit02a2f0f89cb1f39a5f8856bca1971a4e1b12374f (patch)
treedcea2fb2f46f47b0f5a961a52023510d227b5936 /opendc-web/opendc-web-ui/src/data/topology.js
parent9c8a987556d0fb0cdf0eb67e0c191a8dcc5593b9 (diff)
ui: Use React Query defaults to reduce duplication
Diffstat (limited to 'opendc-web/opendc-web-ui/src/data/topology.js')
-rw-r--r--opendc-web/opendc-web-ui/src/data/topology.js55
1 files changed, 37 insertions, 18 deletions
diff --git a/opendc-web/opendc-web-ui/src/data/topology.js b/opendc-web/opendc-web-ui/src/data/topology.js
index 4c746a7e..92911a70 100644
--- a/opendc-web/opendc-web-ui/src/data/topology.js
+++ b/opendc-web/opendc-web-ui/src/data/topology.js
@@ -21,7 +21,40 @@
*/
import { useSelector } from 'react-redux'
-import { useActiveProjectId, useProject } from './project'
+import { useQueries } from 'react-query'
+import { addTopology, deleteTopology, fetchTopology, updateTopology } from '../api/topologies'
+
+/**
+ * Configure the query defaults for the topology endpoints.
+ */
+export function configureTopologyClient(queryClient, auth) {
+ queryClient.setQueryDefaults('topologies', { queryFn: ({ queryKey }) => fetchTopology(auth, queryKey[1]) })
+
+ queryClient.setMutationDefaults('addTopology', {
+ mutationFn: (data) => addTopology(auth, data),
+ onSuccess: async (result) => {
+ queryClient.setQueryData(['projects', result.projectId], (old) => ({
+ ...old,
+ topologyIds: [...old.topologyIds, result._id],
+ }))
+ queryClient.setQueryData(['topologies', result._id], result)
+ },
+ })
+ queryClient.setMutationDefaults('updateTopology', {
+ mutationFn: (data) => updateTopology(auth, data),
+ onSuccess: async (result) => queryClient.setQueryData(['topologies', result._id], result),
+ })
+ queryClient.setMutationDefaults('deleteTopology', {
+ mutationFn: (id) => deleteTopology(auth, id),
+ onSuccess: async (result) => {
+ queryClient.setQueryData(['projects', result.projectId], (old) => ({
+ ...old,
+ topologyIds: old.topologyIds.filter((id) => id !== result._id),
+ }))
+ queryClient.removeQueries(['topologies', result._id])
+ },
+ })
+}
/**
* Return the current active topology.
@@ -31,22 +64,8 @@ export function useActiveTopology() {
}
/**
- * Return the topologies for the active project.
+ * Return the scenarios with the specified identifiers.
*/
-export function useProjectTopologies() {
- const projectId = useActiveProjectId()
- const { data: project } = useProject(projectId)
- return useSelector(({ objects }) => {
- if (!project) {
- return []
- }
-
- const topologies = project.topologyIds.map((t) => objects.topology[t])
-
- if (topologies.filter((t) => !t).length > 0) {
- return []
- }
-
- return topologies
- })
+export function useTopologies(topologyIds) {
+ return useQueries(topologyIds.map((topologyId) => ({ queryKey: ['topologies', topologyId] })))
}