@webbio/strapi-plugin-page-builder 0.9.9-platform → 0.10.1-platform

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webbio/strapi-plugin-page-builder",
3
- "version": "0.9.9-platform",
3
+ "version": "0.10.1-platform",
4
4
  "description": "This is the description of the plugin.",
5
5
  "scripts": {
6
6
  "develop": "tsc -p tsconfig.server.json -w",
@@ -42,7 +42,7 @@
42
42
  },
43
43
  "peerDependencies": {
44
44
  "@strapi/strapi": "^4.15.0",
45
- "@webbio/strapi-plugin-slug": "^3.2.6",
45
+ "@webbio/strapi-plugin-slug": "^3.3.0",
46
46
  "react": "^17.0.0 || ^18.0.0",
47
47
  "react-dom": "^17.0.0 || ^18.0.0",
48
48
  "react-router-dom": "^5.3.4",
@@ -1,3 +1,4 @@
1
+ import uniq from 'lodash/uniq';
1
2
  import { Strapi } from '@strapi/strapi';
2
3
  import { PLATFORM_UID } from '../../shared/utils/constants';
3
4
 
@@ -54,6 +55,8 @@ export default async ({ strapi }: { strapi: Strapi }) => {
54
55
  }
55
56
  });
56
57
 
58
+ const uniquePermissions = uniq(permissions);
59
+
57
60
  return {
58
61
  $and: [
59
62
  {
@@ -62,9 +65,19 @@ export default async ({ strapi }: { strapi: Strapi }) => {
62
65
  }
63
66
  },
64
67
  {
65
- 'pageType.uid': {
66
- $in: permissions
67
- }
68
+ $or: [
69
+ {
70
+ 'pageType.uid': {
71
+ $in: uniquePermissions
72
+ }
73
+ },
74
+ {
75
+ 'pageType.uid': {
76
+ // This means there is no pageType (which is the case for any other page)
77
+ $eq: null
78
+ }
79
+ }
80
+ ]
68
81
  }
69
82
  ]
70
83
  };
@@ -137,100 +137,86 @@ export default async ({ strapi }: { strapi: Strapi }) => {
137
137
  }
138
138
  },
139
139
  async afterUpdate(event) {
140
+ await afterUpdate(event?.['result']);
141
+ },
142
+ async afterUpdateMany(event) {
140
143
  try {
141
- const data = event?.['result'];
142
- const hasCollectionTypeRelation =
143
- data && data?.collectionTypeData?.[0] && data?.collectionTypeData[0].__type && data?.collectionTypeData[0];
144
-
145
- if (hasCollectionTypeRelation) {
146
- const isPublished = data.publishedAt !== undefined;
147
- const pageData = isPublished ? { hasPage: !!data.publishedAt } : {};
148
-
149
- await strapi.entityService?.update(data.collectionTypeData[0].__type, data.collectionTypeData[0].id, {
150
- data: {
151
- ...pageData,
152
- id: data.collectionTypeData[0].id,
153
- lifecycleState: {
154
- exit: true
155
- }
144
+ const pagesToUpdate = event.params.where?.id?.['$in'];
145
+ const pages: Record<string, any>[] | undefined = (await strapi.entityService?.findMany(PAGE_UID, {
146
+ populate: {
147
+ collectionTypeData: true
148
+ },
149
+ filters: {
150
+ id: {
151
+ $in: pagesToUpdate
156
152
  }
157
- });
158
- }
153
+ }
154
+ })) as any;
159
155
 
160
- // This used to be the way to go. Not sure if the new way works
161
- // const data = event?.params?.data;
162
- // if (data.collectionTypeData?.__type && data.collectionTypeData.id) {
163
- // await strapi.entityService?.update(data.collectionTypeData.__type, data.collectionTypeData.id, {
164
- // data: {
165
- // id: data.collectionTypeData.id,
166
- // hasPage: true,
167
- // lifecycleState: {
168
- // exit: true
169
- // }
170
- // }
171
- // });
172
- // }
156
+ if (pages && pages.length > 0) {
157
+ await Promise.all(pages?.map((p) => afterUpdate(p)));
158
+ }
173
159
  } catch (error) {
174
- console.error('Failed to save hasPage data', error);
160
+ console.error('Failed to save hasPage many data', error);
175
161
  }
176
162
  },
177
- // async afterCreate(event) {
178
- // const data = event?.params?.data;
179
- // if (data.collectionTypeData?.__type && data.collectionTypeData.id) {
180
- // await strapi.entityService?.update(data.collectionTypeData.__type, data.collectionTypeData.id, {
181
- // data: {
182
- // id: data.collectionTypeData.id,
183
- // hasPage: true,
184
- // lifecycleState: {
185
- // exit: true
186
- // }
187
- // }
188
- // });
189
- // }
190
- // },
191
163
  async beforeDelete(event) {
192
- const originalEntity = (await strapi.entityService?.findOne(PAGE_UID, event.params.where.id, {
193
- populate: {
194
- collectionTypeData: true
195
- }
196
- })) as Record<string, any> | undefined;
197
-
198
- if (originalEntity?.collectionTypeData?.[0]?.__type && originalEntity?.collectionTypeData?.[0]?.id) {
199
- await strapi.entityService?.update(
200
- originalEntity.collectionTypeData[0].__type,
201
- originalEntity.collectionTypeData[0].id,
202
- {
203
- data: {
204
- id: originalEntity.collectionTypeData[0].id,
205
- hasPage: false
206
- }
207
- }
208
- );
209
- }
164
+ await beforeDelete(event.params.where.id);
210
165
  },
211
166
  async beforeDeleteMany(event) {
212
167
  const pagesToDisconnect = event.params.where?.['$and']?.[0]?.id?.['$in'];
213
168
 
214
169
  for (const pageId of pagesToDisconnect) {
215
- const originalEntity = (await strapi.entityService?.findOne(PAGE_UID, pageId, {
216
- populate: {
217
- collectionTypeData: true
218
- }
219
- })) as Record<string, any> | undefined;
170
+ await beforeDelete(pageId);
171
+ }
172
+ }
173
+ });
174
+ };
220
175
 
