Skip to content

Commit 942fd34

Browse files
authored
Merge pull request #1021
* fix(33994): fix initial state for the schema panel * fix(33994): fix initial state for the function panel * fix(33994): add computation of internal state for resources * test(33994): add tests * test(33994): fix tests * fix(33994): a bit of cleaning
1 parent 6f812eb commit 942fd34

File tree

4 files changed

+88
-26
lines changed

4 files changed

+88
-26
lines changed

hivemq-edge-frontend/src/extensions/datahub/designer/schema/SchemaPanel.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import type { CustomValidator, UiSchema } from '@rjsf/utils'
66
import type { IChangeEvent } from '@rjsf/core'
77
import { Card, CardBody } from '@chakra-ui/react'
88

9+
import type { PolicySchema } from '@/api/__generated__'
10+
import ErrorMessage from '@/components/ErrorMessage.tsx'
911
import { enumFromStringValue } from '@/utils/types.utils.ts'
1012

11-
import type { PanelProps, SchemaData } from '@datahub/types.ts'
12-
import { ResourceStatus, ResourceWorkingVersion, SchemaType } from '@datahub/types.ts'
1313
import { MOCK_JSONSCHEMA_SCHEMA, MOCK_PROTOBUF_SCHEMA } from '@datahub/__test-utils__/schema.mocks.ts'
1414
import { useGetAllSchemas } from '@datahub/api/hooks/DataHubSchemasService/useGetAllSchemas.ts'
1515
import { ReactFlowSchemaForm } from '@datahub/components/forms/ReactFlowSchemaForm.tsx'
@@ -18,19 +18,23 @@ import { MOCK_SCHEMA_SCHEMA } from '@datahub/designer/schema/SchemaData.ts'
1818
import { getSchemaFamilies } from '@datahub/designer/schema/SchemaNode.utils.ts'
1919
import useDataHubDraftStore from '@datahub/hooks/useDataHubDraftStore.ts'
2020
import { usePolicyGuards } from '@datahub/hooks/usePolicyGuards.ts'
21-
import ErrorMessage from '@/components/ErrorMessage.tsx'
21+
import { ResourceStatus, ResourceWorkingVersion, SchemaType } from '@datahub/types.ts'
22+
import type { PanelProps, SchemaData } from '@datahub/types.ts'
23+
import { getResourceInternalStatus } from '@datahub/utils/policy.utils.ts'
2224

