Skip to content

Commit 491fea5

Browse files
committed
Add resource schema-related state to binding store
1 parent 5a90c26 commit 491fea5

File tree

16 files changed

+144
-123
lines changed

16 files changed

+144
-123
lines changed

src/components/capture/Create/index.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ function CaptureCreate() {
8787
return (
8888
<DetailsFormHydrator>
8989
<EndpointConfigHydrator>
90-
<ResourceConfigHydrator>
91-
<BindingHydrator>
90+
<BindingHydrator>
91+
<ResourceConfigHydrator>
9292
<MutateDraftSpecProvider value={updateDraftSpecs}>
9393
<EntityCreate
9494
entityType={entityType}
@@ -135,8 +135,8 @@ function CaptureCreate() {
135135
}
136136
/>
137137
</MutateDraftSpecProvider>
138-
</BindingHydrator>
139-
</ResourceConfigHydrator>
138+
</ResourceConfigHydrator>
139+
</BindingHydrator>
140140
</EndpointConfigHydrator>
141141
</DetailsFormHydrator>
142142
);

src/components/capture/Edit.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ function CaptureEdit() {
6969
<DraftInitializer>
7070
<DetailsFormHydrator>
7171
<EndpointConfigHydrator>
72-
<ResourceConfigHydrator>
73-
<BindingHydrator>
72+
<BindingHydrator>
73+
<ResourceConfigHydrator>
7474
<MutateDraftSpecProvider value={updateDraftSpecs}>
7575
<EntityEdit
7676
title="routeTitle.captureEdit"
@@ -115,8 +115,8 @@ function CaptureEdit() {
115115
}
116116
/>
117117
</MutateDraftSpecProvider>
118-
</BindingHydrator>
119-
</ResourceConfigHydrator>
118+
</ResourceConfigHydrator>
119+
</BindingHydrator>
120120
</EndpointConfigHydrator>
121121
</DetailsFormHydrator>
122122
</DraftInitializer>

src/components/collection/ResourceConfigForm.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import { useEffect, useMemo, useRef } from 'react';
44
import { custom_generateDefaultUISchema } from 'services/jsonforms';
55
import { jsonFormsDefaults } from 'services/jsonforms/defaults';
66
import { showValidation } from 'services/jsonforms/shared';
7+
import { useBinding_resourceSchema } from 'stores/Binding/hooks';
78
import {
89
useFormStateStore_displayValidation,
910
useFormStateStore_isActive,
1011
} from 'stores/FormState/hooks';
1112
import {
1213
useResourceConfig_resourceConfigOfCollectionProperty,
13-
useResourceConfig_resourceSchema,
1414
useResourceConfig_updateResourceConfig,
1515
} from 'stores/ResourceConfig/hooks';
1616

@@ -29,7 +29,7 @@ function ResourceConfigForm({ collectionName, readOnly = false }: Props) {
2929
);
3030

3131
const updateResourceConfig = useResourceConfig_updateResourceConfig();
32-
const resourceSchema = useResourceConfig_resourceSchema();
32+
const resourceSchema = useBinding_resourceSchema();
3333

3434
// Form State Store
3535
const displayValidation = useFormStateStore_displayValidation();

src/components/editor/Bindings/UpdateResourceConfigButton.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { AddCollectionDialogCTAProps } from 'components/shared/Entity/types';
33
import invariableStores from 'context/Zustand/invariableStores';
44

55
import { FormattedMessage } from 'react-intl';
6+
import { useBinding_resourceSchema } from 'stores/Binding/hooks';
67
import {
78
useResourceConfig_discoveredCollections,
89
useResourceConfig_setResourceConfig,
@@ -19,6 +20,8 @@ function UpdateResourceConfigButton({ toggle }: AddCollectionDialogCTAProps) {
1920
}
2021
);
2122

23+
const resourceSchema = useBinding_resourceSchema();
24+
2225
const discoveredCollections = useResourceConfig_discoveredCollections();
2326
const setResourceConfig = useResourceConfig_setResourceConfig();
2427
const setRestrictedDiscoveredCollections =
@@ -33,6 +36,7 @@ function UpdateResourceConfigButton({ toggle }: AddCollectionDialogCTAProps) {
3336

3437
setResourceConfig(
3538
value.map(({ name }) => name),
39+
resourceSchema,
3640
undefined,
3741
false,
3842
true

src/components/editor/Bindings/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { DraftSpecQuery } from 'hooks/useDraftSpecs';
1717
import { useServerUpdateRequiredMonitor } from 'hooks/useServerUpdateRequiredMonitor';
1818
import { ReactNode, useEffect, useMemo } from 'react';
1919
import { FormattedMessage, useIntl } from 'react-intl';
20+
import { useBinding_setResourceSchema } from 'stores/Binding/hooks';
2021
import {
2122
useDetailsForm_connectorImage,
2223
useDetailsForm_details_entityName,
@@ -25,7 +26,6 @@ import { useFormStateStore_messagePrefix } from 'stores/FormState/hooks';
2526
import {
2627
useResourceConfig_discoveredCollections,
2728
useResourceConfig_resetResourceConfigAndCollections,
28-
useResourceConfig_setResourceSchema,
2929
} from 'stores/ResourceConfig/hooks';
3030
import { EditorStoreNames } from 'stores/names';
3131
import { Schema } from 'types';
@@ -70,7 +70,7 @@ function BindingsMultiEditor({
7070
// Resource Config Store
7171
const discoveredCollections = useResourceConfig_discoveredCollections();
7272

73-
const setResourceSchema = useResourceConfig_setResourceSchema();
73+
const setResourceSchema = useBinding_setResourceSchema();
7474

7575
const resetResourceConfigAndCollections =
7676
useResourceConfig_resetResourceConfigAndCollections();

src/components/materialization/Create/index.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ function MaterializationCreate() {
7070
return (
7171
<DetailsFormHydrator>
7272
<EndpointConfigHydrator>
73-
<ResourceConfigHydrator>
74-
<BindingHydrator>
73+
<BindingHydrator>
74+
<ResourceConfigHydrator>
7575
<MutateDraftSpecProvider value={updateDraftSpecs}>
7676
<EntityCreate
7777
entityType={entityType}
@@ -104,8 +104,8 @@ function MaterializationCreate() {
104104
}
105105
/>
106106
</MutateDraftSpecProvider>
107-
</BindingHydrator>
108-
</ResourceConfigHydrator>
107+
</ResourceConfigHydrator>
108+
</BindingHydrator>
109109
</EndpointConfigHydrator>
110110
</DetailsFormHydrator>
111111
);

src/components/materialization/Edit.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ function MaterializationEdit() {
6666
<DraftInitializer>
6767
<DetailsFormHydrator>
6868
<EndpointConfigHydrator>
69-
<ResourceConfigHydrator>
70-
<BindingHydrator>
69+
<BindingHydrator>
70+
<ResourceConfigHydrator>
7171
<MutateDraftSpecProvider value={updateDraftSpecs}>
7272
<EntityEdit
7373
title="routeTitle.materializationEdit"
@@ -102,8 +102,8 @@ function MaterializationEdit() {
102102
}
103103
/>
104104
</MutateDraftSpecProvider>
105-
</BindingHydrator>
106-
</ResourceConfigHydrator>
105+
</ResourceConfigHydrator>
106+
</BindingHydrator>
107107
</EndpointConfigHydrator>
108108
</DetailsFormHydrator>
109109
</DraftInitializer>

src/components/materialization/SourceCapture/AddSourceCaptureToSpecButton.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import invariableStores from 'context/Zustand/invariableStores';
55
import { FormattedMessage } from 'react-intl';
66
import { useResourceConfig_setResourceConfig } from 'stores/ResourceConfig/hooks';
77

8+
import { useBinding_resourceSchema } from 'stores/Binding/hooks';
89
import { useStore } from 'zustand';
910
import useSourceCapture from '../useSourceCapture';
1011

@@ -25,6 +26,7 @@ function AddSourceCaptureToSpecButton({ toggle }: AddCollectionDialogCTAProps) {
2526

2627
const updateDraft = useSourceCapture();
2728

29+
const resourceSchema = useBinding_resourceSchema();
2830
const setResourceConfig = useResourceConfig_setResourceConfig();
2931

3032
const close = async () => {
@@ -41,6 +43,7 @@ function AddSourceCaptureToSpecButton({ toggle }: AddCollectionDialogCTAProps) {
4143
if (selectedRow?.writes_to) {
4244
setResourceConfig(
4345
selectedRow.writes_to,
46+
resourceSchema,
4447
undefined,
4548
false,
4649
true

src/hooks/useServerUpdateRequiredMonitor.ts

+9
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ const useServerUpdateRequiredMonitor = (draftSpecs: DraftSpecQuery[]) => {
5050
}
5151

5252
// Since we checked disabled up above we can not just check if the data changed
53+
console.log(
54+
collectionName,
55+
!isEqual(
56+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
57+
resourceConfig[collectionName]?.data,
58+
resource
59+
)
60+
);
61+
5362
return !isEqual(
5463
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
5564
resourceConfig[collectionName]?.data,

src/stores/Binding/Store.ts

+55-38
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { getDraftSpecsByDraftId } from 'api/draftSpecs';
22
import {
33
getLiveSpecsById_writesTo,
44
getLiveSpecsByLiveSpecId,
5+
getSchema_Resource,
56
} from 'api/hydration';
67
import { GlobalSearchParams } from 'hooks/searchParams/useGlobalSearchParams';
78
import produce from 'immer';
@@ -11,6 +12,7 @@ import {
1112
getStoreWithHydrationSettings,
1213
} from 'stores/extensions/Hydration';
1314
import { BindingStoreNames } from 'stores/names';
15+
import { Schema } from 'types';
1416
import { hasLength } from 'utils/misc-utils';
1517
import { devtoolsOptions } from 'utils/store-utils';
1618
import { getCollectionName } from 'utils/workflow-utils';
@@ -34,41 +36,23 @@ const getInitialBindingData = (): Pick<BindingState, 'bindings'> => ({
3436
bindings: {},
3537
});
3638

37-
const getInitialStateData = () => ({
39+
const getInitialStateData = (): Pick<BindingState, 'resourceSchema'> => ({
40+
resourceSchema: {},
41+
});
42+
43+
const getInitialStoreData = () => ({
3844
...getInitialBindingData(),
45+
...getInitialStateData(),
3946
...getInitialHydrationData(),
4047
});
4148

4249
const getInitialState = (
4350
set: NamedSet<BindingState>,
4451
get: StoreApi<BindingState>['getState']
4552
): BindingState => ({
46-
...getInitialStateData(),
53+
...getInitialStoreData(),
4754
...getStoreWithHydrationSettings(STORE_KEY, set),
4855

49-
prefillBindingDependentState: (bindings, _referenceBindings) => {
50-
set(
51-
produce((state: BindingState) => {
52-
bindings.forEach((binding) => {
53-
const collection = getCollectionName(binding);
54-
const UUID = crypto.randomUUID();
55-
56-
const existingBindingIds: string[] = Object.hasOwn(
57-
state.bindings,
58-
collection
59-
)
60-
? state.bindings[collection]
61-
: [];
62-
63-
state.bindings[collection] =
64-
existingBindingIds.concat(UUID);
65-
});
66-
}),
67-
false,
68-
'Binding dependent state prefilled'
69-
);
70-
},
71-
7256
addEmptyBindings: (data, rehydrating) => {
7357
set(
7458
produce((state: BindingState) => {
@@ -117,7 +101,7 @@ const getInitialState = (
117101

118102
hydrateState: async (editWorkflow, entityType, rehydrating) => {
119103
const searchParams = new URLSearchParams(window.location.search);
120-
// const connectorId = searchParams.get(GlobalSearchParams.CONNECTOR_ID);
104+
const connectorId = searchParams.get(GlobalSearchParams.CONNECTOR_ID);
121105
const draftId = searchParams.get(GlobalSearchParams.DRAFT_ID);
122106
const liveSpecIds = searchParams.getAll(
123107
GlobalSearchParams.LIVE_SPEC_ID
@@ -134,19 +118,19 @@ const getInitialState = (
134118

135119
// resetState(materializationRehydrating);
136120

137-
// if (connectorId) {
138-
// const { data, error } = await getSchema_Resource(connectorId);
121+
if (connectorId) {
122+
const { data, error } = await getSchema_Resource(connectorId);
139123

140-
// if (error) {
141-
// setHydrationErrorsExist(true);
142-
// } else if (data && data.length > 0) {
143-
// const { setResourceSchema } = get();
124+
if (error) {
125+
setHydrationErrorsExist(true);
126+
} else if (data && data.length > 0) {
127+
const { setResourceSchema } = get();
144128

145-
// setResourceSchema(
146-
// data[0].resource_spec_schema as unknown as Schema
147-
// );
148-
// }
149-
// }
129+
setResourceSchema(
130+
data[0].resource_spec_schema as unknown as Schema
131+
);
132+
}
133+
}
150134

151135
if (editWorkflow && liveSpecIds.length > 0) {
152136
const { data: liveSpecs, error: liveSpecError } =
@@ -203,12 +187,35 @@ const getInitialState = (
203187
return Promise.resolve(null);
204188
},
205189

190+
prefillBindingDependentState: (bindings, _referenceBindings) => {
191+
set(
192+
produce((state: BindingState) => {
193+
bindings.forEach((binding) => {
194+
const collection = getCollectionName(binding);
195+
const UUID = crypto.randomUUID();
196+
197+
const existingBindingIds: string[] = Object.hasOwn(
198+
state.bindings,
199+
collection
200+
)
201+
? state.bindings[collection]
202+
: [];
203+
204+
state.bindings[collection] =
205+
existingBindingIds.concat(UUID);
206+
});
207+
}),
208+
false,
209+
'Binding dependent state prefilled'
210+
);
211+
},
212+
206213
resetState: (keepCollections) => {
207214
const currentState = get();
208215

209216
const initState = keepCollections
210217
? getInitialBindingData()
211-
: getInitialStateData();
218+
: getInitialStoreData();
212219

213220
const newState = {
214221
...currentState,
@@ -217,6 +224,16 @@ const getInitialState = (
217224

218225
set(newState, false, 'Binding State Reset');
219226
},
227+
228+
setResourceSchema: (value) => {
229+
set(
230+
produce((state: BindingState) => {
231+
state.resourceSchema = value;
232+
}),
233+
false,
234+
'Resource Schema Set'
235+
);
236+
},
220237
});
221238

222239
export const bindingStore = create<BindingState>()(

src/stores/Binding/hooks.ts

+14
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,17 @@ export const useBinding_resetState = () => {
5050
(state) => state.resetState
5151
);
5252
};
53+
54+
export const useBinding_resourceSchema = () => {
55+
return useZustandStore<BindingState, BindingState['resourceSchema']>(
56+
BindingStoreNames.GENERAL,
57+
(state) => state.resourceSchema
58+
);
59+
};
60+
61+
export const useBinding_setResourceSchema = () => {
62+
return useZustandStore<BindingState, BindingState['setResourceSchema']>(
63+
BindingStoreNames.GENERAL,
64+
(state) => state.setResourceSchema
65+
);
66+
};

src/stores/Binding/types.ts

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ export interface BindingState extends StoreWithHydration {
1818
rehydrating?: boolean
1919
) => void;
2020

21+
// Resource Schema
22+
resourceSchema: Schema;
23+
setResourceSchema: (val: BindingState['resourceSchema']) => void;
24+
25+
// Misc.
2126
hydrateState: (
2227
editWorkflow: boolean,
2328
entityType: Entity,

0 commit comments

Comments
 (0)