Skip to content

Commit 0e43160

Browse files
committed
Create and Update item
1 parent 2772446 commit 0e43160

File tree

3 files changed

+109
-9
lines changed

3 files changed

+109
-9
lines changed

components/webflow_v2/actions/create-collection-item/create-collection-item.mjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,17 @@ export default {
5353
// eslint-disable-next-line no-unused-vars
5454
siteId,
5555
collectionId,
56-
...fields
56+
...fieldData
5757
} = this;
5858

59-
const response = await app.createCollectionItem({
59+
const response = await app.createCollectionItem(
6060
collectionId,
61-
fieldData: {
62-
...fields,
61+
{
62+
fieldData,
6363
isArchived: false,
6464
isDraft: false,
6565
},
66-
});
66+
);
6767

6868
$.export("$summary", `Successfully created collection item ${fields.name ?? ""}`);
6969

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import app from "../../webflow_v2.app.mjs";
2+
3+
export default {
4+
key: "webflow_v2-update-collection-item",
5+
name: "Update Collection Item",
6+
description:
7+
"Update collection item. [See the documentation](https://developers.webflow.com/#update-collection-item)",
8+
version: "0.0.{{ts}}",
9+
type: "action",
10+
props: {
11+
app,
12+
siteId: {
13+
propDefinition: [app, "sites"],
14+
},
15+
collectionId: {
16+
propDefinition: [
17+
app,
18+
"collections",
19+
(c) => ({
20+
siteId: c.siteId,
21+
}),
22+
],
23+
reloadProps: true,
24+
},
25+
itemId: {
26+
propDefinition: [
27+
app,
28+
"items",
29+
(c) => ({
30+
collectionId: c.collectionId,
31+
}),
32+
],
33+
},
34+
},
35+
async additionalProps() {
36+
const props = {};
37+
if (!this.collectionId) {
38+
return props;
39+
}
40+
const { fields } = await this.app.getCollection(this.collectionId);
41+
for (const field of fields) {
42+
if (
43+
field.editable &&
44+
field.slug !== "isArchived" &&
45+
field.slug !== "isDraft"
46+
) {
47+
props[field.slug] = {
48+
type: "string",
49+
label: field.name,
50+
description:
51+
field.slug === "name"
52+
? "Name given to the Item."
53+
: field.slug === "slug"
54+
? "URL structure of the Item in your site. Note: Updates to an item slug will break all links referencing the old slug."
55+
: "See the documentation for additional information about [Field Types & Item Values](https://developers.webflow.com/reference/field-types-item-values).",
56+
optional: true,
57+
};
58+
}
59+
}
60+
61+
return props;
62+
},
63+
async run({ $ }) {
64+
const {
65+
app,
66+
// eslint-disable-next-line no-unused-vars
67+
siteId,
68+
collectionId,
69+
itemId,
70+
name,
71+
slug,
72+
...customFields
73+
} = this;
74+
75+
const item = await app.getCollectionItem(collectionId, itemId);
76+
77+
const response = await app.updateCollectionItem(collectionId, itemId, {
78+
isArchived: false,
79+
isDraft: false,
80+
fieldData: {
81+
...customFields,
82+
name: name || item.name,
83+
slug: slug || item.slug,
84+
},
85+
});
86+
87+
$.export("$summary", "Successfully updated collection item");
88+
89+
return response;
90+
},
91+
};

components/webflow_v2/webflow_v2.app.mjs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,14 @@ export default {
120120
const response = await this.webflowClient().sites.getCustomDomain(siteId);
121121
return response?.customDomains;
122122
},
123-
async getSite(siteId) {
123+
getSite(siteId) {
124124
return this.webflowClient().sites.get(siteId);
125125
},
126126
async listSites() {
127127
const response = await this.webflowClient().sites.list();
128128
return response?.sites;
129129
},
130-
async getCollection(collectionId) {
130+
getCollection(collectionId) {
131131
return this.webflowClient().collections.get(collectionId);
132132
},
133133
async listCollections(siteId) {
@@ -146,8 +146,17 @@ export default {
146146

147147
return response?.items;
148148
},
149-
async getCollectionItem(collectionId, itemId) {
149+
getCollectionItem(collectionId, itemId) {
150150
return this.webflowClient().collections.items.getItem(collectionId, itemId);
151-
}
151+
},
152+
deleteCollectionItem(collectionId, itemId) {
153+
return this.webflowClient().collections.items.deleteItem(collectionId, itemId);
154+
},
155+
createCollectionItem(collectionId, data) {
156+
return this.webflowClient().collections.items.createItem(collectionId, data);
157+
},
158+
updateCollectionItem(collectionId, itemId, data) {
159+
return this.webflowClient().collections.items.updateItem(collectionId, itemId, data);
160+
},
152161
},
153162
};

0 commit comments

Comments
 (0)