-
Notifications
You must be signed in to change notification settings - Fork 4
/
mutations.js
125 lines (98 loc) · 3.26 KB
/
mutations.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
//
// Credit to Dave Stockley (magicspon)
// https://github.com/magicspon
//
import gql from 'graphql-tag';
import { FormDataJson } from 'form-data-json-convert';
import { flatMap, isPlainObject } from 'lodash-es';
export const getFormFieldMeta = (form) => {
const allRows = flatMap(form.pages, 'rows');
const allFields = flatMap(allRows, 'fields');
const fields = flatMap(allFields, ({ handle, inputTypeName }) => {
return { handle, inputTypeName };
});
return fields;
};
function createMutationHandle(form) {
return `save_${form.handle}_Submission`;
}
function createMutationTypes(form) {
const types = getFormFieldMeta(form).map(({ handle, inputTypeName }) => {
return `$${handle}: ${inputTypeName}`;
});
// Add in any captcha tokens generated when we queried the form.
form.captchas.forEach((captcha) => {
types.push(`$${captcha.handle}: FormieCaptchaInput`);
});
return types.join(', ');
}
function createMutationValues(form) {
const values = flatMap(getFormFieldMeta(form), 'handle').map((key) => {
return `${key}: $${key}`;
});
// Add in any captcha tokens generated when we queried the form.
form.captchas.forEach((captcha) => {
values.push(`${captcha.handle}: $${captcha.handle}`);
});
return values.join(', ');
}
export const getFormMutation = (form) => {
const mutationTypes = createMutationTypes(form);
const mutationHandle = createMutationHandle(form);
const mutationValues = createMutationValues(form);
return gql`
mutation FormMutation(${mutationTypes}) {
${mutationHandle}(${mutationValues}) {
id
}
}
`;
};
export const getMutationVariables = (form, el) => {
const object = FormDataJson.formToJson(el);
// Get the mutation types to ensure we cast everything properly
const mutationTypes = getFormFieldMeta(form);
mutationTypes.forEach((info) => {
let value = object[info.handle];
if (typeof value === 'undefined') {
return;
}
// Fix up any objects that look like arrays
if (isPlainObject(value)) {
if (typeof value[0] !== 'undefined') {
value = Object.values(value);
}
}
if (info.inputTypeName === 'Int') {
value = parseInt(object[info.handle], 10);
}
if (info.inputTypeName === '[Int]') {
if (isPlainObject(value)) {
value = Object.values(value);
}
value = value.map((item) => {
return parseInt(item, 10);
});
}
if (info.inputTypeName === 'Number') {
value = Number(object[info.handle]);
}
if (info.inputTypeName === '[Number]') {
if (isPlainObject(value)) {
value = Object.values(value);
}
value = value.map((item) => {
return Number(item);
});
}
object[info.handle] = value;
});
// Add in any captcha tokens generated when we queried the form.
form.captchas.forEach((captcha) => {
object[captcha.handle] = {
name: captcha.name,
value: captcha.value,
};
});
return object;
};