@webbio/strapi-plugin-page-builder 0.3.2 → 0.3.3-legacy

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.
Files changed (77) hide show
  1. package/README.md +21 -6
  2. package/admin/src/api/collection-type.ts +1 -7
  3. package/admin/src/components/EditView/CollectionTypeSearch/index.tsx +11 -14
  4. package/admin/src/components/EditView/CollectionTypeSettings/CreatePageButton/index.tsx +27 -9
  5. package/admin/src/components/EditView/CollectionTypeSettings/index.tsx +16 -6
  6. package/admin/src/components/EditView/Details/index.tsx +1 -1
  7. package/admin/src/components/EditView/PageSettings/index.tsx +6 -57
  8. package/admin/src/components/PageTypeFilter/index.tsx +17 -0
  9. package/admin/src/components/PageTypeFilter/page-type-filter.tsx +130 -0
  10. package/admin/src/index.tsx +2 -2
  11. package/admin/src/utils/sanitizeModules.ts +92 -9
  12. package/dist/package.json +11 -9
  13. package/dist/server/bootstrap.js +24 -40
  14. package/dist/server/content-types/user-categories/schema.json +18 -0
  15. package/dist/server/content-types/user-category/schema.json +23 -0
  16. package/dist/server/controllers/index.js +1 -3
  17. package/dist/server/controllers/platform.js +5 -5
  18. package/dist/server/controllers/private-content.js +18 -0
  19. package/dist/server/controllers/sitemap.js +29 -0
  20. package/dist/server/controllers/user-category.js +4 -0
  21. package/dist/server/graphql/page-by-path.js +113 -0
  22. package/dist/server/graphql/pages-by-uid.js +7 -9
  23. package/dist/server/policies/isAuthorizedPage.js +11 -0
  24. package/dist/server/routes/index.js +0 -21
  25. package/dist/server/routes/user-category.js +4 -0
  26. package/dist/server/schema/page-end.json +0 -5
  27. package/dist/server/schema/platform-start.json +0 -10
  28. package/dist/server/schema/template-end.json +40 -0
  29. package/dist/server/services/builder.js +5 -15
  30. package/dist/server/services/email.js +160 -0
  31. package/dist/server/services/index.js +1 -3
  32. package/dist/server/services/platform.js +6 -11
  33. package/dist/server/services/private-content/components/admin-email.json +22 -0
  34. package/dist/server/services/private-content/components/email.json +22 -0
  35. package/dist/server/services/private-content/components/platform-email.json +33 -0
  36. package/dist/server/services/private-content/constants/index.js +16 -0
  37. package/dist/server/services/private-content/graphql/index.js +77 -0
  38. package/dist/server/services/private-content/graphql/resolvers/findOnePage.js +40 -0
  39. package/dist/server/services/private-content/graphql/resolvers/findPage.js +44 -0
  40. package/dist/server/services/private-content/graphql/resolvers/forgot-password.js +29 -0
  41. package/dist/server/services/private-content/graphql/resolvers/login.js +49 -0
  42. package/dist/server/services/private-content/graphql/resolvers/register.js +68 -0
  43. package/dist/server/services/private-content/graphql/resolvers/reset-password.js +41 -0
  44. package/dist/server/services/private-content/graphql/types/index.js +89 -0
  45. package/dist/server/services/private-content/index.js +94 -0
  46. package/dist/server/services/private-content/mail-template/txtMail.email.template.text.js +12 -0
  47. package/dist/server/services/private-content/page.js +22 -0
  48. package/dist/server/services/private-content/platform.js +22 -0
  49. package/dist/server/services/private-content/schemas/index.js +30 -0
  50. package/dist/server/services/private-content/user.js +170 -0
  51. package/dist/server/services/sitemap.js +78 -0
  52. package/dist/server/services/template.js +1 -2
  53. package/dist/server/services/user-category.js +4 -0
  54. package/dist/server/utils/strapi.js +1 -4
  55. package/dist/shared/utils/constants.js +1 -3
  56. package/dist/tsconfig.server.tsbuildinfo +1 -1
  57. package/package.json +11 -9
  58. package/server/bootstrap/collection-type-lifecycles.ts +1 -1
  59. package/server/bootstrap.ts +24 -43
  60. package/server/controllers/index.ts +1 -3
  61. package/server/graphql/pages-by-uid.ts +7 -9
  62. package/server/routes/index.ts +0 -21
  63. package/server/schema/page-end.json +0 -5
  64. package/server/services/builder.ts +6 -18
  65. package/server/services/index.ts +1 -3
  66. package/server/services/template.ts +1 -2
  67. package/server/utils/strapi.ts +1 -5
  68. package/shared/utils/constants.ts +0 -2
  69. package/admin/src/api/platform.ts +0 -34
  70. package/admin/src/components/EditView/Platform/platform-select.tsx +0 -30
  71. package/admin/src/components/PageFilters/PageTypeFilter/index.tsx +0 -39
  72. package/admin/src/components/PageFilters/PlatformFilter/index.tsx +0 -28
  73. package/admin/src/components/PageFilters/filters.tsx +0 -180
  74. package/admin/src/components/PageFilters/index.tsx +0 -30
  75. package/server/controllers/platform.ts +0 -21
  76. package/server/schema/platform-start.json +0 -31
  77. package/server/services/platform.ts +0 -36
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webbio/strapi-plugin-page-builder",
3
- "version": "0.3.2",
3
+ "version": "0.3.3-legacy",
4
4
  "description": "This is the description of the plugin.",