221
- if (originalEntity?.collectionTypeData?.[0]?.__type && originalEntity?.collectionTypeData?.[0]?.id) {
222
- await strapi.entityService?.update(
223
- originalEntity.collectionTypeData[0].__type,
224
- originalEntity.collectionTypeData[0].id,
225
- {
226
- data: {
227
- id: originalEntity.collectionTypeData[0].id,
228
- hasPage: false
229
- }
230
- }
231
- );
176
+ const beforeDelete = async (id: number) => {
177
+ const originalEntity = (await strapi.entityService?.findOne(PAGE_UID, id, {
178
+ populate: {
179
+ collectionTypeData: true
180
+ }
181
+ })) as Record<string, any> | undefined;
182
+
183
+ if (originalEntity?.collectionTypeData?.[0]?.__type && originalEntity?.collectionTypeData?.[0]?.id) {
184
+ await strapi.entityService?.update(
185
+ originalEntity.collectionTypeData[0].__type,
186
+ originalEntity.collectionTypeData[0].id,
187
+ {
188
+ data: {
189
+ id: originalEntity.collectionTypeData[0].id,
190
+ hasPage: false
232
191
  }
233
192
  }
193
+ );
194
+ }
195
+ };
196
+
197
+ const afterUpdate = async (result: Record<string, any>) => {
198
+ try {
199
+ const hasCollectionTypeRelation =
200
+ result &&
201
+ result?.collectionTypeData?.[0] &&
202
+ result?.collectionTypeData[0].__type &&
203
+ result?.collectionTypeData[0];
204
+
205
+ if (hasCollectionTypeRelation) {
206
+ const isPublished = result.publishedAt !== undefined;
207
+ const pageData = isPublished ? { hasPage: !!result.publishedAt } : {};
208
+
209
+ await strapi.entityService?.update(result.collectionTypeData[0].__type, result.collectionTypeData[0].id, {
210
+ data: {
211
+ ...pageData,
212
+ id: result.collectionTypeData[0].id,
213
+ lifecycleState: {
214
+ exit: true
215
+ }
216
+ }
217
+ });
234
218
  }
235
- });
219
+ } catch (error) {
220
+ console.error('Failed to save hasPage data', error);
221
+ }
236
222
  };