diff options
Diffstat (limited to 'app/assets/javascripts/clusters_list/graphql/cache_update.js')
-rw-r--r-- | app/assets/javascripts/clusters_list/graphql/cache_update.js | 84 |
1 files changed, 60 insertions, 24 deletions
diff --git a/app/assets/javascripts/clusters_list/graphql/cache_update.js b/app/assets/javascripts/clusters_list/graphql/cache_update.js index dd633820952..4d12bc8151c 100644 --- a/app/assets/javascripts/clusters_list/graphql/cache_update.js +++ b/app/assets/javascripts/clusters_list/graphql/cache_update.js @@ -1,29 +1,65 @@ import produce from 'immer'; import { getAgentConfigPath } from '../clusters_util'; +export const hasErrors = ({ errors = [] }) => errors?.length; + export function addAgentToStore(store, createClusterAgent, query, variables) { - const { clusterAgent } = createClusterAgent; - const sourceData = store.readQuery({ - query, - variables, - }); - - const data = produce(sourceData, (draftData) => { - const configuration = { - name: clusterAgent.name, - path: getAgentConfigPath(clusterAgent.name), - webPath: clusterAgent.webPath, - __typename: 'TreeEntry', - }; - - draftData.project.clusterAgents.nodes.push(clusterAgent); - draftData.project.clusterAgents.count += 1; - draftData.project.repository.tree.trees.nodes.push(configuration); - }); - - store.writeQuery({ - query, - variables, - data, - }); + if (!hasErrors(createClusterAgent)) { + const { clusterAgent } = createClusterAgent; + const sourceData = store.readQuery({ + query, + variables, + }); + + const data = produce(sourceData, (draftData) => { + const configuration = { + id: clusterAgent.id, + name: clusterAgent.name, + path: getAgentConfigPath(clusterAgent.name), + webPath: clusterAgent.webPath, + __typename: 'TreeEntry', + }; + + draftData.project.clusterAgents.nodes.push(clusterAgent); + draftData.project.clusterAgents.count += 1; + draftData.project.repository.tree.trees.nodes.push(configuration); + }); + + store.writeQuery({ + query, + variables, + data, + }); + } +} + +export function addAgentConfigToStore( + store, + clusterAgentTokenCreate, + clusterAgent, + query, + variables, +) { + if (!hasErrors(clusterAgentTokenCreate)) { + const sourceData = store.readQuery({ + query, + variables, + }); + + const data = produce(sourceData, (draftData) => { + const configuration = { + agentName: clusterAgent.name, + __typename: 'AgentConfiguration', + }; + + draftData.project.clusterAgents.nodes.push(clusterAgent); + draftData.project.agentConfigurations.nodes.push(configuration); + }); + + store.writeQuery({ + query, + variables, + data, + }); + } } |