2325
export const SchemaPanel: FC<PanelProps> = ({ selectedNode, onFormSubmit }) => {
2426
const { data: allSchemas } = useGetAllSchemas()
2527
const { nodes } = useDataHubDraftStore()
2628
const { guardAlert, isNodeEditable } = usePolicyGuards(selectedNode)
2729
const [formData, setFormData] = useState<SchemaData | null>(() => {
30+
if (!allSchemas) return null
2831
const adapterNode = nodes.find((e) => e.id === selectedNode) as Node<SchemaData> | undefined
32+
if (!adapterNode) return null
2933

30-
const internalStatus =
31-
typeof adapterNode?.data.version === 'number' ? ResourceStatus.LOADED : adapterNode?.data.version
34+
const internalState = getResourceInternalStatus<PolicySchema>(adapterNode.data.name, allSchemas, getSchemaFamilies)
35+
const intData: SchemaData = { ...adapterNode.data, ...internalState }
3236

33-
return adapterNode ? { ...adapterNode.data, internalStatus } : null
37+
return intData
3438
})
3539

3640
const onReactFlowSchemaFormChange = useCallback(

hivemq-edge-frontend/src/extensions/datahub/designer/script/FunctionPanel.tsx

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,35 @@ import { Card, CardBody } from '@chakra-ui/react'
55
import type { UiSchema } from '@rjsf/utils'
66
import type { IChangeEvent } from '@rjsf/core'
77

8+
import type { Script } from '@/api/__generated__'
9+
import ErrorMessage from '@/components/ErrorMessage.tsx'
10+
811
import { MOCK_JAVASCRIPT_SCHEMA } from '@datahub/__test-utils__/schema.mocks.ts'
9-
import type { FunctionData, PanelProps } from '@datahub/types.ts'
10-
import { ResourceStatus, ResourceWorkingVersion } from '@datahub/types.ts'
11-
import useDataHubDraftStore from '@datahub/hooks/useDataHubDraftStore.ts'
12-
import { useGetAllScripts } from '@datahub/api/hooks/DataHubScriptsService/useGetAllScripts.ts'
1312
import { ReactFlowSchemaForm } from '@datahub/components/forms/ReactFlowSchemaForm.tsx'
1413
import { datahubRJSFWidgets } from '@datahub/designer/datahubRJSFWidgets.tsx'
1514
import { MOCK_FUNCTION_SCHEMA } from '@datahub/designer/script/FunctionData.ts'
1615
import { getScriptFamilies } from '@datahub/designer/schema/SchemaNode.utils.ts'
16+
import useDataHubDraftStore from '@datahub/hooks/useDataHubDraftStore.ts'
17+
import { useGetAllScripts } from '@datahub/api/hooks/DataHubScriptsService/useGetAllScripts.ts'
1718
import { usePolicyGuards } from '@datahub/hooks/usePolicyGuards.ts'
18-
import ErrorMessage from '@/components/ErrorMessage.tsx'
19+
import type { FunctionData, PanelProps } from '@datahub/types.ts'
20+
import { ResourceStatus, ResourceWorkingVersion } from '@datahub/types.ts'
21+
import { getResourceInternalStatus } from '@datahub/utils/policy.utils.ts'
1922

2023
export const FunctionPanel: FC<PanelProps> = ({ selectedNode, onFormSubmit }) => {
2124
const { data: allScripts } = useGetAllScripts({})
2225
const { nodes } = useDataHubDraftStore()
2326
const { guardAlert, isNodeEditable } = usePolicyGuards(selectedNode)
2427

2528
const [formData, setFormData] = useState<FunctionData | null>(() => {
29+
if (!allScripts) return null
2630
const sourceNode = nodes.find((node) => node.id === selectedNode) as Node<FunctionData> | undefined
31+
if (!sourceNode) return null
2732

28-
const internalStatus =
29-
typeof sourceNode?.data.version === 'number' ? ResourceStatus.LOADED : sourceNode?.data.version
33+
const internalState = getResourceInternalStatus<Script>(sourceNode.data.name, allScripts, getScriptFamilies)
34+
const intData: FunctionData = { ...sourceNode.data, ...internalState }
3035

31-
return sourceNode ? { ...sourceNode.data, internalStatus } : null
36+
return intData
3237
})
3338

3439
const getUISchema = (script: FunctionData | null): UiSchema => {
@@ -37,10 +42,6 @@ export const FunctionPanel: FC<PanelProps> = ({ selectedNode, onFormSubmit }) =>
3742
type: {
3843
'ui:widget': 'hidden',
3944
},
40-
// 'ui:order':
41-
// internalStatus === ResourceStatus.DRAFT || !internalStatus
42-
// ? ['name', 'type', 'schemaSource', 'messageType', 'version']
43-
// : ['name', 'version', 'schemaSource', 'messageType', 'type'],
4445
name: {
4546
'ui:widget': 'datahub:function-name',
4647
'ui:options': {

hivemq-edge-frontend/src/extensions/datahub/utils/policy.utils.spec.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import { MOCK_SCRIPT_ID, mockScript } from '@datahub/api/hooks/DataHubScriptsService/__handlers__'
2+
import { getSchemaFamilies, getScriptFamilies } from '@datahub/designer/schema/SchemaNode.utils.ts'
3+
import { ResourceStatus, ResourceWorkingVersion } from '@datahub/types.ts'
14
import { describe, expect } from 'vitest'
25

3-
import type { PolicySchema, SchemaList } from '@/api/__generated__'
4-
import { mockSchemaTempHumidity } from '@datahub/api/hooks/DataHubSchemasService/__handlers__'
5-
import { type ExpandableGroupedResource, groupResourceItems } from './policy.utils'
6+
import type { PolicySchema, SchemaList, Script } from '@/api/__generated__'
7+
import { MOCK_SCHEMA_ID, mockSchemaTempHumidity } from '@datahub/api/hooks/DataHubSchemasService/__handlers__'
8+
import { type ExpandableGroupedResource, getResourceInternalStatus, groupResourceItems } from './policy.utils'
69

710
interface GroupSchemaTest {
811
data: SchemaList | undefined
@@ -88,3 +91,34 @@ describe('groupResourceItems', () => {
8891
}
8992
)
9093
})
94+
95+
describe('getResourceInternalStatus', () => {
96+
it('should work for Schemas', () => {
97+
expect(getResourceInternalStatus<PolicySchema>('my-schema', {}, getSchemaFamilies)).toStrictEqual({
98+
internalStatus: ResourceStatus.DRAFT,
99+
version: ResourceWorkingVersion.DRAFT,
100+
})
101+
102+
expect(
103+
getResourceInternalStatus<PolicySchema>(MOCK_SCHEMA_ID, { items: [mockSchemaTempHumidity] }, getSchemaFamilies)
104+
).toStrictEqual({
105+
internalStatus: ResourceStatus.LOADED,
106+
internalVersions: [1],
107+
version: 1,
108+
})
109+
})
110+
it('should work for Scripts', () => {
111+
expect(getResourceInternalStatus<Script>('my-script', {}, getScriptFamilies)).toStrictEqual({
112+
internalStatus: ResourceStatus.DRAFT,
113+
version: ResourceWorkingVersion.DRAFT,
114+
})
115+
116+
expect(getResourceInternalStatus<Script>(MOCK_SCRIPT_ID, { items: [mockScript] }, getScriptFamilies)).toStrictEqual(
117+
{
118+
internalStatus: ResourceStatus.LOADED,
119+
internalVersions: [1],
120+
version: 1,
121+
}
122+
)
123+
})
124+
})

hivemq-edge-frontend/src/extensions/datahub/utils/policy.utils.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
// TODO[OpenAPI] This MUST be the common properties of DataHub resources, i.e. Script and Schema
2-
export interface ResourceBase {
3-
id: string
4-
readonly version?: number
5-
}
1+
import type { PolicySchema, Script } from '@/api/__generated__'
2+
import { type ResourceFamily, ResourceStatus } from '@datahub/types.ts'
3+
import type { ResourceState } from '@datahub/types.ts'
4+
import { ResourceWorkingVersion } from '@datahub/types.ts'
5+
6+
// TODO[OpenAPI]
7+
// - This MUST be the common properties of DataHub resources, i.e. Script and Schema
8+
// - Assuming that Script and Schema both have the same properties
9+
export type ResourceBase = Pick<PolicySchema, 'id' | 'version'> | Pick<Script, 'id' | 'version'>
610

711
export type ExpandableGroupedResource<T extends ResourceBase> = T & {
812
children?: T[]
@@ -39,3 +43,22 @@ export const groupResourceItems = <T extends { items?: Array<U> }, U extends Res
3943
} as ExpandableGroupedResource<U>
4044
})
4145
}
46+
47+
export const getResourceInternalStatus = <U extends ResourceBase>(
48+
id: string,
49+
allResources: { items?: U[] },
50+
formatter: (items: U[]) => Record<string, ResourceFamily>
51+
): ResourceState | undefined => {
52+
const schema = allResources.items?.findLast((resource) => resource.id === id)
53+
if (schema) {
54+
return {
55+
version: schema.version || ResourceWorkingVersion.MODIFIED,
56+
internalVersions: formatter(allResources?.items || [])[schema.id].versions,
57+
internalStatus: ResourceStatus.LOADED,
58+
}
59+
}
60+
return {
61+
internalStatus: ResourceStatus.DRAFT,
62+
version: ResourceWorkingVersion.DRAFT,
63+
}
64+
}

0 commit comments

Comments
 (0)