5
5
  "scripts": {
6
6
  "develop": "tsc -p tsconfig.server.json -w",
@@ -20,14 +20,16 @@
20
20
  "url": "https://github.com/webbio/strapi-plugin-page-builder.git"
21
21
  },
22
22
  "dependencies": {
23
- "@strapi/design-system": "^1.11.0",
24
- "@strapi/helper-plugin": "^4.15.0",
23
+ "@strapi/design-system": "^1.13.1",
24
+ "@strapi/helper-plugin": "<4.15.0",
25
25
  "@strapi/icons": "^1.11.0",
26
- "@strapi/typescript-utils": "^4.15.0",
27
- "@strapi/utils": "^4.15.0",
28
- "react-select": "^5.7.4"
26
+ "@strapi/typescript-utils": "<4.15.0",
27
+ "@strapi/utils": "<4.15.0",
28
+ "react-select": "^5.7.4",
29
+ "slugify": "^1.6.6"
29
30
  },
30
31
  "devDependencies": {
32
+ "@strapi/typescript-utils": "<4.15.0",
31
33
  "@types/react": "^18.2.21",
32
34
  "@types/react-dom": "^18.2.7",
33
35
  "@types/react-router-dom": "^5.3.3",
@@ -39,8 +41,8 @@
39
41
  "typescript": "5.1.6"
40
42
  },
41
43
  "peerDependencies": {
42
- "@strapi/strapi": "^4.15.0",
43
- "@webbio/strapi-plugin-slug": "^2.0.2",
44
+ "@strapi/strapi": "<4.15.0",
45
+ "@webbio/strapi-plugin-slug": "^2.0.5",
44
46
  "react": "^17.0.0 || ^18.0.0",
45
47
  "react-dom": "^17.0.0 || ^18.0.0",
46
48
  "react-router-dom": "^5.3.4",
@@ -59,7 +61,7 @@
59
61
  }
60
62
  ],
61
63
  "engines": {
62
- "node": ">=14.19.1 <=18.x.x",
64
+ "node": ">=14.19.1 <=20.x.x",
63
65
  "npm": ">=6.0.0"
64
66
  },
65
67
  "license": "MIT",
@@ -37,7 +37,7 @@ export default async ({ strapi }: { strapi: Strapi }) => {
37
37
  data: {
38
38
  updatedAt: data.updatedAt
39
39
  }
40
- } as Record<string, any>);
40
+ } as any);
41
41
  }
