Skip to content

Commit b3e8b11

Browse files
committed
Applying comon behavior to 'new field'
1 parent b371f6e commit b3e8b11

File tree

3 files changed

+63
-58
lines changed

3 files changed

+63
-58
lines changed

components/airtable_oauth/sources/common/common-webhook-field.mjs

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,46 @@ export default {
1010
"tableFields",
1111
];
1212
},
13-
},
14-
props: {
15-
...common.props,
16-
watchSchemasOfFieldIds: {
17-
propDefinition: [
18-
airtable,
19-
"sortFieldId",
20-
(c) => ({
21-
baseId: c.baseId,
22-
tableId: c.tableId,
23-
}),
24-
],
25-
type: "string[]",
26-
label: "Field Schemas to Watch",
27-
description:
28-
"Only emit events for updates that modify the schemas of these fields. If omitted, schemas of all fields within the table/view/base are watched",
13+
async saveAdditionalData() {
14+
const tableData = await this.airtable.listTables({ baseId: this.baseId });
15+
const filteredData = tableData?.tables?.map(({ id, name, fields}) => ({
16+
id,
17+
name,
18+
fields,
19+
}));
20+
if (filteredData?.length) {
21+
this.db.set("tableSchemas", filteredData);
22+
}
23+
},
24+
async emitEvent(payload) {
25+
const [tableId, tableData] = Object.entries(payload.changedTablesById)[0];
26+
const [operation, fieldObj] = Object.entries(tableData)[0];
27+
const [fieldId, fieldUpdateInfo] = Object.entries(fieldObj)[0];
28+
29+
const updateType = operation === "createdFieldsById" ? "created" : "updated";
30+
31+
let table = {
32+
id: tableId,
33+
}
34+
let field = {
35+
id: fieldId,
36+
}
37+
38+
const tableSchemas = this.db.get("tableSchemas");
39+
if (tableSchemas) {
40+
table = tableSchemas.find(({ id }) => id === tableId)
41+
field = table?.fields.find(({ id }) => id === fieldId);
42+
delete table.fields;
43+
}
44+
45+
const summary = `Field ${updateType}: ${field.name ?? fieldUpdateInfo?.name ?? field.id}`
46+
47+
this.$emit({
48+
originalPayload: payload,
49+
table,
50+
field,
51+
fieldUpdateInfo,
52+
}, this.generateMeta(payload, summary));
2953
},
3054
},
3155
};

components/airtable_oauth/sources/common/common-webhook.mjs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,13 @@ export default {
114114
const meta = this.generateMeta(payload);
115115
this.$emit(payload, meta);
116116
},
117+
emitEvent(payload) {
118+
// sources may call this to customize event emission, but it is
119+
// a separate method so that the source may fall back to default
120+
this.emitDefaultEvent(payload);
121+
},
117122
async saveAdditionalData() {
118-
// some sources may call this to fetch additional data (e.g. field schemas)
123+
// sources may call this to fetch additional data (e.g. field schemas)
119124
// and it can be silently ignored when not required
120125
return true;
121126
}

components/airtable_oauth/sources/new-or-modified-field/new-or-modified-field.mjs

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,46 +16,22 @@ export default {
1616
"update",
1717
];
1818
},
19-
async saveAdditionalData() {
20-
const tableData = await this.airtable.listTables({ baseId: this.baseId });
21-
const filteredData = tableData?.tables?.map(({ id, name, fields}) => ({
22-
id,
23-
name,
24-
fields,
25-
}));
26-
if (filteredData?.length) {
27-
this.db.set("tableSchemas", filteredData);
28-
}
29-
},
30-
async emitEvent(payload) {
31-
const [tableId, tableData] = Object.entries(payload.changedTablesById)[0];
32-
const [operation, fieldObj] = Object.entries(tableData)[0];
33-
const [fieldId, fieldUpdateInfo] = Object.entries(fieldObj)[0];
34-
35-
const updateType = operation === "createdFieldsById" ? "created" : "updated";
36-
37-
let table = {
38-
id: tableId,
39-
}
40-
let field = {
41-
id: fieldId,
42-
}
43-
44-
const tableSchemas = this.db.get("tableSchemas");
45-
if (tableSchemas) {
46-
table = tableSchemas.find(({ id }) => id === tableId)
47-
field = table?.fields.find(({ id }) => id === fieldId);
48-
delete table.fields;
49-
}
50-
51-
const summary = `Field ${updateType}: ${field.name ?? fieldUpdateInfo?.name ?? field.id}`
52-
53-
this.$emit({
54-
originalPayload: payload,
55-
table,
56-
field,
57-
fieldUpdateInfo,
58-
}, this.generateMeta(payload, summary));
59-
}
6019
},
20+
props: {
21+
...common.props,
22+
watchSchemasOfFieldIds: {
23+
propDefinition: [
24+
airtable,
25+
"sortFieldId",
26+
(c) => ({
27+
baseId: c.baseId,
28+
tableId: c.tableId,
29+
}),
30+
],
31+
type: "string[]",
32+
label: "Field Schemas to Watch",
33+
description:
34+
"Only emit events for updates that modify the schemas of these fields. If omitted, schemas of all fields within the table/view/base are watched",
35+
},
36+
}
6137
};

0 commit comments

Comments
 (0)