@payloadcms/plugin-form-builder 1.2.0 → 3.0.0-alpha.50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/collections/FormSubmissions/hooks/createCharge.js +3 -13
- package/dist/collections/FormSubmissions/hooks/createCharge.js.map +1 -0
- package/dist/collections/FormSubmissions/hooks/sendEmail.js +15 -25
- package/dist/collections/FormSubmissions/hooks/sendEmail.js.map +1 -0
- package/dist/collections/FormSubmissions/index.js +36 -49
- package/dist/collections/FormSubmissions/index.js.map +1 -0
- package/dist/collections/Forms/DynamicFieldSelector.js +11 -58
- package/dist/collections/Forms/DynamicFieldSelector.js.map +1 -0
- package/dist/collections/Forms/DynamicPriceSelector.js +13 -61
- package/dist/collections/Forms/DynamicPriceSelector.js.map +1 -0
- package/dist/collections/Forms/fields.js +102 -120
- package/dist/collections/Forms/fields.js.map +1 -0
- package/dist/collections/Forms/index.js +47 -61
- package/dist/collections/Forms/index.js.map +1 -0
- package/dist/index.js +8 -29
- package/dist/index.js.map +1 -0
- package/dist/types.js +2 -12
- package/dist/types.js.map +1 -0
- package/dist/utilities/getPaymentTotal.js +2 -12
- package/dist/utilities/getPaymentTotal.js.map +1 -0
- package/dist/utilities/lexical/converters/heading.js +4 -14
- package/dist/utilities/lexical/converters/heading.js.map +1 -0
- package/dist/utilities/lexical/converters/linebreak.js +2 -12
- package/dist/utilities/lexical/converters/linebreak.js.map +1 -0
- package/dist/utilities/lexical/converters/link.js +8 -17
- package/dist/utilities/lexical/converters/link.js.map +1 -0
- package/dist/utilities/lexical/converters/list.js +7 -25
- package/dist/utilities/lexical/converters/list.js.map +1 -0
- package/dist/utilities/lexical/converters/paragraph.js +4 -14
- package/dist/utilities/lexical/converters/paragraph.js.map +1 -0
- package/dist/utilities/lexical/converters/quote.js +4 -14
- package/dist/utilities/lexical/converters/quote.js.map +1 -0
- package/dist/utilities/lexical/converters/text.js +12 -22
- package/dist/utilities/lexical/converters/text.js.map +1 -0
- package/dist/utilities/lexical/defaultConverters.js +17 -27
- package/dist/utilities/lexical/defaultConverters.js.map +1 -0
- package/dist/utilities/lexical/nodeFormat.js +16 -61
- package/dist/utilities/lexical/nodeFormat.js.map +1 -0
- package/dist/utilities/lexical/serializeLexical.js +5 -23
- package/dist/utilities/lexical/serializeLexical.js.map +1 -0
- package/dist/utilities/lexical/types.js +2 -5
- package/dist/utilities/lexical/types.js.map +1 -0
- package/dist/utilities/replaceDoubleCurlys.js +2 -12
- package/dist/utilities/replaceDoubleCurlys.js.map +1 -0
- package/dist/utilities/slate/serializeSlate.js +6 -21
- package/dist/utilities/slate/serializeSlate.js.map +1 -0
- package/package.json +24 -11
- package/src/index.ts +55 -0
- package/dist/collections/FormSubmissions/hooks/createCharge.d.ts +0 -4
- package/dist/collections/FormSubmissions/hooks/createCharge.d.ts.map +0 -1
- package/dist/collections/FormSubmissions/hooks/sendEmail.d.ts +0 -4
- package/dist/collections/FormSubmissions/hooks/sendEmail.d.ts.map +0 -1
- package/dist/collections/FormSubmissions/index.d.ts +0 -4
- package/dist/collections/FormSubmissions/index.d.ts.map +0 -1
- package/dist/collections/Forms/DynamicFieldSelector.d.ts +0 -4
- package/dist/collections/Forms/DynamicFieldSelector.d.ts.map +0 -1
- package/dist/collections/Forms/DynamicPriceSelector.d.ts +0 -4
- package/dist/collections/Forms/DynamicPriceSelector.d.ts.map +0 -1
- package/dist/collections/Forms/fields.d.ts +0 -7
- package/dist/collections/Forms/fields.d.ts.map +0 -1
- package/dist/collections/Forms/index.d.ts +0 -4
- package/dist/collections/Forms/index.d.ts.map +0 -1
- package/dist/index.d.ts +0 -7
- package/dist/index.d.ts.map +0 -1
- package/dist/types.d.ts +0 -175
- package/dist/types.d.ts.map +0 -1
- package/dist/utilities/getPaymentTotal.d.ts +0 -5
- package/dist/utilities/getPaymentTotal.d.ts.map +0 -1
- package/dist/utilities/lexical/converters/heading.d.ts +0 -3
- package/dist/utilities/lexical/converters/heading.d.ts.map +0 -1
- package/dist/utilities/lexical/converters/linebreak.d.ts +0 -3
- package/dist/utilities/lexical/converters/linebreak.d.ts.map +0 -1
- package/dist/utilities/lexical/converters/link.d.ts +0 -3
- package/dist/utilities/lexical/converters/link.d.ts.map +0 -1
- package/dist/utilities/lexical/converters/list.d.ts +0 -4
- package/dist/utilities/lexical/converters/list.d.ts.map +0 -1
- package/dist/utilities/lexical/converters/paragraph.d.ts +0 -3
- package/dist/utilities/lexical/converters/paragraph.d.ts.map +0 -1
- package/dist/utilities/lexical/converters/quote.d.ts +0 -3
- package/dist/utilities/lexical/converters/quote.d.ts.map +0 -1
- package/dist/utilities/lexical/converters/text.d.ts +0 -3
- package/dist/utilities/lexical/converters/text.d.ts.map +0 -1
- package/dist/utilities/lexical/defaultConverters.d.ts +0 -3
- package/dist/utilities/lexical/defaultConverters.d.ts.map +0 -1
- package/dist/utilities/lexical/nodeFormat.d.ts +0 -39
- package/dist/utilities/lexical/nodeFormat.d.ts.map +0 -1
- package/dist/utilities/lexical/serializeLexical.d.ts +0 -9
- package/dist/utilities/lexical/serializeLexical.d.ts.map +0 -1
- package/dist/utilities/lexical/types.d.ts +0 -14
- package/dist/utilities/lexical/types.d.ts.map +0 -1
- package/dist/utilities/replaceDoubleCurlys.d.ts +0 -8
- package/dist/utilities/replaceDoubleCurlys.d.ts.map +0 -1
- package/dist/utilities/slate/serializeSlate.d.ts +0 -12
- package/dist/utilities/slate/serializeSlate.d.ts.map +0 -1
|
@@ -1,24 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(exports, "default", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: function() {
|
|
8
|
-
return _default;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
const createCharge = async (beforeChangeData, formConfig)=>{
|
|
1
|
+
export const createCharge = async (beforeChangeData, formConfig)=>{
|
|
12
2
|
const { data, operation } = beforeChangeData;
|
|
13
3
|
let dataWithPaymentDetails = data;
|
|
14
4
|
if (operation === 'create') {
|
|
15
5
|
const { handlePayment } = formConfig || {};
|
|
16
6
|
if (typeof handlePayment === 'function') {
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/await-thenable
|
|
17
8
|
dataWithPaymentDetails = await handlePayment(beforeChangeData);
|
|
18
9
|
}
|
|
19
10
|
}
|
|
20
11
|
return dataWithPaymentDetails;
|
|
21
12
|
};
|
|
22
|
-
const _default = createCharge;
|
|
23
13
|
|
|
24
|
-
//# sourceMappingURL=
|
|
14
|
+
//# sourceMappingURL=createCharge.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/collections/FormSubmissions/hooks/createCharge.ts"],"sourcesContent":["import type { PluginConfig } from '../../../types.js'\n\nexport const createCharge = async (\n beforeChangeData: any,\n formConfig: PluginConfig,\n): Promise<any> => {\n const { data, operation } = beforeChangeData\n\n let dataWithPaymentDetails = data\n\n if (operation === 'create') {\n const { handlePayment } = formConfig || {}\n\n if (typeof handlePayment === 'function') {\n // eslint-disable-next-line @typescript-eslint/await-thenable\n dataWithPaymentDetails = await handlePayment(beforeChangeData)\n }\n }\n\n return dataWithPaymentDetails\n}\n"],"names":["createCharge","beforeChangeData","formConfig","data","operation","dataWithPaymentDetails","handlePayment"],"mappings":"AAEA,OAAO,MAAMA,eAAe,OAC1BC,kBACAC;IAEA,MAAM,EAAEC,IAAI,EAAEC,SAAS,EAAE,GAAGH;IAE5B,IAAII,yBAAyBF;IAE7B,IAAIC,cAAc,UAAU;QAC1B,MAAM,EAAEE,aAAa,EAAE,GAAGJ,cAAc,CAAC;QAEzC,IAAI,OAAOI,kBAAkB,YAAY;YACvC,6DAA6D;YAC7DD,yBAAyB,MAAMC,cAAcL;QAC/C;IACF;IAEA,OAAOI;AACT,EAAC"}
|
|
@@ -1,18 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: function() {
|
|
8
|
-
return _default;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
const _serializeLexical = require("../../../utilities/lexical/serializeLexical");
|
|
12
|
-
const _replaceDoubleCurlys = require("../../../utilities/replaceDoubleCurlys");
|
|
13
|
-
const _serializeSlate = require("../../../utilities/slate/serializeSlate");
|
|
14
|
-
const sendEmail = async (beforeChangeData, formConfig)=>{
|
|
15
|
-
const { data, operation } = beforeChangeData;
|
|
1
|
+
import { serializeLexical } from '../../../utilities/lexical/serializeLexical.js';
|
|
2
|
+
import { replaceDoubleCurlys } from '../../../utilities/replaceDoubleCurlys.js';
|
|
3
|
+
import { serializeSlate } from '../../../utilities/slate/serializeSlate.js';
|
|
4
|
+
export const sendEmail = async (beforeChangeData, formConfig)=>{
|
|
5
|
+
const { data, operation, req } = beforeChangeData;
|
|
16
6
|
if (operation === 'create') {
|
|
17
7
|
const { data: { id: formSubmissionID }, req: { locale, payload } } = beforeChangeData;
|
|
18
8
|
const { form: formID, submissionData } = data || {};
|
|
@@ -21,26 +11,27 @@ const sendEmail = async (beforeChangeData, formConfig)=>{
|
|
|
21
11
|
const form = await payload.findByID({
|
|
22
12
|
id: formID,
|
|
23
13
|
collection: formOverrides?.slug || 'forms',
|
|
24
|
-
locale
|
|
14
|
+
locale,
|
|
15
|
+
req
|
|
25
16
|
});
|
|
26
17
|
const { emails } = form;
|
|
27
18
|
if (emails && emails.length) {
|
|
28
19
|
const formattedEmails = await Promise.all(emails.map(async (email)=>{
|
|
29
20
|
const { bcc: emailBCC, cc: emailCC, emailFrom, emailTo, message, replyTo: emailReplyTo, subject } = email;
|
|
30
|
-
const to =
|
|
31
|
-
const cc = emailCC ?
|
|
32
|
-
const bcc = emailBCC ?
|
|
33
|
-
const from =
|
|
34
|
-
const replyTo =
|
|
21
|
+
const to = replaceDoubleCurlys(emailTo, submissionData);
|
|
22
|
+
const cc = emailCC ? replaceDoubleCurlys(emailCC, submissionData) : '';
|
|
23
|
+
const bcc = emailBCC ? replaceDoubleCurlys(emailBCC, submissionData) : '';
|
|
24
|
+
const from = replaceDoubleCurlys(emailFrom, submissionData);
|
|
25
|
+
const replyTo = replaceDoubleCurlys(emailReplyTo || emailFrom, submissionData);
|
|
35
26
|
const isLexical = message && !Array.isArray(message) && 'root' in message;
|
|
36
|
-
const serializedMessage = isLexical ? await
|
|
27
|
+
const serializedMessage = isLexical ? await serializeLexical(message, submissionData) : serializeSlate(message, submissionData);
|
|
37
28
|
return {
|
|
38
29
|
bcc,
|
|
39
30
|
cc,
|
|
40
31
|
from,
|
|
41
32
|
html: `<div>${serializedMessage}</div>`,
|
|
42
33
|
replyTo,
|
|
43
|
-
subject:
|
|
34
|
+
subject: replaceDoubleCurlys(subject, submissionData),
|
|
44
35
|
to
|
|
45
36
|
};
|
|
46
37
|
}));
|
|
@@ -74,6 +65,5 @@ const sendEmail = async (beforeChangeData, formConfig)=>{
|
|
|
74
65
|
}
|
|
75
66
|
return data;
|
|
76
67
|
};
|
|
77
|
-
const _default = sendEmail;
|
|
78
68
|
|
|
79
|
-
//# sourceMappingURL=
|
|
69
|
+
//# sourceMappingURL=sendEmail.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/collections/FormSubmissions/hooks/sendEmail.ts"],"sourcesContent":["import type { Email, FormattedEmail, PluginConfig } from '../../../types.js'\n\nimport { serializeLexical } from '../../../utilities/lexical/serializeLexical.js'\nimport { replaceDoubleCurlys } from '../../../utilities/replaceDoubleCurlys.js'\nimport { serializeSlate } from '../../../utilities/slate/serializeSlate.js'\n\nexport const sendEmail = async (beforeChangeData: any, formConfig: PluginConfig): Promise<any> => {\n const { data, operation, req } = beforeChangeData\n\n if (operation === 'create') {\n const {\n data: { id: formSubmissionID },\n req: { locale, payload },\n } = beforeChangeData\n\n const { form: formID, submissionData } = data || {}\n\n const { beforeEmail, formOverrides } = formConfig || {}\n\n try {\n const form = await payload.findByID({\n id: formID,\n collection: formOverrides?.slug || 'forms',\n locale,\n req,\n })\n\n const { emails } = form\n\n if (emails && emails.length) {\n const formattedEmails: FormattedEmail[] = await Promise.all(\n emails.map(async (email: Email): Promise<FormattedEmail | null> => {\n const {\n bcc: emailBCC,\n cc: emailCC,\n emailFrom,\n emailTo,\n message,\n replyTo: emailReplyTo,\n subject,\n } = email\n\n const to = replaceDoubleCurlys(emailTo, submissionData)\n const cc = emailCC ? replaceDoubleCurlys(emailCC, submissionData) : ''\n const bcc = emailBCC ? replaceDoubleCurlys(emailBCC, submissionData) : ''\n const from = replaceDoubleCurlys(emailFrom, submissionData)\n const replyTo = replaceDoubleCurlys(emailReplyTo || emailFrom, submissionData)\n\n const isLexical = message && !Array.isArray(message) && 'root' in message\n\n const serializedMessage = isLexical\n ? await serializeLexical(message, submissionData)\n : serializeSlate(message, submissionData)\n\n return {\n bcc,\n cc,\n from,\n html: `<div>${serializedMessage}</div>`,\n replyTo,\n subject: replaceDoubleCurlys(subject, submissionData),\n to,\n }\n }),\n )\n\n let emailsToSend = formattedEmails\n\n if (typeof beforeEmail === 'function') {\n emailsToSend = await beforeEmail(formattedEmails)\n }\n\n // const log = emailsToSend.map(({ html, ...rest }) => ({ ...rest }))\n\n await Promise.all(\n emailsToSend.map(async (email) => {\n const { to } = email\n try {\n const emailPromise = await payload.sendEmail(email)\n return emailPromise\n } catch (err: unknown) {\n payload.logger.error({\n err: `Error while sending email to address: ${to}. Email not sent: ${JSON.stringify(\n err,\n )}`,\n })\n }\n }),\n )\n } else {\n payload.logger.info({ msg: 'No emails to send.' })\n }\n } catch (err: unknown) {\n const msg = `Error while sending one or more emails in form submission id: ${formSubmissionID}.`\n payload.logger.error({ err: msg })\n }\n }\n\n return data\n}\n"],"names":["serializeLexical","replaceDoubleCurlys","serializeSlate","sendEmail","beforeChangeData","formConfig","data","operation","req","id","formSubmissionID","locale","payload","form","formID","submissionData","beforeEmail","formOverrides","findByID","collection","slug","emails","length","formattedEmails","Promise","all","map","email","bcc","emailBCC","cc","emailCC","emailFrom","emailTo","message","replyTo","emailReplyTo","subject","to","from","isLexical","Array","isArray","serializedMessage","html","emailsToSend","emailPromise","err","logger","error","JSON","stringify","info","msg"],"mappings":"AAEA,SAASA,gBAAgB,QAAQ,iDAAgD;AACjF,SAASC,mBAAmB,QAAQ,4CAA2C;AAC/E,SAASC,cAAc,QAAQ,6CAA4C;AAE3E,OAAO,MAAMC,YAAY,OAAOC,kBAAuBC;IACrD,MAAM,EAAEC,IAAI,EAAEC,SAAS,EAAEC,GAAG,EAAE,GAAGJ;IAEjC,IAAIG,cAAc,UAAU;QAC1B,MAAM,EACJD,MAAM,EAAEG,IAAIC,gBAAgB,EAAE,EAC9BF,KAAK,EAAEG,MAAM,EAAEC,OAAO,EAAE,EACzB,GAAGR;QAEJ,MAAM,EAAES,MAAMC,MAAM,EAAEC,cAAc,EAAE,GAAGT,QAAQ,CAAC;QAElD,MAAM,EAAEU,WAAW,EAAEC,aAAa,EAAE,GAAGZ,cAAc,CAAC;QAEtD,IAAI;YACF,MAAMQ,OAAO,MAAMD,QAAQM,QAAQ,CAAC;gBAClCT,IAAIK;gBACJK,YAAYF,eAAeG,QAAQ;gBACnCT;gBACAH;YACF;YAEA,MAAM,EAAEa,MAAM,EAAE,GAAGR;YAEnB,IAAIQ,UAAUA,OAAOC,MAAM,EAAE;gBAC3B,MAAMC,kBAAoC,MAAMC,QAAQC,GAAG,CACzDJ,OAAOK,GAAG,CAAC,OAAOC;oBAChB,MAAM,EACJC,KAAKC,QAAQ,EACbC,IAAIC,OAAO,EACXC,SAAS,EACTC,OAAO,EACPC,OAAO,EACPC,SAASC,YAAY,EACrBC,OAAO,EACR,GAAGV;oBAEJ,MAAMW,KAAKrC,oBAAoBgC,SAASlB;oBACxC,MAAMe,KAAKC,UAAU9B,oBAAoB8B,SAAShB,kBAAkB;oBACpE,MAAMa,MAAMC,WAAW5B,oBAAoB4B,UAAUd,kBAAkB;oBACvE,MAAMwB,OAAOtC,oBAAoB+B,WAAWjB;oBAC5C,MAAMoB,UAAUlC,oBAAoBmC,gBAAgBJ,WAAWjB;oBAE/D,MAAMyB,YAAYN,WAAW,CAACO,MAAMC,OAAO,CAACR,YAAY,UAAUA;oBAElE,MAAMS,oBAAoBH,YACtB,MAAMxC,iBAAiBkC,SAASnB,kBAChCb,eAAegC,SAASnB;oBAE5B,OAAO;wBACLa;wBACAE;wBACAS;wBACAK,MAAM,CAAC,KAAK,EAAED,kBAAkB,MAAM,CAAC;wBACvCR;wBACAE,SAASpC,oBAAoBoC,SAAStB;wBACtCuB;oBACF;gBACF;gBAGF,IAAIO,eAAetB;gBAEnB,IAAI,OAAOP,gBAAgB,YAAY;oBACrC6B,eAAe,MAAM7B,YAAYO;gBACnC;gBAEA,qEAAqE;gBAErE,MAAMC,QAAQC,GAAG,CACfoB,aAAanB,GAAG,CAAC,OAAOC;oBACtB,MAAM,EAAEW,EAAE,EAAE,GAAGX;oBACf,IAAI;wBACF,MAAMmB,eAAe,MAAMlC,QAAQT,SAAS,CAACwB;wBAC7C,OAAOmB;oBACT,EAAE,OAAOC,KAAc;wBACrBnC,QAAQoC,MAAM,CAACC,KAAK,CAAC;4BACnBF,KAAK,CAAC,sCAAsC,EAAET,GAAG,kBAAkB,EAAEY,KAAKC,SAAS,CACjFJ,KACA,CAAC;wBACL;oBACF;gBACF;YAEJ,OAAO;gBACLnC,QAAQoC,MAAM,CAACI,IAAI,CAAC;oBAAEC,KAAK;gBAAqB;YAClD;QACF,EAAE,OAAON,KAAc;YACrB,MAAMM,MAAM,CAAC,8DAA8D,EAAE3C,iBAAiB,CAAC,CAAC;YAChGE,QAAQoC,MAAM,CAACC,KAAK,CAAC;gBAAEF,KAAKM;YAAI;QAClC;IACF;IAEA,OAAO/C;AACT,EAAC"}
|
|
@@ -1,24 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "generateSubmissionCollection", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: function() {
|
|
8
|
-
return generateSubmissionCollection;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
const _createCharge = /*#__PURE__*/ _interop_require_default(require("./hooks/createCharge"));
|
|
12
|
-
const _sendEmail = /*#__PURE__*/ _interop_require_default(require("./hooks/sendEmail"));
|
|
13
|
-
function _interop_require_default(obj) {
|
|
14
|
-
return obj && obj.__esModule ? obj : {
|
|
15
|
-
default: obj
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
const generateSubmissionCollection = (formConfig)=>{
|
|
1
|
+
import { createCharge } from './hooks/createCharge.js';
|
|
2
|
+
import { sendEmail } from './hooks/sendEmail.js';
|
|
3
|
+
// all settings can be overridden by the config
|
|
4
|
+
export const generateSubmissionCollection = (formConfig)=>{
|
|
19
5
|
const formSlug = formConfig?.formOverrides?.slug || 'forms';
|
|
20
6
|
const newConfig = {
|
|
21
7
|
...formConfig?.formSubmissionOverrides || {},
|
|
8
|
+
slug: formConfig?.formSubmissionOverrides?.slug || 'form-submissions',
|
|
22
9
|
access: {
|
|
23
10
|
create: ()=>true,
|
|
24
11
|
read: ({ req: { user } })=>!!user,
|
|
@@ -32,20 +19,21 @@ const generateSubmissionCollection = (formConfig)=>{
|
|
|
32
19
|
fields: [
|
|
33
20
|
{
|
|
34
21
|
name: 'form',
|
|
22
|
+
type: 'relationship',
|
|
35
23
|
admin: {
|
|
36
24
|
readOnly: true
|
|
37
25
|
},
|
|
38
26
|
relationTo: formSlug,
|
|
39
27
|
required: true,
|
|
40
|
-
|
|
41
|
-
validate: async (value, { payload })=>{
|
|
28
|
+
validate: async (value, { req: { payload }, req })=>{
|
|
42
29
|
/* Don't run in the client side */ if (!payload) return true;
|
|
43
30
|
if (payload) {
|
|
44
|
-
let
|
|
31
|
+
let _existingForm;
|
|
45
32
|
try {
|
|
46
|
-
|
|
33
|
+
_existingForm = await payload.findByID({
|
|
47
34
|
id: value,
|
|
48
|
-
collection: formSlug
|
|
35
|
+
collection: formSlug,
|
|
36
|
+
req
|
|
49
37
|
});
|
|
50
38
|
return true;
|
|
51
39
|
} catch (error) {
|
|
@@ -56,19 +44,20 @@ const generateSubmissionCollection = (formConfig)=>{
|
|
|
56
44
|
},
|
|
57
45
|
{
|
|
58
46
|
name: 'submissionData',
|
|
47
|
+
type: 'array',
|
|
59
48
|
admin: {
|
|
60
49
|
readOnly: true
|
|
61
50
|
},
|
|
62
51
|
fields: [
|
|
63
52
|
{
|
|
64
53
|
name: 'field',
|
|
65
|
-
|
|
66
|
-
|
|
54
|
+
type: 'text',
|
|
55
|
+
required: true
|
|
67
56
|
},
|
|
68
57
|
{
|
|
69
58
|
name: 'value',
|
|
70
|
-
required: true,
|
|
71
59
|
type: 'text',
|
|
60
|
+
required: true,
|
|
72
61
|
validate: (value)=>{
|
|
73
62
|
// TODO:
|
|
74
63
|
// create a validation function that dynamically
|
|
@@ -83,45 +72,44 @@ const generateSubmissionCollection = (formConfig)=>{
|
|
|
83
72
|
return 'This field is required.';
|
|
84
73
|
}
|
|
85
74
|
}
|
|
86
|
-
]
|
|
87
|
-
type: 'array'
|
|
75
|
+
]
|
|
88
76
|
},
|
|
89
77
|
...formConfig?.formSubmissionOverrides?.fields || []
|
|
90
78
|
],
|
|
91
79
|
hooks: {
|
|
92
80
|
beforeChange: [
|
|
93
|
-
(data)=>(
|
|
94
|
-
(data)=>(
|
|
81
|
+
(data)=>createCharge(data, formConfig),
|
|
82
|
+
(data)=>sendEmail(data, formConfig),
|
|
95
83
|
...formConfig?.formSubmissionOverrides?.hooks?.beforeChange || []
|
|
96
84
|
],
|
|
97
85
|
...formConfig?.formSubmissionOverrides?.hooks || {}
|
|
98
|
-
}
|
|
99
|
-
slug: formConfig?.formSubmissionOverrides?.slug || 'form-submissions'
|
|
86
|
+
}
|
|
100
87
|
};
|
|
101
88
|
const paymentFieldConfig = formConfig?.fields?.payment;
|
|
102
89
|
if (paymentFieldConfig) {
|
|
103
90
|
newConfig.fields.push({
|
|
104
91
|
name: 'payment',
|
|
92
|
+
type: 'group',
|
|
105
93
|
admin: {
|
|
106
94
|
readOnly: true
|
|
107
95
|
},
|
|
108
96
|
fields: [
|
|
109
97
|
{
|
|
110
98
|
name: 'field',
|
|
111
|
-
|
|
112
|
-
|
|
99
|
+
type: 'text',
|
|
100
|
+
label: 'Field'
|
|
113
101
|
},
|
|
114
102
|
{
|
|
115
103
|
name: 'status',
|
|
116
|
-
|
|
117
|
-
|
|
104
|
+
type: 'text',
|
|
105
|
+
label: 'Status'
|
|
118
106
|
},
|
|
119
107
|
{
|
|
120
108
|
name: 'amount',
|
|
109
|
+
type: 'number',
|
|
121
110
|
admin: {
|
|
122
111
|
description: 'Amount in cents'
|
|
123
|
-
}
|
|
124
|
-
type: 'number'
|
|
112
|
+
}
|
|
125
113
|
},
|
|
126
114
|
{
|
|
127
115
|
name: 'paymentProcessor',
|
|
@@ -129,31 +117,30 @@ const generateSubmissionCollection = (formConfig)=>{
|
|
|
129
117
|
},
|
|
130
118
|
{
|
|
131
119
|
name: 'creditCard',
|
|
120
|
+
type: 'group',
|
|
132
121
|
fields: [
|
|
133
122
|
{
|
|
134
123
|
name: 'token',
|
|
135
|
-
|
|
136
|
-
|
|
124
|
+
type: 'text',
|
|
125
|
+
label: 'token'
|
|
137
126
|
},
|
|
138
127
|
{
|
|
139
128
|
name: 'brand',
|
|
140
|
-
|
|
141
|
-
|
|
129
|
+
type: 'text',
|
|
130
|
+
label: 'Brand'
|
|
142
131
|
},
|
|
143
132
|
{
|
|
144
133
|
name: 'number',
|
|
145
|
-
|
|
146
|
-
|
|
134
|
+
type: 'text',
|
|
135
|
+
label: 'Number'
|
|
147
136
|
}
|
|
148
137
|
],
|
|
149
|
-
label: 'Credit Card'
|
|
150
|
-
type: 'group'
|
|
138
|
+
label: 'Credit Card'
|
|
151
139
|
}
|
|
152
|
-
]
|
|
153
|
-
type: 'group'
|
|
140
|
+
]
|
|
154
141
|
});
|
|
155
142
|
}
|
|
156
143
|
return newConfig;
|
|
157
144
|
};
|
|
158
145
|
|
|
159
|
-
//# sourceMappingURL=
|
|
146
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/collections/FormSubmissions/index.ts"],"sourcesContent":["import type { CollectionConfig } from 'payload/types'\n\nimport type { PluginConfig } from '../../types.js'\n\nimport { createCharge } from './hooks/createCharge.js'\nimport { sendEmail } from './hooks/sendEmail.js'\n\n// all settings can be overridden by the config\nexport const generateSubmissionCollection = (formConfig: PluginConfig): CollectionConfig => {\n const formSlug = formConfig?.formOverrides?.slug || 'forms'\n\n const newConfig: CollectionConfig = {\n ...(formConfig?.formSubmissionOverrides || {}),\n slug: formConfig?.formSubmissionOverrides?.slug || 'form-submissions',\n access: {\n create: () => true,\n read: ({ req: { user } }) => !!user, // logged-in users,\n update: () => false,\n ...(formConfig?.formSubmissionOverrides?.access || {}),\n },\n admin: {\n ...(formConfig?.formSubmissionOverrides?.admin || {}),\n enableRichTextRelationship: false,\n },\n fields: [\n {\n name: 'form',\n type: 'relationship',\n admin: {\n readOnly: true,\n },\n relationTo: formSlug,\n required: true,\n validate: async (value, { req: { payload }, req }) => {\n /* Don't run in the client side */\n if (!payload) return true\n\n if (payload) {\n let _existingForm\n\n try {\n _existingForm = await payload.findByID({\n id: value,\n collection: formSlug,\n req,\n })\n\n return true\n } catch (error) {\n return 'Cannot create this submission because this form does not exist.'\n }\n }\n },\n },\n {\n name: 'submissionData',\n type: 'array',\n admin: {\n readOnly: true,\n },\n fields: [\n {\n name: 'field',\n type: 'text',\n required: true,\n },\n {\n name: 'value',\n type: 'text',\n required: true,\n validate: (value: unknown) => {\n // TODO:\n // create a validation function that dynamically\n // relies on the field type and its options as configured.\n\n // How to access sibling data from this field?\n // Need the `name` of the field in order to validate it.\n\n // Might not be possible to use this validation function.\n // Instead, might need to do all validation in a `beforeValidate` collection hook.\n\n if (typeof value !== 'undefined') {\n return true\n }\n\n return 'This field is required.'\n },\n },\n ],\n },\n ...(formConfig?.formSubmissionOverrides?.fields || []),\n ],\n hooks: {\n beforeChange: [\n (data) => createCharge(data, formConfig),\n (data) => sendEmail(data, formConfig),\n ...(formConfig?.formSubmissionOverrides?.hooks?.beforeChange || []),\n ],\n ...(formConfig?.formSubmissionOverrides?.hooks || {}),\n },\n }\n\n const paymentFieldConfig = formConfig?.fields?.payment\n\n if (paymentFieldConfig) {\n newConfig.fields.push({\n name: 'payment',\n type: 'group',\n admin: {\n readOnly: true,\n },\n fields: [\n {\n name: 'field',\n type: 'text',\n label: 'Field',\n },\n {\n name: 'status',\n type: 'text',\n label: 'Status',\n },\n {\n name: 'amount',\n type: 'number',\n admin: {\n description: 'Amount in cents',\n },\n },\n {\n name: 'paymentProcessor',\n type: 'text',\n },\n {\n name: 'creditCard',\n type: 'group',\n fields: [\n {\n name: 'token',\n type: 'text',\n label: 'token',\n },\n {\n name: 'brand',\n type: 'text',\n label: 'Brand',\n },\n {\n name: 'number',\n type: 'text',\n label: 'Number',\n },\n ],\n label: 'Credit Card',\n },\n ],\n })\n }\n\n return newConfig\n}\n"],"names":["createCharge","sendEmail","generateSubmissionCollection","formConfig","formSlug","formOverrides","slug","newConfig","formSubmissionOverrides","access","create","read","req","user","update","admin","enableRichTextRelationship","fields","name","type","readOnly","relationTo","required","validate","value","payload","_existingForm","findByID","id","collection","error","hooks","beforeChange","data","paymentFieldConfig","payment","push","label","description"],"mappings":"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"}
|
|
@@ -1,60 +1,11 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
return DynamicFieldSelector;
|
|
10
|
-
}
|
|
11
|
-
});
|
|
12
|
-
const _forms = require("payload/components/forms");
|
|
13
|
-
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
|
|
14
|
-
function _getRequireWildcardCache(nodeInterop) {
|
|
15
|
-
if (typeof WeakMap !== "function") return null;
|
|
16
|
-
var cacheBabelInterop = new WeakMap();
|
|
17
|
-
var cacheNodeInterop = new WeakMap();
|
|
18
|
-
return (_getRequireWildcardCache = function(nodeInterop) {
|
|
19
|
-
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
|
20
|
-
})(nodeInterop);
|
|
21
|
-
}
|
|
22
|
-
function _interop_require_wildcard(obj, nodeInterop) {
|
|
23
|
-
if (!nodeInterop && obj && obj.__esModule) {
|
|
24
|
-
return obj;
|
|
25
|
-
}
|
|
26
|
-
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
27
|
-
return {
|
|
28
|
-
default: obj
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
var cache = _getRequireWildcardCache(nodeInterop);
|
|
32
|
-
if (cache && cache.has(obj)) {
|
|
33
|
-
return cache.get(obj);
|
|
34
|
-
}
|
|
35
|
-
var newObj = {};
|
|
36
|
-
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
37
|
-
for(var key in obj){
|
|
38
|
-
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
39
|
-
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
40
|
-
if (desc && (desc.get || desc.set)) {
|
|
41
|
-
Object.defineProperty(newObj, key, desc);
|
|
42
|
-
} else {
|
|
43
|
-
newObj[key] = obj[key];
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
newObj.default = obj;
|
|
48
|
-
if (cache) {
|
|
49
|
-
cache.set(obj, newObj);
|
|
50
|
-
}
|
|
51
|
-
return newObj;
|
|
52
|
-
}
|
|
53
|
-
const DynamicFieldSelector = (props)=>{
|
|
54
|
-
const { fields, getDataByPath } = (0, _forms.useForm)();
|
|
55
|
-
const [options, setOptions] = (0, _react.useState)([]);
|
|
56
|
-
(0, _react.useEffect)(()=>{
|
|
57
|
-
// @ts-ignore
|
|
2
|
+
import { Select } from '@payloadcms/ui/fields/Select';
|
|
3
|
+
import { useForm } from '@payloadcms/ui/forms/Form';
|
|
4
|
+
import React, { useEffect, useState } from 'react';
|
|
5
|
+
export const DynamicFieldSelector = (props)=>{
|
|
6
|
+
const { fields, getDataByPath } = useForm();
|
|
7
|
+
const [options, setOptions] = useState([]);
|
|
8
|
+
useEffect(()=>{
|
|
58
9
|
const fields = getDataByPath('fields');
|
|
59
10
|
if (fields) {
|
|
60
11
|
const allNonPaymentFields = fields.map((block)=>{
|
|
@@ -73,10 +24,12 @@ const DynamicFieldSelector = (props)=>{
|
|
|
73
24
|
fields,
|
|
74
25
|
getDataByPath
|
|
75
26
|
]);
|
|
76
|
-
|
|
27
|
+
// TODO: label from config is Record<string, string> | false | string
|
|
28
|
+
// but the FormFieldBase type has only label?: string, changing FormFieldBase breaks other ui components
|
|
29
|
+
return /*#__PURE__*/ React.createElement(Select, {
|
|
77
30
|
...props,
|
|
78
31
|
options: options
|
|
79
32
|
});
|
|
80
33
|
};
|
|
81
34
|
|
|
82
|
-
//# sourceMappingURL=
|
|
35
|
+
//# sourceMappingURL=DynamicFieldSelector.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/collections/Forms/DynamicFieldSelector.tsx"],"sourcesContent":["'use client'\n\nimport type { TextField } from 'payload/types'\n\nimport { Select } from '@payloadcms/ui/fields/Select'\nimport { useForm } from '@payloadcms/ui/forms/Form'\nimport React, { useEffect, useState } from 'react'\n\nimport type { SelectFieldOption } from '../../types.js'\n\nexport const DynamicFieldSelector: React.FC<TextField> = (props) => {\n const { fields, getDataByPath } = useForm()\n\n const [options, setOptions] = useState<SelectFieldOption[]>([])\n\n useEffect(() => {\n const fields: any[] = getDataByPath('fields')\n\n if (fields) {\n const allNonPaymentFields = fields\n .map((block): SelectFieldOption | null => {\n const { name, blockType, label } = block\n\n if (blockType !== 'payment') {\n return {\n label,\n value: name,\n }\n }\n\n return null\n })\n .filter(Boolean)\n setOptions(allNonPaymentFields)\n }\n }, [fields, getDataByPath])\n\n // TODO: label from config is Record<string, string> | false | string\n // but the FormFieldBase type has only label?: string, changing FormFieldBase breaks other ui components\n return <Select {...props} options={options} />\n}\n"],"names":["Select","useForm","React","useEffect","useState","DynamicFieldSelector","props","fields","getDataByPath","options","setOptions","allNonPaymentFields","map","block","name","blockType","label","value","filter","Boolean"],"mappings":"AAAA;AAIA,SAASA,MAAM,QAAQ,+BAA8B;AACrD,SAASC,OAAO,QAAQ,4BAA2B;AACnD,OAAOC,SAASC,SAAS,EAAEC,QAAQ,QAAQ,QAAO;AAIlD,OAAO,MAAMC,uBAA4C,CAACC;IACxD,MAAM,EAAEC,MAAM,EAAEC,aAAa,EAAE,GAAGP;IAElC,MAAM,CAACQ,SAASC,WAAW,GAAGN,SAA8B,EAAE;IAE9DD,UAAU;QACR,MAAMI,SAAgBC,cAAc;QAEpC,IAAID,QAAQ;YACV,MAAMI,sBAAsBJ,OACzBK,GAAG,CAAC,CAACC;gBACJ,MAAM,EAAEC,IAAI,EAAEC,SAAS,EAAEC,KAAK,EAAE,GAAGH;gBAEnC,IAAIE,cAAc,WAAW;oBAC3B,OAAO;wBACLC;wBACAC,OAAOH;oBACT;gBACF;gBAEA,OAAO;YACT,GACCI,MAAM,CAACC;YACVT,WAAWC;QACb;IACF,GAAG;QAACJ;QAAQC;KAAc;IAE1B,qEAAqE;IACrE,yGAAyG;IACzG,qBAAO,oBAACR;QAAQ,GAAGM,KAAK;QAAEG,SAASA;;AACrC,EAAC"}
|
|
@@ -1,64 +1,16 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: function() {
|
|
9
|
-
return DynamicPriceSelector;
|
|
10
|
-
}
|
|
11
|
-
});
|
|
12
|
-
const _forms = require("payload/components/forms");
|
|
13
|
-
const _utilities = require("payload/components/utilities");
|
|
14
|
-
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
|
|
15
|
-
function _getRequireWildcardCache(nodeInterop) {
|
|
16
|
-
if (typeof WeakMap !== "function") return null;
|
|
17
|
-
var cacheBabelInterop = new WeakMap();
|
|
18
|
-
var cacheNodeInterop = new WeakMap();
|
|
19
|
-
return (_getRequireWildcardCache = function(nodeInterop) {
|
|
20
|
-
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
|
21
|
-
})(nodeInterop);
|
|
22
|
-
}
|
|
23
|
-
function _interop_require_wildcard(obj, nodeInterop) {
|
|
24
|
-
if (!nodeInterop && obj && obj.__esModule) {
|
|
25
|
-
return obj;
|
|
26
|
-
}
|
|
27
|
-
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
28
|
-
return {
|
|
29
|
-
default: obj
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
var cache = _getRequireWildcardCache(nodeInterop);
|
|
33
|
-
if (cache && cache.has(obj)) {
|
|
34
|
-
return cache.get(obj);
|
|
35
|
-
}
|
|
36
|
-
var newObj = {};
|
|
37
|
-
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
38
|
-
for(var key in obj){
|
|
39
|
-
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
40
|
-
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
41
|
-
if (desc && (desc.get || desc.set)) {
|
|
42
|
-
Object.defineProperty(newObj, key, desc);
|
|
43
|
-
} else {
|
|
44
|
-
newObj[key] = obj[key];
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
newObj.default = obj;
|
|
49
|
-
if (cache) {
|
|
50
|
-
cache.set(obj, newObj);
|
|
51
|
-
}
|
|
52
|
-
return newObj;
|
|
53
|
-
}
|
|
54
|
-
const DynamicPriceSelector = (props)=>{
|
|
2
|
+
import { Text } from '@payloadcms/ui/fields/Text';
|
|
3
|
+
import { useWatchForm } from '@payloadcms/ui/forms/Form';
|
|
4
|
+
import { useLocale } from '@payloadcms/ui/providers/Locale';
|
|
5
|
+
import React, { useEffect, useState } from 'react';
|
|
6
|
+
export const DynamicPriceSelector = (props)=>{
|
|
55
7
|
const { label, path } = props;
|
|
56
|
-
const { fields, getData, getDataByPath } =
|
|
57
|
-
const locale =
|
|
58
|
-
const [isNumberField, setIsNumberField] =
|
|
59
|
-
const [valueType, setValueType] =
|
|
8
|
+
const { fields, getData, getDataByPath } = useWatchForm();
|
|
9
|
+
const locale = useLocale();
|
|
10
|
+
const [isNumberField, setIsNumberField] = useState();
|
|
11
|
+
const [valueType, setValueType] = useState();
|
|
60
12
|
// only number fields can use 'valueOfField`
|
|
61
|
-
|
|
13
|
+
useEffect(()=>{
|
|
62
14
|
if (path) {
|
|
63
15
|
const parentPath = path.split('.').slice(0, -1).join('.');
|
|
64
16
|
const paymentFieldData = getDataByPath(parentPath);
|
|
@@ -81,7 +33,7 @@ const DynamicPriceSelector = (props)=>{
|
|
|
81
33
|
]);
|
|
82
34
|
// TODO: make this a number field, block by Payload
|
|
83
35
|
if (valueType === 'static') {
|
|
84
|
-
return /*#__PURE__*/
|
|
36
|
+
return /*#__PURE__*/ React.createElement(Text, props);
|
|
85
37
|
}
|
|
86
38
|
const localeCode = typeof locale === 'object' && 'code' in locale ? locale.code : locale;
|
|
87
39
|
const localLabels = typeof label === 'object' ? label : {
|
|
@@ -89,7 +41,7 @@ const DynamicPriceSelector = (props)=>{
|
|
|
89
41
|
};
|
|
90
42
|
const labelValue = localLabels[localeCode] || localLabels['en'] || '';
|
|
91
43
|
if (valueType === 'valueOfField' && !isNumberField) {
|
|
92
|
-
return /*#__PURE__*/
|
|
44
|
+
return /*#__PURE__*/ React.createElement("div", null, /*#__PURE__*/ React.createElement("div", null, labelValue), /*#__PURE__*/ React.createElement("div", {
|
|
93
45
|
style: {
|
|
94
46
|
color: '#9A9A9A'
|
|
95
47
|
}
|
|
@@ -98,4 +50,4 @@ const DynamicPriceSelector = (props)=>{
|
|
|
98
50
|
return null;
|
|
99
51
|
};
|
|
100
52
|
|
|
101
|
-
//# sourceMappingURL=
|
|
53
|
+
//# sourceMappingURL=DynamicPriceSelector.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/collections/Forms/DynamicPriceSelector.tsx"],"sourcesContent":["'use client'\n\nimport type { TextFieldProps } from '@payloadcms/ui/fields/Text'\nimport type { Data } from 'payload/types'\n\nimport { Text } from '@payloadcms/ui/fields/Text'\nimport { useWatchForm } from '@payloadcms/ui/forms/Form'\nimport { useLocale } from '@payloadcms/ui/providers/Locale'\nimport React, { useEffect, useState } from 'react'\n\ntype FieldWithID = {\n id: string\n name: string\n}\n\nexport const DynamicPriceSelector: React.FC<TextFieldProps> = (props) => {\n const { label, path } = props\n\n const { fields, getData, getDataByPath } = useWatchForm()\n\n const locale = useLocale()\n\n const [isNumberField, setIsNumberField] = useState<boolean>()\n const [valueType, setValueType] = useState<'static' | 'valueOfField'>()\n\n // only number fields can use 'valueOfField`\n useEffect(() => {\n if (path) {\n const parentPath = path.split('.').slice(0, -1).join('.')\n const paymentFieldData: any = getDataByPath(parentPath)\n\n if (paymentFieldData) {\n const { fieldToUse, valueType } = paymentFieldData\n\n setValueType(valueType)\n\n const { fields: allFields }: Data = getData()\n const field = allFields.find((field: FieldWithID) => field.name === fieldToUse)\n\n if (field) {\n const { blockType } = field\n setIsNumberField(blockType === 'number')\n }\n }\n }\n }, [fields, path, getDataByPath, getData])\n\n // TODO: make this a number field, block by Payload\n if (valueType === 'static') {\n return <Text {...props} />\n }\n\n const localeCode = typeof locale === 'object' && 'code' in locale ? locale.code : locale\n\n const localLabels = typeof label === 'object' ? label : { [localeCode]: label }\n const labelValue = localLabels[localeCode] || localLabels['en'] || ''\n\n if (valueType === 'valueOfField' && !isNumberField) {\n return (\n <div>\n <div>{labelValue}</div>\n <div\n style={{\n color: '#9A9A9A',\n }}\n >\n The selected field must be a number field.\n </div>\n </div>\n )\n }\n\n return null\n}\n"],"names":["Text","useWatchForm","useLocale","React","useEffect","useState","DynamicPriceSelector","props","label","path","fields","getData","getDataByPath","locale","isNumberField","setIsNumberField","valueType","setValueType","parentPath","split","slice","join","paymentFieldData","fieldToUse","allFields","field","find","name","blockType","localeCode","code","localLabels","labelValue","div","style","color"],"mappings":"AAAA;AAKA,SAASA,IAAI,QAAQ,6BAA4B;AACjD,SAASC,YAAY,QAAQ,4BAA2B;AACxD,SAASC,SAAS,QAAQ,kCAAiC;AAC3D,OAAOC,SAASC,SAAS,EAAEC,QAAQ,QAAQ,QAAO;AAOlD,OAAO,MAAMC,uBAAiD,CAACC;IAC7D,MAAM,EAAEC,KAAK,EAAEC,IAAI,EAAE,GAAGF;IAExB,MAAM,EAAEG,MAAM,EAAEC,OAAO,EAAEC,aAAa,EAAE,GAAGX;IAE3C,MAAMY,SAASX;IAEf,MAAM,CAACY,eAAeC,iBAAiB,GAAGV;IAC1C,MAAM,CAACW,WAAWC,aAAa,GAAGZ;IAElC,4CAA4C;IAC5CD,UAAU;QACR,IAAIK,MAAM;YACR,MAAMS,aAAaT,KAAKU,KAAK,CAAC,KAAKC,KAAK,CAAC,GAAG,CAAC,GAAGC,IAAI,CAAC;YACrD,MAAMC,mBAAwBV,cAAcM;YAE5C,IAAII,kBAAkB;gBACpB,MAAM,EAAEC,UAAU,EAAEP,SAAS,EAAE,GAAGM;gBAElCL,aAAaD;gBAEb,MAAM,EAAEN,QAAQc,SAAS,EAAE,GAASb;gBACpC,MAAMc,QAAQD,UAAUE,IAAI,CAAC,CAACD,QAAuBA,MAAME,IAAI,KAAKJ;gBAEpE,IAAIE,OAAO;oBACT,MAAM,EAAEG,SAAS,EAAE,GAAGH;oBACtBV,iBAAiBa,cAAc;gBACjC;YACF;QACF;IACF,GAAG;QAAClB;QAAQD;QAAMG;QAAeD;KAAQ;IAEzC,mDAAmD;IACnD,IAAIK,cAAc,UAAU;QAC1B,qBAAO,oBAAChB,MAASO;IACnB;IAEA,MAAMsB,aAAa,OAAOhB,WAAW,YAAY,UAAUA,SAASA,OAAOiB,IAAI,GAAGjB;IAElF,MAAMkB,cAAc,OAAOvB,UAAU,WAAWA,QAAQ;QAAE,CAACqB,WAAW,EAAErB;IAAM;IAC9E,MAAMwB,aAAaD,WAAW,CAACF,WAAW,IAAIE,WAAW,CAAC,KAAK,IAAI;IAEnE,IAAIf,cAAc,kBAAkB,CAACF,eAAe;QAClD,qBACE,oBAACmB,2BACC,oBAACA,aAAKD,2BACN,oBAACC;YACCC,OAAO;gBACLC,OAAO;YACT;WACD;IAKP;IAEA,OAAO;AACT,EAAC"}
|