Skip to content

Commit f7f0f0c

Browse files
vanch3dcaoccao
authored andcommitted
Merge pull request #1024
* refactor(33943): change the draft SCHEMA nodes when publishing is suc… * refactor(33943): refactor the toasts to avoid stampede * refactor(33943): refactor the toasts to avoid stampede * test(33943): fix tests
1 parent 3a52a87 commit f7f0f0c

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

hivemq-edge-frontend/src/extensions/datahub/components/toolbar/ToolbarPublish.spec.cy.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ describe('ToolbarPublish', () => {
1919
cy.get('button').click()
2020
cy.wait('@getPolicies')
2121
cy.get('[role="status"] div').should('have.attr', 'data-status', 'error')
22-
cy.get('[role="status"] div#toast-publish-error-title').should('have.text', 'Error publishing Data Policy')
23-
cy.get('[role="status"] div#toast-publish-error-description').should('have.text', 'Not Found')
22+
cy.get('[role="status"] div#toast-publish-error-DATA_POLICY-title').should(
23+
'have.text',
24+
'Error publishing Data Policy'
25+
)
26+
cy.get('[role="status"] div#toast-publish-error-DATA_POLICY-description').should('have.text', 'Not Found')
2427
})
2528

2629
it('should handle success', () => {
@@ -31,8 +34,8 @@ describe('ToolbarPublish', () => {
3134
cy.get('button').click()
3235
cy.wait('@getPolicies')
3336
cy.get('[role="status"] div').should('have.attr', 'data-status', 'success')
34-
cy.get('[role="status"] div#toast-publish-success-title').should('have.text', 'Data Policy published')
35-
cy.get('[role="status"] div#toast-publish-success-description').should(
37+
cy.get('[role="status"] div#toast-publish-success-DATA_POLICY-title').should('have.text', 'Data Policy published')
38+
cy.get('[role="status"] div#toast-publish-success-DATA_POLICY-description').should(
3639
'have.text',
3740
"We've created a new Data Policy for you."
3841
)

hivemq-edge-frontend/src/extensions/datahub/components/toolbar/ToolbarPublish.tsx

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import type { FC } from 'react'
22
import { useTranslation } from 'react-i18next'
33
import { useNavigate } from 'react-router-dom'
4+
import type { Node } from '@xyflow/react'
45
import type { UseMutateAsyncFunction } from '@tanstack/react-query'
6+
import type { UseToastOptions } from '@chakra-ui/react'
57
import { Button, Icon, useToast } from '@chakra-ui/react'
68
import { MdPublishedWithChanges } from 'react-icons/md'
79

@@ -19,7 +21,7 @@ import { usePolicyChecksStore } from '@datahub/hooks/usePolicyChecksStore.ts'
1921
import { usePolicyGuards } from '@datahub/hooks/usePolicyGuards.ts'
2022

2123
import { dataHubToastOption } from '@datahub/utils/toast.utils.ts'
22-
import type { DryRunResults, ResourceState } from '@datahub/types.ts'
24+
import type { DryRunResults, ResourceState, SchemaData } from '@datahub/types.ts'
2325
import { DataHubNodeType, ResourceWorkingVersion, DesignerStatus } from '@datahub/types.ts'
2426

2527
// Should be PolicySchema | Script | DataPolicy | BehaviorPolicy
@@ -50,7 +52,7 @@ const resourceReducer =
5052

5153
export const ToolbarPublish: FC = () => {
5254
const { t } = useTranslation('datahub')
53-
const { status } = useDataHubDraftStore()
55+
const { status, nodes, onUpdateNodes } = useDataHubDraftStore()
5456
const { report, node: selectedNode, setNode, reset } = usePolicyChecksStore()
5557
const createSchema = useCreateSchema()
5658
const createScript = useCreateScript()
@@ -64,31 +66,62 @@ export const ToolbarPublish: FC = () => {
6466

6567
const isValid = !!report && report.length >= 1 && report?.every((e) => !e.error)
6668

69+
// TODO[NVL] The routine doesn't change title/description based on the number of resources published
70+
const manageToast = (id: string, conf: UseToastOptions) => {
71+
const { id: _, ...cleanConfig } = conf
72+
if (!toast.isActive(id)) toast({ ...cleanConfig, id })
73+
else toast.update(id, cleanConfig)
74+
}
75+
6776
const toastInternalError = (message: string) => {
68-
toast({
77+
manageToast(`publish-internal-${selectedNode?.type}`, {
6978
...dataHubToastOption,
7079
title: t('publish.internal.title', { source: selectedNode?.type }),
7180
description: message,
7281
status: 'error',
73-
id: 'publish-internal',
7482
})
7583
}
7684

7785
const reportMutation = (promise: Promise<unknown>, type?: string) => {
7886
promise
7987
.then(() => {
80-
toast({
88+
manageToast(`publish-success-${type || selectedNode?.type}`, {
8189
...dataHubToastOption,
8290
title: t('publish.success.title', { source: type || selectedNode?.type }),
8391
description: t('publish.success.description', { source: type || selectedNode?.type, context: status }),
8492
status: 'success',
85-
id: 'publish-success',
8693
})
8794
})
8895
.catch(() => {})
8996
return promise
9097
}
9198

99+
/**
100+
* This routine is called on successful publish of a resource node (script or schema), in order to update a draft node to its published version.
101+
* In case the main policy fails, the graph will be holding the newly published resource nodes, but the main policy will not be updated.
102+
* TODO[NVL] This is so much a hack, but we need to update the draft resource nodes and we lost the link between nodes and mutation requests
103+
* The whole publish process needs to be refactored
104+
*/
105+
const updateDraftResourceNodes = (request: Mutate<PolicySchema> | Mutate<Script>) => (value: unknown) => {
106+
if (request.type === DataHubNodeType.SCHEMA) {
107+
const draftSchemaNodes = nodes.filter(
108+
(node): node is Node<SchemaData> =>
109+
node.type === DataHubNodeType.SCHEMA &&
110+
node.data.name === request.payload.id &&
111+
// if this is a draft, versions MUST be 1 AND ResourceWorkingVersion.DRAFT
112+
node.data.version === ResourceWorkingVersion.DRAFT &&
113+
(value as PolicySchema).version === 1
114+
)
115+
116+
draftSchemaNodes.forEach((node) => {
117+
onUpdateNodes<SchemaData>(node.id, {
118+
...node.data,
119+
version: 1,
120+
})
121+
})
122+
}
123+
}
124+
92125
const publishResources = (resources?: DryRunResults<never>[]) => {
93126
const allSchemas =
94127
resources
@@ -107,7 +140,7 @@ export const ToolbarPublish: FC = () => {
107140
})) || []
108141

109142
return [...allSchemas, ...allScripts].map((request) =>
110-
reportMutation(request.mutation(request.payload), request.type)
143+
reportMutation(request.mutation(request.payload), request.type).then(updateDraftResourceNodes(request))
111144
)
112145
}
113146

@@ -184,12 +217,12 @@ export const ToolbarPublish: FC = () => {
184217
let message
185218
if (error instanceof Error) message = error.message
186219
else message = String(error)
187-
return toast({
220+
221+
manageToast(`publish-error-${selectedNode?.type}`, {
188222
...dataHubToastOption,
189223
title: t('publish.error.title', { source: selectedNode?.type }),
190224
description: message.toString(),
191225
status: 'error',
192-
id: 'publish-error',
193226
})
194227
})
195228
}

0 commit comments

Comments
 (0)