forked from OpenStackweb/summit-admin
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathform-template-actions.js
More file actions
359 lines (315 loc) · 10.9 KB
/
Copy pathform-template-actions.js
File metadata and controls
359 lines (315 loc) · 10.9 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/**
* Copyright 2026 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* */
import T from "i18n-react/dist/i18n-react";
import {
getRequest,
putRequest,
postRequest,
deleteRequest,
createAction,
stopLoading,
startLoading,
showMessage,
showSuccessMessage,
authErrorHandler,
escapeFilterValue
} from "openstack-uicore-foundation/lib/utils/actions";
import history from "../history";
import { getAccessTokenSafely } from "../utils/methods";
import {
DEFAULT_CURRENT_PAGE,
DEFAULT_ORDER_DIR,
DEFAULT_PER_PAGE
} from "../utils/constants";
import {
saveMetaFieldTypes,
deleteMetaFieldType,
saveMetaFieldValues,
deleteMetaFieldTypeValue,
saveFiles,
deleteFile,
archiveItem,
unarchiveItem
} from "./inventory-shared-actions";
export const ADD_FORM_TEMPLATE = "ADD_FORM_TEMPLATE";
export const CHANGE_FORM_TEMPLATE_SEARCH_TERM =
"CHANGE_FORM_TEMPLATE_SEARCH_TERM";
export const FORM_TEMPLATE_ADDED = "FORM_TEMPLATE_ADDED";
export const FORM_TEMPLATE_DELETED = "FORM_TEMPLATE_DELETED";
export const FORM_TEMPLATE_UPDATED = "FORM_TEMPLATE_UPDATED";
export const RECEIVE_FORM_TEMPLATE = "RECEIVE_FORM_TEMPLATE";
export const RECEIVE_FORM_TEMPLATES = "RECEIVE_FORM_TEMPLATES";
export const REQUEST_FORM_TEMPLATES = "REQUEST_FORM_TEMPLATES";
export const RESET_FORM_TEMPLATE_FORM = "RESET_FORM_TEMPLATE_FORM";
export const UPDATE_FORM_TEMPLATE = "UPDATE_FORM_TEMPLATE";
export const ADD_FORM_TEMPLATE_MATERIAL = "ADD_FORM_TEMPLATE_MATERIAL";
export const ADD_FORM_TEMPLATE_META_FIELD_TYPE =
"ADD_FORM_TEMPLATE_META_FIELD_TYPE";
export const FORM_TEMPLATE_META_FIELD_SAVED = "FORM_TEMPLATE_META_FIELD_SAVED";
export const FORM_TEMPLATE_META_FIELD_DELETED =
"FORM_TEMPLATE_META_FIELD_DELETED";
export const FORM_TEMPLATE_META_FIELD_VALUE_SAVED =
"FORM_TEMPLATE_META_FIELD_VALUE_SAVED";
export const FORM_TEMPLATE_META_FIELD_VALUE_DELETED =
"FORM_TEMPLATE_META_FIELD_VALUE_DELETED";
export const FORM_TEMPLATE_MATERIAL_SAVED = "FORM_TEMPLATE_MATERIAL_SAVED";
export const FORM_TEMPLATE_MATERIAL_DELETED = "FORM_TEMPLATE_MATERIAL_DELETED";
export const FORM_TEMPLATE_ARCHIVED = "FORM_TEMPLATE_ARCHIVED";
export const FORM_TEMPLATE_UNARCHIVED = "FORM_TEMPLATE_UNARCHIVED";
export const getFormTemplates =
(
term = null,
page = DEFAULT_CURRENT_PAGE,
perPage = DEFAULT_PER_PAGE,
order = "id",
orderDir = DEFAULT_ORDER_DIR,
hideArchived = false
) =>
async (dispatch) => {
const accessToken = await getAccessTokenSafely();
const filter = [];
dispatch(startLoading());
if (term) {
const escapedTerm = escapeFilterValue(term);
filter.push(`name=@${escapedTerm},code=@${escapedTerm}`);
}
const params = {
page,
fields: "id,code,name,level,expire_date,is_archived",
relations: "items",
per_page: perPage,
access_token: accessToken
};
if (hideArchived) filter.push("is_archived==0");
if (filter.length > 0) {
params["filter[]"] = filter;
}
// order
if (order != null && orderDir != null) {
const orderDirSign = orderDir === 1 ? "" : "-";
params.order = `${orderDirSign}${order}`;
}
return getRequest(
createAction(REQUEST_FORM_TEMPLATES),
createAction(RECEIVE_FORM_TEMPLATES),
`${window.INVENTORY_API_BASE_URL}/api/v1/form-templates`,
authErrorHandler,
{ order, orderDir, page, perPage, term, hideArchived }
)(params)(dispatch).then(() => {
dispatch(stopLoading());
});
};
export const getFormTemplate = (formTemplateId) => async (dispatch) => {
const accessToken = await getAccessTokenSafely();
dispatch(startLoading());
const params = {
access_token: accessToken,
expand: "materials,meta_fields,meta_fields.values"
};
return getRequest(
null,
createAction(RECEIVE_FORM_TEMPLATE),
`${window.INVENTORY_API_BASE_URL}/api/v1/form-templates/${formTemplateId}`,
authErrorHandler
)(params)(dispatch).then(() => {
dispatch(stopLoading());
});
};
export const deleteFormTemplate = (formTemplateId) => async (dispatch) => {
const accessToken = await getAccessTokenSafely();
dispatch(startLoading());
const params = {
access_token: accessToken
};
return deleteRequest(
null,
createAction(FORM_TEMPLATE_DELETED)({ formTemplateId }),
`${window.INVENTORY_API_BASE_URL}/api/v1/form-templates/${formTemplateId}`,
null,
authErrorHandler
)(params)(dispatch).then(() => {
dispatch(stopLoading());
});
};
export const resetFormTemplateForm = () => (dispatch) => {
dispatch(createAction(RESET_FORM_TEMPLATE_FORM)({}));
};
const normalizeEntity = (entity) => {
const normalizedEntity = { ...entity };
normalizedEntity.meta_fields = normalizedEntity.meta_fields?.filter(
(mf) => mf.name
);
normalizedEntity.materials = normalizedEntity.materials?.filter(
(mat) => mat.file_path
);
return normalizedEntity;
};
export const saveFormTemplate = (entity) => async (dispatch) => {
const accessToken = await getAccessTokenSafely();
const params = {
access_token: accessToken,
expand: "materials,meta_fields,meta_fields.values"
};
dispatch(startLoading());
const normalizedEntity = normalizeEntity(entity);
if (entity.id) {
return putRequest(
createAction(UPDATE_FORM_TEMPLATE),
createAction(FORM_TEMPLATE_UPDATED),
`${window.INVENTORY_API_BASE_URL}/api/v1/form-templates/${entity.id}`,
normalizedEntity,
authErrorHandler,
entity
)(params)(dispatch)
.then(() => {
const promises = [];
if (entity.materials.length > 0) {
promises.push(saveFormTemplateMaterials(normalizedEntity)(dispatch));
}
if (entity.meta_fields.length > 0) {
promises.push(
saveFormTemplateMetaFieldTypes(normalizedEntity)(dispatch)
);
}
Promise.all(promises)
.then(() => {
dispatch(
showSuccessMessage(
T.translate("edit_form_template.form_template_saved")
)
);
})
.catch((err) => {
console.error(err);
})
.finally(() => {
dispatch(stopLoading());
});
})
.catch((err) => {
console.error(err);
});
}
const success_message = {
title: T.translate("general.done"),
html: T.translate("edit_form_template.form_template_created"),
type: "success"
};
return postRequest(
createAction(ADD_FORM_TEMPLATE),
createAction(FORM_TEMPLATE_ADDED),
`${window.INVENTORY_API_BASE_URL}/api/v1/form-templates`,
normalizedEntity,
authErrorHandler,
entity
)(params)(dispatch)
.then(({ response }) => {
const formTemplate = { ...normalizedEntity, id: response.id };
const promises = [];
if (entity.materials.length > 0) {
promises.push(saveFormTemplateMaterials(formTemplate)(dispatch));
}
if (entity.meta_fields.length > 0) {
promises.push(saveFormTemplateMetaFieldTypes(formTemplate)(dispatch));
}
Promise.all(promises)
.then(() => {
dispatch(
showMessage(success_message, () => {
history.push("/app/form-templates");
})
);
})
.catch((err) => {
console.error(err);
})
.finally(() => {
dispatch(stopLoading());
});
})
.catch((err) => {
console.error(err);
});
};
/* ************************************ META FIELD TYPES ************************************ */
const saveFormTemplateMetaFieldTypes = (formTemplate) => {
const settings = {
url: `${window.INVENTORY_API_BASE_URL}/api/v1/form-templates/${formTemplate.id}/meta-field-types`,
addedActionName: FORM_TEMPLATE_META_FIELD_SAVED,
updatedActionName: FORM_TEMPLATE_META_FIELD_SAVED
};
return saveMetaFieldTypes(
formTemplate,
settings,
(formTemplateId, metaFieldType) =>
saveFormTemplateMetaFieldTypeValues(formTemplateId, metaFieldType)
);
};
export const deleteFormTemplateMetaFieldType = (templateId, metaFieldId) => {
const settings = {
url: `${window.INVENTORY_API_BASE_URL}/api/v1/form-templates/${templateId}/meta-field-types`,
deletedActionName: FORM_TEMPLATE_META_FIELD_DELETED
};
return deleteMetaFieldType(metaFieldId, settings);
};
/* ************************************ META FIELD VALUES ************************************ */
const saveFormTemplateMetaFieldTypeValues = (templateId, metaFieldType) => {
const settings = {
url: `${window.INVENTORY_API_BASE_URL}/api/v1/form-templates/${templateId}/meta-field-types/${metaFieldType.id}/values`,
addedActionName: FORM_TEMPLATE_META_FIELD_VALUE_SAVED,
updatedActionName: FORM_TEMPLATE_META_FIELD_VALUE_SAVED
};
return saveMetaFieldValues(metaFieldType, settings);
};
export const deleteFormTemplateMetaFieldTypeValue = (
templateId,
metaFieldId,
valueId
) => {
const settings = {
url: `${window.INVENTORY_API_BASE_URL}/api/v1/form-templates/${templateId}/meta-field-types/${metaFieldId}/values`,
deletedActionName: FORM_TEMPLATE_META_FIELD_VALUE_DELETED
};
return deleteMetaFieldTypeValue(metaFieldId, valueId, settings);
};
/* ************************************ MATERIALS ************************************ */
const saveFormTemplateMaterials = (template) => {
const settings = {
url: `${window.INVENTORY_API_BASE_URL}/api/v1/form-templates/${template.id}/materials`,
addedActionName: FORM_TEMPLATE_MATERIAL_SAVED,
updatedActionName: FORM_TEMPLATE_MATERIAL_SAVED
};
return saveFiles(template.materials, settings);
};
export const deleteFormTemplateMaterial = (templateId, materialId) => {
const settings = {
url: `${window.INVENTORY_API_BASE_URL}/api/v1/form-templates/${templateId}/materials`,
deletedActionName: FORM_TEMPLATE_MATERIAL_DELETED
};
return deleteFile(materialId, settings);
};
/* ************************************** ARCHIVE ************************************** */
export const archiveFormTemplate = (formTemplate) => {
const settings = {
url: `${window.INVENTORY_API_BASE_URL}/api/v1/form-templates/${formTemplate.id}/archive`,
updatedActionName: FORM_TEMPLATE_ARCHIVED
};
return archiveItem(formTemplate, settings);
};
export const unarchiveFormTemplate = (formTemplate) => {
const settings = {
url: `${window.INVENTORY_API_BASE_URL}/api/v1/form-templates/${formTemplate.id}/archive`,
deletedActionName: FORM_TEMPLATE_UNARCHIVED
};
return unarchiveItem(formTemplate, settings);
};