42
42
  } catch (error) {
43
43
  console.log('[PAGEBUILDER PLUGIN] collection type lifecycle error: ', error);
@@ -1,6 +1,6 @@
1
1
  import { Common, Strapi } from '@strapi/strapi';
2
2
  import { errors } from '@strapi/utils';
3
- import { PAGE_TYPE_UID, PAGE_UID, PLATFORM_UID } from '../shared/utils/constants';
3
+ import { PAGE_TYPE_UID, PAGE_UID } from '../shared/utils/constants';
4
4
  import permissions from './bootstrap/permissions';
5
5
  import collectionTypeLifecycles from './bootstrap/collection-type-lifecycles';
6
6
 
@@ -27,7 +27,6 @@ export default async ({ strapi }: { strapi: Strapi }) => {
27
27
  let { data } = event.params;
28
28
  const collectionTypeId = data?.collectionTypeId;
29
29
  const pageTypeId = data?.pageType.connect?.[0]?.id || data.initialPageType;
30
- const platformId = data?.platform.connect?.[0]?.id;
31
30
 
32
31
  if (collectionTypeId && pageTypeId) {
33
32
  const pageType: Record<string, any> | undefined | null = await strapi.entityService?.findOne(
@@ -40,26 +39,10 @@ export default async ({ strapi }: { strapi: Strapi }) => {
40
39
 
41
40
  const page: Record<string, any>[] = collectionToConnect?.page as any;
42
41
 
43
- const foundPlatforms: Record<string, any> | undefined | null = await strapi.entityService?.findMany(
44
- PLATFORM_UID,
45
- {
46
- populate: '*',
47
- filters: {
48
- pagetype: {
49
- uid: pageType?.uid
50
- }
51
- }
52
- }
53
- );
54
-
55
42
  if (page && page.length > 0) {
56
43
  throw new errors.ValidationError('You can only link one CollectionType to one page');
57
44
  }
58
45
 
59
- if (platformId && !foundPlatforms?.some((platform) => platform.id === platformId)) {
60
- throw new errors.ValidationError('Platform not found');
61
- }
62
-
63
46
  data = updateCollectionTypeData(data, collectionTypeId, pageType?.uid);
64
47
  }
65
48
  },
@@ -115,6 +98,8 @@ export default async ({ strapi }: { strapi: Strapi }) => {
115
98
  data = updateCollectionTypeData(data, collectionTypeId, pageType?.uid);
116
99
  }
117
100
 
101
+ // needs to check if the collectionTypeData is already connected to another page
102
+ // if so, remove the hasPage for filtering
118
103
  if (
119
104
  data.collectionTypeData &&
120
105
  originalEntity?.collectionTypeData?.[0]?.__type &&
@@ -137,32 +122,28 @@ export default async ({ strapi }: { strapi: Strapi }) => {
137
122
  }
138
123
  },
139
124
  async afterUpdate(event) {
140
- const data = event?.params?.data;
141
-
142
- if (data.collectionTypeData?.__type && data.collectionTypeData.id) {
143
- await strapi.entityService?.update(data.collectionTypeData.__type, data.collectionTypeData.id, {
144
- data: {
145
- id: data.collectionTypeData.id,
146
- hasPage: true,
147
- lifecycleState: {
148
- exit: true
149
- }
150
- }
151
- });
152
- }
153
- },
154
- async afterCreate(event) {
155
- const data = event?.params?.data;
156
- if (data.collectionTypeData?.__type && data.collectionTypeData.id) {
157
- await strapi.entityService?.update(data.collectionTypeData.__type, data.collectionTypeData.id, {
158
- data: {
159
- id: data.collectionTypeData.id,
160
- hasPage: true,
161
- lifecycleState: {
162
- exit: true
125
+ try {
126
+ // Result not correctly typed within Strapi
127
+ const data = event?.['result'];
128
+ const hasCollectionTypeRelation =
129
+ data && data?.collectionTypeData?.[0] && data?.collectionTypeData[0].__type && data?.collectionTypeData[0].id;
130
+
131
+ if (hasCollectionTypeRelation) {
132
+ const isPublished = data.publishedAt !== undefined;
133
+ const pageData = isPublished ? { hasPage: !!data.publishedAt } : {};
134
+
135
+ await strapi.entityService?.update(data.collectionTypeData[0].__type, data.collectionTypeData[0].id, {
136
+ data: {
137
+ ...pageData,
138
+ id: data.collectionTypeData[0].id,
139
+ lifecycleState: {
140
+ exit: true
141
+ }
163
142
  }
164
- }
165
- });
143
+ });
144
+ }
145
+ } catch (error) {
146
+ console.error('Failed to save hasPage data', error);
166
147
  }
167
148
  },
168
149
  async beforeDelete(event) {
@@ -2,12 +2,10 @@ import page from './page';
2
2
  import pageType from './page-type';
3
3
  import collectionTypes from './collection-types';
4
4
  import template from './template';
5
- import platform from './platform';
6
5
 
7
6
  export default {
8
7
  page,
9
8
  'page-type': pageType,
10
9
  'collection-types': collectionTypes,
11
- template,
12
- platform
10
+ template
13
11
  };
@@ -16,16 +16,13 @@ const getPageInfoFromUID = (strapi: Strapi) => {
16
16
  pageSize: Int
17
17
  }`;
18
18
  const entityInfo = collectionTypes.map((collectionType) => {
19
- return `
20
- extend type ${collectionType.type}EntityResponseCollection {
21
- metaInfo: MetaInfo
22
- }
23
-
24
- type Query {
25
- get${collectionType.type}Pages(filters: ${collectionType.type}FiltersInput,pagination: PaginationArg, pageFilters: PageFiltersInput): ${collectionType.type}EntityResponseCollection
26
- }
19
+ return `extend type ${collectionType.type}EntityResponseCollection {
20
+ metaInfo: MetaInfo
21
+ }
27
22
 
28
- `;
23
+ type Query {
24
+ get${collectionType.type}Pages(filters: ${collectionType.type}FiltersInput, pagination: PaginationArg, sort:[String], pageFilters: PageFiltersInput): ${collectionType.type}EntityResponseCollection
25
+ }`;
29
26
  });
30
27
 
31
28
  return `${metaInfo} ${entityInfo}`;
@@ -67,6 +64,7 @@ const getPageInfoFromUID = (strapi: Strapi) => {
67
64
  hasPage: { $eq: true },
68
65
  page: { ...transformedpageArgs.filters }
69
66
  },
67
+ sort: transformedArgs.sort,
70
68
  start: start,
71
69
  limit: limit
72
70
  });
@@ -52,27 +52,6 @@ const routes = {
52
52
  handler: 'template.findOneById'
53
53
  }
54
54
  ]
55
- },
56
- platform: {
57
- type: 'admin',
58
- prefix: undefined,
59
- routes: [
60
- {
61
- method: 'GET',
62
- path: '/platform',
63
- handler: 'platform.findAll'
64
- },
65
- {
66
- method: 'GET',
67
- path: '/platform/:uid',
68
- handler: 'platform.findOneByUid'
69
- },
70
- {
71
- method: 'GET',
72
- path: '/platform/:platform/page-types',
73
- handler: 'platform.findPageTypesByPlatform'
74
- }
75
- ]
76
55
  }
77
56
  };
78
57
 
@@ -86,11 +86,6 @@
86
86
  "type": "relation",
87
87
  "relation": "oneToOne",
88
88
  "target": "api::page-type.page-type"
89
- },
90
- "platform": {
91
- "type": "relation",
92
- "relation": "oneToOne",
93
- "target": "api::platform.platform"
94
89
  }
95
90
  }
96
91
  }
@@ -4,14 +4,13 @@ import { Common } from '@strapi/strapi';
4
4
  import pageStart from '../schema/page-start.json';
5
5
  import pageEnd from '../schema/page-end.json';
6
6
  import pluginId from '../../admin/src/pluginId';
7
- import { PAGE_UID, TEMPLATE_UID, PAGE_TYPE_UID, PLATFORM_UID } from '../../shared/utils/constants';
7
+ import { PAGE_UID, TEMPLATE_UID, PAGE_TYPE_UID } from '../../shared/utils/constants';
8
8
  import { reloadStrapiOnLoad } from '../utils/reload-strapi-on-load';
9
9
  import pageTypeStart from '../schema/page-type-start.json';
10
10
  import pageTypeEnd from '../schema/page-type-end.json';
11
11
  import templateStart from '../schema/template-start.json';
12
- import platformStart from '../schema/platform-start.json';
13
12
 
14
- const UIDS: Common.UID.ContentType[] = [TEMPLATE_UID, PAGE_TYPE_UID, PLATFORM_UID, PAGE_UID];
13
+ const UIDS: Common.UID.ContentType[] = [TEMPLATE_UID, PAGE_TYPE_UID, PAGE_UID];
15
14
 
16
15
  export default {
17
16
  async buildContentTypes() {
@@ -88,10 +87,6 @@ export default {
88
87
  [TEMPLATE_UID]: {
89
88
  create: this.getTemplateContentType(),
90
89
  update: this.getTemplateContentType()
91
- },
92
- [PLATFORM_UID]: {
93
- create: this.getPlatformContentType(),
94
- update: this.getPlatformContentType()
95
90
  }
96
91
  };
97
92
 
@@ -99,30 +94,23 @@ export default {
99
94
  },
100
95
  getPageContentType(create?: boolean) {
101
96
  const page = create ? pageStart : pageEnd;
102
- const contentType = this.mergeCollectionTypeWithOld(page, PAGE_UID);
97
+ const contentType = this.mergeCollectionTypeWithModules(page, PAGE_UID);
103
98
 
104
99
  return { uid: PAGE_UID, contentType };
105
100
  },
106
101
  getPageTypeContentType(create?: boolean) {
107
102
  const pageType = create ? pageTypeStart : pageTypeEnd;
108
- const contentType = this.mergeCollectionTypeWithOld(pageType, PAGE_TYPE_UID);
103
+ const contentType = this.mergeCollectionTypeWithModules(pageType, PAGE_TYPE_UID);
109
104
 
110
105
  return { uid: PAGE_TYPE_UID, contentType };
111
106
  },
112
107
  getTemplateContentType() {
113
108
  const template = templateStart;
114
- const contentType = this.mergeCollectionTypeWithOld(template, TEMPLATE_UID);
109
+ const contentType = this.mergeCollectionTypeWithModules(template, TEMPLATE_UID);
115
110
 
116
111
  return { uid: TEMPLATE_UID, contentType };
117
112
  },
118
- getPlatformContentType() {
119
- const platform = platformStart;
120
-
121
- const contentType = this.mergeCollectionTypeWithOld(platform, PLATFORM_UID);
122
-
123
- return { uid: PLATFORM_UID, contentType };
124
- },
125
- mergeCollectionTypeWithOld(collectionType: Record<string, any>, uid: Common.UID.ContentType) {
113
+ mergeCollectionTypeWithModules(collectionType: Record<string, any>, uid: Common.UID.ContentType) {
126
114
  const { pluginOptions: oldPluginOptions, __schema__: oldSchema } = (strapi.contentType(uid) as any) || {};
127
115
  const modulesFromConfig = oldSchema?.attributes?.modules ? this.getConfigModuleComponents() : undefined;
128
116
 
@@ -3,13 +3,11 @@ import builder from './builder';
3
3
  import pageType from './page-type';
4
4
  import collectionTypes from './collection-types';
5
5
  import template from './template';
6
- import platform from './platform';
7
6
 
8
7
  export default {
9
8
  page,
10
9
  builder,
11
10
  'page-type': pageType,
12
11
  'collection-types': collectionTypes,
13
- template,
14
- platform
12
+ template
15
13
  };
@@ -8,7 +8,6 @@ export default {
8
8
  return foundTemplates;
9
9
  },
10
10
  async findOne(id: number) {
11
- const getpopulatedTemplate = getPopulatedEntity(TEMPLATE_UID, id);
12
- return getpopulatedTemplate;
11
+ return await getPopulatedEntity(TEMPLATE_UID, id);
13
12
  }
14
13
  };
@@ -14,17 +14,13 @@ export const getDeepPopulate = (uid: Common.UID.Component, populate?: any, depth
14
14
  return Object.keys(attributes).reduce((populateAcc, attributeName) => {
15
15
  const attribute = attributes[attributeName];
16
16
 
17
- if (attribute.type === 'relation') {
18
- return populateAcc;
19
- }
20
-
21
17
  if (attribute.type === 'component') {
22
18
  populateAcc[attributeName] = {
23
19
  populate: getDeepPopulate(attribute.component, null, depth + 1)
24
20
  };
25
21
  }
26
22
 
27
- if (attribute.type === 'media') {
23
+ if (attribute.type === 'relation' || attribute.type === 'media') {
28
24
  populateAcc[attributeName] = true;
29
25
  }
30
26
 
@@ -1,6 +1,4 @@
1
1
  export const PAGE_UID = 'api::page.page';
2
2
  export const TEMPLATE_UID = 'api::template.template';
3
3
  export const PAGE_TYPE_UID = 'api::page-type.page-type';
4
- export const PLATFORM_UID = 'api::platform.platform';
5
4
  export const PAGE_TYPE_PAGE = 'page';
6
- export const PLATFORM = 'platform';
@@ -1,34 +0,0 @@
1
- import { useQuery } from 'react-query';
2
-
3
- import { useFetchClient } from '@strapi/helper-plugin';
4
-
5
- import getRequestUrl from '../utils/getRequestUrl';
6
- import { PageType } from './page-type';
7
-
8
- export type Platform = {
9
- id: number;
10
- title?: string;
11
- pageTypes?: PageType[];
12
- };
13
-
14
- const QUERY_KEY = 'platforms';
15
-
16
- const fetchPlatforms = async ({ fetchClient }: any): Promise<Platform[]> => {
17
- const { get } = fetchClient;
18
- const result = await get(getRequestUrl(`/platform`));
19
-
20
- return result?.data?.map((entity: any) => ({
21
- id: entity.id,
22
- title: entity.title,
23
- pageTypes: entity.pageTypes
24
- }));
25
- };
26
-
27
- export const useGetPlatforms = (params: any) => {
28
- const fetchClient = useFetchClient();
29
- params = {
30
- ...params,
31
- fetchClient
32
- };
33
- return useQuery<Platform[], Error>(QUERY_KEY, () => fetchPlatforms(params));
34
- };
@@ -1,30 +0,0 @@
1
- import React from 'react';
2
-
3
- import { SingleSelect, SingleSelectOption } from '@strapi/design-system';
4
-
5
- import { Platform } from '../../../api/platform';
6
- import { PLATFORM } from '../../../../../shared/utils/constants';
7
-
8
- interface Props {
9
- onChange: (platformId: string) => void;
10
- selectedPlatform?: Platform | null;
11
- platforms?: Platform[];
12
- }
13
-
14
- export const PlatformSelect = ({ onChange, platforms, selectedPlatform }: Props) => {
15
- return (
16
- <SingleSelect
17
- label="Platform"
18
- placeholder="Choose a platform"
19
- value={selectedPlatform?.id || PLATFORM}
20
- onChange={onChange}
21
- >
22
- <SingleSelectOption value={PLATFORM}>Platform</SingleSelectOption>
23
- {platforms?.map((pageType: Platform) => (
24
- <SingleSelectOption key={pageType?.id} value={pageType?.id}>
25
- {pageType?.title}
26
- </SingleSelectOption>
27
- ))}
28
- </SingleSelect>
29
- );
30
- };
@@ -1,39 +0,0 @@
1
- import React from 'react';
2
-
3
- import { SingleSelectOption, SingleSelect } from '@strapi/design-system';
4
-
5
- import { PageType } from '../../../api/page-type';
6
- import { PAGE_TYPE_NO_FILTER } from '../../../constants';
7
- import { PAGE_TYPE_PAGE } from '../../../../../shared/utils/constants';
8
-
9
- interface Props {
10
- onChange: (pageTypeUId: string) => void;
11
- selectedPageTypeUid?: string;
12
- pageTypes?: PageType[];
13
- }
14
-
15
- const PageTypeFilter = ({ onChange, pageTypes, selectedPageTypeUid }: Props) => {
16
- return (
17
- <SingleSelect
18
- placeholder="Select page type"
19
- onChange={onChange}
20
- size="S"
21
- value={selectedPageTypeUid}
22
- disabled={(pageTypes?.length || 0) === 0}
23
- >
24
- <SingleSelectOption key={PAGE_TYPE_NO_FILTER} value={PAGE_TYPE_NO_FILTER}>
25
- All Pagetypes
26
- </SingleSelectOption>
27
- <SingleSelectOption key={PAGE_TYPE_PAGE} value={PAGE_TYPE_PAGE}>
28
- Page
29
- </SingleSelectOption>
30
- {pageTypes?.map((pageType) => (
31
- <SingleSelectOption key={pageType.uid} value={pageType.uid}>
32
- {pageType.title || pageType.uid}
33
- </SingleSelectOption>
34
- ))}
35
- </SingleSelect>
36
- );
37
- };
38
-
39
- export default PageTypeFilter;
@@ -1,28 +0,0 @@
1
- import React from 'react';
2
-
3
- import { SingleSelectOption, SingleSelect } from '@strapi/design-system';
4
- import { Platform } from '../../../api/platform';
5
- import { PLATFORM_NO_FILTER } from '../../../constants';
6
-
7
- interface Props {
8
- onChange?: (platformId: string) => void;
9
- selectedPlatform?: string;
10
- platforms?: Platform[];
11
- }
12
-
13
- const PlatformFilter = ({ onChange, platforms, selectedPlatform }: Props) => {
14
- return (
15
- <SingleSelect placeholder="Select platform" onChange={onChange} size="S" value={selectedPlatform}>
16
- <SingleSelectOption key={PLATFORM_NO_FILTER} value={PLATFORM_NO_FILTER}>
17
- All Platforms
18
- </SingleSelectOption>
19
- {platforms?.map((platform: Platform) => (
20
- <SingleSelectOption key={platform?.id} value={platform?.title}>
21
- {platform?.title}
22
- </SingleSelectOption>
23
- ))}
24
- </SingleSelect>
25
- );
26
- };
27
-
28
- export default PlatformFilter;
@@ -1,180 +0,0 @@
1
- import React, { useMemo } from 'react';
2
- import set from 'lodash/set';
3
-
4
- import { Stack } from '@strapi/design-system';
5
- import { useQueryParams } from '@strapi/helper-plugin';
6
-
7
- import PageTypeFilter from './PageTypeFilter';
8
- import { PAGE_TYPE_PAGE, PLATFORM } from '../../../../shared/utils/constants';
9
- import PlatformFilter from './PlatformFilter';
10
- import { Platform, useGetPlatforms } from '../../api/platform';
11
- import { PAGE_TYPE, PAGE_TYPE_NO_FILTER, PLATFORM_NO_FILTER } from '../../constants';
12
- import { PageType } from '../../api/page-type';
13
-
14
- interface PageFiltersProps {
15
- hidePageType?: boolean;
16
- }
17
-
18
- const PageFilters = ({ hidePageType }: PageFiltersProps) => {
19
- const [{ query }, setQuery] = useQueryParams() as any;
20
-
21
- const { data: platforms } = useGetPlatforms({});
22
-
23
- const selectedPlatformTitle = useMemo(() => getPlatformFromQuery(query, platforms), [query, platforms]);
24
- const selectedPageTypeUid = useMemo(
25
- () => getPageTypeFromQuery(query, getPageTypesFromPlatformTitle(selectedPlatformTitle, platforms)),
26
- [query, platforms, selectedPlatformTitle]
27
- );
28
-
29
- const handleFilterChange = (
30
- filters: Record<string, any>,
31
- filterCategory: string,
32
- filterCategoryToRemove?: string
33
- ) => {
34
- const currentFilters = query.filters?.$and ? query.filters : { ...query.filters, $and: [] };
35
- const filterIndex = currentFilters.$and.findIndex(
36
- (x?: Record<string, any>) => Object.keys(x || {})?.[0] === filterCategory
37
- );
38
-
39
- if (filterIndex > -1) {
40
- set(currentFilters, `$and[${filterIndex}]`, filters); // If the filter in this category already exists, replace it
41
- } else {
42
- currentFilters.$and.push(filters);
43
- }
44
-
45
- const removedFilters = removeFiltersFromQuery({ filters: currentFilters }, filterCategoryToRemove);
46
-
47
- setQuery({
48
- page: 1,
49
- filters: removedFilters
50
- });
51
- };
52
-
53
- const removeFilters = (filterCategory: string) => {
54
- const filters = removeFiltersFromQuery(query, filterCategory);
55
-
56
- setQuery({
57
- page: 1,
58
- filters
59
- });
60
- };
61
-
62
- const handlePlatformSelect = (platformId: string) => {
63
- if (platformId === PLATFORM_NO_FILTER) {
64
- removeFilters(PLATFORM);
65
- return;
66
- }
67
-
68
- handleFilterChange(getPlatformQueryFilter(platformId), PLATFORM, PAGE_TYPE);
69
- };
70
-
71
- const handlePageTypeSelect = (pageType: string) => {
72
- if (pageType === PAGE_TYPE_NO_FILTER) {
73
- removeFilters(PAGE_TYPE);
74
- return;
75
- }
76
-
77
- if (pageType === PAGE_TYPE_PAGE) {
78
- handleFilterChange(nullPageTypeQueryFilter, PAGE_TYPE);
79
- return;
80
- }
81
-
82
- handleFilterChange(getPageTypeQueryFilter(pageType), PAGE_TYPE);
83
- };
84
-
85
- return (
86
- <Stack horizontal spacing={2}>
87
- <PlatformFilter onChange={handlePlatformSelect} platforms={platforms} selectedPlatform={selectedPlatformTitle} />
88
- {!hidePageType && (
89
- <PageTypeFilter
90
- onChange={handlePageTypeSelect}
91
- pageTypes={getPageTypesFromPlatformTitle(selectedPlatformTitle, platforms)}
92
- selectedPageTypeUid={selectedPageTypeUid}
93
- />
94
- )}
95
- </Stack>
96
- );
97
- };
98
-
99
- export default PageFilters;
100
-
101
- const getPageTypesFromPlatformTitle = (selectedPlatformTitle?: string, platforms?: Platform[]) =>
102
- platforms?.find((p) => p.title === selectedPlatformTitle)?.pageTypes;
103
-
104
- const getPageTypeFromQuery = (query: Record<string, any>, pageTypes?: PageType[]) => {
105
- const pageTypeFromQuery = query?.filters?.$and?.find(
106
- (x?: Record<string, any>) => Object.keys(x || {})?.[0] === PAGE_TYPE
107
- )?.pageType;
108
-
109
- if (pageTypeFromQuery?.uid?.$null === 'true') {
110
- return PAGE_TYPE_PAGE;
111
- }
112
-
113
- if (pageTypeFromQuery?.uid?.$eq) {
114
- const matchingPageType = pageTypes?.find((pageType: any) => pageType?.uid === pageTypeFromQuery.uid.$eq);
115
-
116
- if (matchingPageType) {
117
- return matchingPageType.uid;
118
- }
119
- }
120
-
121
- return PAGE_TYPE_NO_FILTER;
122
- };
123
-
124
- const getPlatformFromQuery = (query: Record<string, any>, platforms?: Platform[]): string | undefined => {
125
- const platformFromQuery = query?.filters?.$and?.find(
126
- (x?: Record<string, any>) => Object.keys(x || {})?.[0] === PLATFORM
127
- )?.platform;
128
-
129
- if (platformFromQuery?.title?.$null === 'true') {
130
- return PLATFORM_NO_FILTER;
131
- }
132
-
133
- if (platformFromQuery?.title?.$eq) {
134
- const matchingPageType = platforms?.find((platforms: Platform) => platforms?.title === platformFromQuery.title.$eq);
135
-
136
- if (matchingPageType) {
137
- return matchingPageType.title;
138
- }
139
- }
140
-
141
- return PLATFORM_NO_FILTER;
142
- };
143
-
144
- const getPlatformQueryFilter = (platform: string) => ({
145
- platform: {
146
- title: {
147
- $eq: platform
148
- }
149
- }
150
- });
151
-
152
- const getPageTypeQueryFilter = (pageType: string) => ({
153
- pageType: {
154
- uid: {
155
- $eq: pageType
156
- }
157
- }
158
- });
159
-
160
- const nullPageTypeQueryFilter = {
161
- pageType: {
162
- uid: {
163
- $null: true
164
- }
165
- }
166
- };
167
-
168
- const removeFiltersFromQuery = (query: Record<string, any>, filterCategory?: string) => {
169
- const newAndFilters = query.filters?.$and?.filter(
170
- (x?: Record<string, any>) => Object.keys(x || {})?.[0] !== filterCategory
171
- );
172
-
173
- const filters = { ...query.filters, $and: newAndFilters };
174
-
175
- if (newAndFilters && newAndFilters.length === 0) {
176
- delete filters.$and;
177
- }
178
-
179
- return filters;
180
- };