|
| 1 | +Fliplet.FormBuilder.field('timeStamp', { |
| 2 | + name: 'Time Stamp', |
| 3 | + category: 'Date & time', |
| 4 | + props: { |
| 5 | + value: { |
| 6 | + type: Object |
| 7 | + }, |
| 8 | + placeholder: { |
| 9 | + type: String |
| 10 | + }, |
| 11 | + description: { |
| 12 | + type: String |
| 13 | + }, |
| 14 | + createdAt: { |
| 15 | + type: Boolean, |
| 16 | + default: true |
| 17 | + }, |
| 18 | + updatedAt: { |
| 19 | + type: Boolean, |
| 20 | + default: true |
| 21 | + }, |
| 22 | + showLabel: { |
| 23 | + type: Boolean, |
| 24 | + default: false |
| 25 | + }, |
| 26 | + label: undefined |
| 27 | + }, |
| 28 | + created: function() { |
| 29 | + Fliplet.Hooks.on('afterFormSubmit', this.afterFormSubmit); |
| 30 | + }, |
| 31 | + destroyed: function() { |
| 32 | + Fliplet.Hooks.off('afterFormSubmit', this.afterFormSubmit); |
| 33 | + }, |
| 34 | + mounted: function() { |
| 35 | + this.value = { |
| 36 | + createdAt: this.createdAt, |
| 37 | + updatedAt: this.updatedAt |
| 38 | + }; |
| 39 | + this.$emit('_input', this.name, this.value); |
| 40 | + }, |
| 41 | + watch: { |
| 42 | + createdAt: function(val) { |
| 43 | + this.value = { |
| 44 | + createdAt: val, |
| 45 | + updatedAt: this.updatedAt |
| 46 | + }; |
| 47 | + this.$emit('_input', this.name, this.value); |
| 48 | + }, |
| 49 | + updatedAt: function(val) { |
| 50 | + this.value = { |
| 51 | + createdAt: this.createdAt, |
| 52 | + updatedAt: val |
| 53 | + }; |
| 54 | + this.$emit('_input', this.name, this.value); |
| 55 | + } |
| 56 | + }, |
| 57 | + methods: { |
| 58 | + afterFormSubmit: async function(data, form) { |
| 59 | + const fields = form.$instance.fields; |
| 60 | + const hasTimeStamp = fields.some(field => field._type === 'flTimeStamp'); |
| 61 | + |
| 62 | + if (!hasTimeStamp) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + const dataSourceId = data.result && data.result.dataSourceId; |
| 67 | + |
| 68 | + if (!dataSourceId) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + const connection = await Fliplet.DataSources.connect(dataSourceId); |
| 73 | + |
| 74 | + if (data.result.createdAt !== data.result.updatedAt && this.updatedAt && this.createdAt) { |
| 75 | + connection.update(data.result.id, { |
| 76 | + 'Created at': data.result.createdAt, |
| 77 | + 'Last updated': data.result.updatedAt |
| 78 | + }); |
| 79 | + } else if (data.result.createdAt !== data.result.updatedAt && this.updatedAt) { |
| 80 | + connection.update(data.result.id, { |
| 81 | + 'Last updated': data.result.updatedAt |
| 82 | + }); |
| 83 | + } else if (this.createdAt) { |
| 84 | + connection.update(data.result.id, { |
| 85 | + 'Created at': data.result.createdAt |
| 86 | + }); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +}); |
0 commit comments