/
api.js
170 lines (143 loc) · 5.58 KB
/
api.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import * as client from './client';
import { handleLegacyConfig } from './legacyMode';
import useNormalizeConfig, { useNormalizeConfigs } from './useNormalizeConfig';
import { setDefaultType } from './utils';
export * from './client';
export const useForm = (formId, options) => {
return client.useForm({ formId }) |> useNormalizeConfig(options);
};
export const useAtomicForm = (formId, formRev, layer, options) => {
const normalize = useNormalizeConfig(options);
if (formRev) {
return normalize(client.useFormRev({ formId, formRev, layer }));
} else {
return normalize(client.useAtomicForm({ formId, layer }));
}
};
export const useFormRev = (formId, formRev) => {
return client.useFormRev({formId, formRev});
};
export const useSubmittedFormData = (formId, formRev, credentials, dataId, dataIsDraft, layer) => {
if (formRev) {
return client.useFormAndFormDataByRevision({formId, formRev, dataId, credentials, dataIsDraft, layer});
} else {
return client.useFormDataFromSubmissionHandler({formId, dataId, credentials, dataIsDraft, layer});
}
};
export const useFormSafe = (formId, options) => {
const normalize = useNormalizeConfig(options);
const [error, data] = client.useFormSafe({ formId });
return [error, data ? normalize(data) : data];
};
export const useAtomicFormSafe = (formId, options) => {
const normalize = useNormalizeConfig(options);
const [error, data] = client.useAtomicFormSafe({ formId });
return [error, data ? normalize(data) : data];
};
export const useForms = ({ status, visibility } = {}, options) =>
({
'': client.useForms,
draft: client.useDraftForms,
published: client.usePublishedForms,
'published/public': client.usePublishedPublicForms,
}[[status, visibility].filter(Boolean).join('/')]()
|> useNormalizeConfigs(options));
export const useFormGroup = (formGroupId, options) =>
client.useFormGroup({ formGroupId })
|> useNormalizeConfig(options |> setDefaultType('formGroup'));
export const useFormGroups = (options) =>
client.useFormGroups()
|> useNormalizeConfigs(options |> setDefaultType('formGroup'));
export const useField = (fieldId, options) =>
client.useField({ fieldId }) |> useNormalizeConfig(options);
export const useFields = (options) =>
client.useFields() |> useNormalizeConfigs(options);
export const useFormData = (dataId) => client.useFormData({ dataId });
export const useTags = () => client.useFormTags();
export const useFieldUsageCounts = () => client.useFieldUsageCount();
export const useFormgroupUsageCounts = () => client.useFormgroupUsageCount();
export const useFormHandlers = () => client.useFormHandlers();
export const useStandardizedForm = (formId) => client.getFormStandardized({formId});
export function useMutations() {
// Trigger refetching paths on update. Only the paths that are currently
// loaded by the app, will be refetched.
const refetch = (response) => Promise.all(response);
const refetchForms = (id) =>
refetch([
client.refetchForm({ formId: String(id) }),
client.refetchForms(),
client.refetchDraftForms(),
client.refetchPublishedForms(),
client.refetchPublishedPublicForms(),
client.refetchFormTags(),
]);
const refetchFormGroups = (id) =>
refetch([
client.refetchFormGroup({ formGroupId: String(id) }),
client.refetchFormGroups(),
]);
const refetchFields = (id) =>
refetch([
client.refetchField({ fieldId: String(id) }),
client.refetchFields(),
]);
return {
async submit(data, credentials, formDataAction, isDraft, layer) {
if (data._id && ((isDraft && data.status === "DRAFT") || (!isDraft && data.status === "SUBMITTED"))) {
return client.postResubmitFormData(
{dataId: data._id, credentials: credentials, actionId: formDataAction, isDraft: isDraft, layer: layer},
data
);
} else {
if (isDraft && data.status === "SUBMITTED") {
return client.postFormData({ actionId: formDataAction, credentials: credentials, draftReference: data._id, layer: layer }, data);
} else {
return client.postFormData({ actionId: formDataAction, credentials: credentials, layer: layer }, data);
}
}
},
async postForm(data) {
await client.postForm(null, handleLegacyConfig(data));
await refetchForms(data._id);
},
async putForm(data) {
await client.putForm(null, handleLegacyConfig(data));
await refetchForms(data._id);
},
async deleteForm(formId) {
await client.deleteForm({ formId });
await refetchForms(formId);
},
async postFormGroup(data) {
await client.postFormGroup(null, handleLegacyConfig(data));
await refetchFormGroups(data._id);
},
async putFormGroup(data) {
await client.putFormGroup(null, handleLegacyConfig(data));
await refetchFormGroups(data._id);
},
async deleteFormGroup(formGroupId) {
await client.deleteFormGroup({ formGroupId });
await refetchFormGroups(formGroupId);
},
async postField(data) {
const response = await client.postField(null, handleLegacyConfig(data));
await refetchFields(data._id);
return response;
},
async putField(data) {
await client.putField(null, handleLegacyConfig(data));
await refetchFields(data._id);
},
async deleteField(fieldId) {
await client.deleteField({ fieldId });
await refetchFields(fieldId);
},
async importData(data, usePortkeys) {
await client.postConfigs({usePortkeys: usePortkeys}, data);
},
async updateConfig(data) {
await client.postConfig(null, data);
},
};
}