|
| 1 | +import contentstack from "../../contentstack.app.mjs"; |
| 2 | +import { |
| 3 | + parseArray, parseEntry, |
| 4 | +} from "../../common/utils.mjs"; |
| 5 | + |
| 6 | +const createDocLink = "https://www.contentstack.com/docs/developers/apis/content-management-api#create-an-entry"; |
| 7 | +const updateDocLink = "https://www.contentstack.com/docs/developers/apis/content-management-api#update-an-entry"; |
| 8 | + |
| 9 | +export default { |
| 10 | + props: { |
| 11 | + contentstack, |
| 12 | + contentType: { |
| 13 | + propDefinition: [ |
| 14 | + contentstack, |
| 15 | + "contentType", |
| 16 | + ], |
| 17 | + reloadProps: true, |
| 18 | + }, |
| 19 | + }, |
| 20 | + async additionalProps() { |
| 21 | + if (!this.contentType) { |
| 22 | + return {}; |
| 23 | + } |
| 24 | + try { |
| 25 | + return await this.buildFieldProps(this.contentType); |
| 26 | + } catch { |
| 27 | + return { |
| 28 | + entryObj: { |
| 29 | + type: "object", |
| 30 | + label: "Entry", |
| 31 | + description: `Enter the entry object as JSON. [See the documentation](${this.isUpdate() |
| 32 | + ? updateDocLink |
| 33 | + : createDocLink}) for more information.`, |
| 34 | + }, |
| 35 | + }; |
| 36 | + } |
| 37 | + }, |
| 38 | + methods: { |
| 39 | + getType(field) { |
| 40 | + if (field.data_type === "boolean") { |
| 41 | + return "boolean"; |
| 42 | + } |
| 43 | + if (field.data_type === "json") { |
| 44 | + return "object"; |
| 45 | + } |
| 46 | + return field.multiple |
| 47 | + ? "string[]" |
| 48 | + : "string"; |
| 49 | + }, |
| 50 | + isUpdate() { |
| 51 | + return false; |
| 52 | + }, |
| 53 | + async getOptions(field) { |
| 54 | + if (field.data_type === "reference") { |
| 55 | + const referenceContentType = field.reference_to[0]; |
| 56 | + const { entries } = await this.contentstack.listEntries({ |
| 57 | + contentType: referenceContentType, |
| 58 | + }); |
| 59 | + return entries?.map(({ |
| 60 | + uid: value, title: label, |
| 61 | + }) => ({ |
| 62 | + value, |
| 63 | + label: label ?? value, |
| 64 | + })) || []; |
| 65 | + } |
| 66 | + if (field.data_type === "file") { |
| 67 | + const { assets } = await this.contentstack.listAssets(); |
| 68 | + return assets?.map(({ |
| 69 | + uid: value, title: label, |
| 70 | + }) => ({ |
| 71 | + value, |
| 72 | + label: label ?? value, |
| 73 | + })) || []; |
| 74 | + } |
| 75 | + return undefined; |
| 76 | + }, |
| 77 | + async buildFieldProps(contentType) { |
| 78 | + const props = {}; |
| 79 | + const { content_type: { schema } } = await this.contentstack.getContentType({ |
| 80 | + contentType, |
| 81 | + }); |
| 82 | + for (const field of schema) { |
| 83 | + props[field.uid] = { |
| 84 | + type: this.getType(field), |
| 85 | + label: field.display_name ?? field.uid, |
| 86 | + description: `Value of field ${field.display_name}. Field type: \`${field.data_type}\``, |
| 87 | + optional: this.isUpdate() |
| 88 | + ? true |
| 89 | + : !field.mandatory, |
| 90 | + options: await this.getOptions(field), |
| 91 | + }; |
| 92 | + } |
| 93 | + return props; |
| 94 | + }, |
| 95 | + async buildEntry() { |
| 96 | + if (this.entryObj) { |
| 97 | + return parseEntry(this.entryObj); |
| 98 | + } |
| 99 | + const { content_type: { schema } } = await this.contentstack.getContentType({ |
| 100 | + contentType: this.contentType, |
| 101 | + }); |
| 102 | + const entry = {}; |
| 103 | + for (const field of schema) { |
| 104 | + if (!this[field.uid]) { |
| 105 | + continue; |
| 106 | + } |
| 107 | + if (field.data_type === "reference") { |
| 108 | + if (field.multiple) { |
| 109 | + const referenceField = parseArray(this[field.uid]); |
| 110 | + entry[field.uid] = referenceField?.map((value) => ({ |
| 111 | + uid: value, |
| 112 | + _content_type_uid: field.reference_to[0], |
| 113 | + })); |
| 114 | + } else { |
| 115 | + entry[field.uid] = { |
| 116 | + uid: this[field.uid], |
| 117 | + _content_type_uid: field.reference_to[0], |
| 118 | + }; |
| 119 | + } |
| 120 | + continue; |
| 121 | + } |
| 122 | + entry[field.uid] = this[field.uid]; |
| 123 | + } |
| 124 | + return parseEntry(entry); |
| 125 | + }, |
| 126 | + }, |
| 127 | +}; |
0 commit comments