-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuseUpdateBackfillCounter.ts
134 lines (112 loc) · 4.55 KB
/
useUpdateBackfillCounter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { modifyDraftSpec } from 'api/draftSpecs';
import { BooleanString } from 'components/editor/Bindings/Backfill';
import { useEntityType } from 'context/EntityContext';
import { DraftSpecQuery } from 'hooks/useDraftSpecs';
import { useCallback } from 'react';
import { useIntl } from 'react-intl';
import { BASE_ERROR } from 'services/supabase';
import { useResourceConfig_backfilledCollections } from 'stores/ResourceConfig/hooks';
import { Schema } from 'types';
import { hasLength } from 'utils/misc-utils';
import { getBackfillCounter, getCollectionName } from 'utils/workflow-utils';
import {
useEditorStore_persistedDraftId,
useEditorStore_queryResponse_mutate,
} from '../../Store/hooks';
export interface BindingMetadata {
bindingIndex: number;
collection: string;
}
const evaluateBackfillCounter = (
binding: Schema,
increment: BooleanString
): number => {
let counter = getBackfillCounter(binding);
if (increment === 'true') {
counter = counter + 1;
} else if (counter > 0) {
counter = counter - 1;
}
return counter;
};
function useUpdateBackfillCounter() {
const intl = useIntl();
const entityType = useEntityType();
// Draft Editor Store
const draftId = useEditorStore_persistedDraftId();
const mutateDraftSpecs = useEditorStore_queryResponse_mutate();
// Resource Config Store
const backfilledCollections = useResourceConfig_backfilledCollections();
const updateBackfillCounter = useCallback(
async (
draftSpec: DraftSpecQuery,
increment: BooleanString,
bindingMetadata: BindingMetadata[]
) => {
const bindingMetadataExists = hasLength(bindingMetadata);
const invalidBindingIndex = bindingMetadataExists
? bindingMetadata.findIndex(
({ bindingIndex }) => bindingIndex === -1
)
: -1;
if (!mutateDraftSpecs || invalidBindingIndex > -1) {
const errorMessageId = bindingMetadataExists
? 'workflows.collectionSelector.manualBackfill.error.message.singleCollection'
: 'workflows.collectionSelector.manualBackfill.error.message.allBindings';
const errorMessageValues = bindingMetadataExists
? {
collection:
bindingMetadata[invalidBindingIndex].collection,
}
: undefined;
return Promise.reject({
...BASE_ERROR,
message: intl.formatMessage(
{ id: errorMessageId },
errorMessageValues
),
});
}
const spec: Schema = draftSpec.spec;
if (bindingMetadataExists) {
bindingMetadata.forEach(({ bindingIndex }) => {
if (bindingIndex > -1) {
spec.bindings[bindingIndex].backfill =
evaluateBackfillCounter(
spec.bindings[bindingIndex],
increment
);
}
});
} else {
spec.bindings.forEach((binding: Schema, index: number) => {
const collection = getCollectionName(binding);
const collectionBackfilled =
backfilledCollections.includes(collection);
const shouldIncrement =
!collectionBackfilled && increment === 'true';
const shouldDecrement =
collectionBackfilled && increment === 'false';
if (shouldIncrement || shouldDecrement) {
spec.bindings[index].backfill = evaluateBackfillCounter(
binding,
increment
);
}
});
}
const updateResponse = await modifyDraftSpec(spec, {
draft_id: draftId,
catalog_name: draftSpec.catalog_name,
spec_type: entityType,
});
if (updateResponse.error) {
return Promise.reject(updateResponse.error);
}
return mutateDraftSpecs();
},
[backfilledCollections, draftId, entityType, intl, mutateDraftSpecs]
);
return { updateBackfillCounter };
}
export default useUpdateBackfillCounter;