@creopse/react 0.0.8 → 0.0.9
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/dist/hooks/index.cjs +438 -0
- package/dist/hooks/index.mjs +439 -0
- package/dist/index-CJtxQKA-.cjs +7788 -0
- package/dist/index-DAEkiy4W.js +7790 -0
- package/dist/index.cjs +345 -4471
- package/dist/index.mjs +335 -4461
- package/package.json +19 -3
- package/types/components/widgets/Image/Image.d.ts +14 -0
- package/types/components/widgets/Image/index.d.ts +1 -0
- package/types/core/contexts.d.ts +7 -0
- package/types/core/props-manager.d.ts +15 -0
- package/types/hooks/api.d.ts +23 -0
- package/types/hooks/config.d.ts +21 -0
- package/types/hooks/content.d.ts +54 -0
- package/types/hooks/helper.d.ts +30 -0
- package/types/hooks/index.d.ts +6 -0
- package/types/hooks/newsletter.d.ts +29 -0
- package/types/hooks/props.d.ts +9 -0
- package/types/index.d.ts +12 -1
- package/types/types/api.d.ts +15 -0
- package/types/types/plugin.d.ts +14 -0
- package/dist/index.js +0 -12398
- /package/types/components/{core/RootContainer → RootContainer}/RootContainer.d.ts +0 -0
- /package/types/components/{core/RootContainer → RootContainer}/index.d.ts +0 -0
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const index = require("../index-CJtxQKA-.cjs");
|
|
4
|
+
const React = require("react");
|
|
5
|
+
const react = require("@inertiajs/react");
|
|
6
|
+
const useProps = () => {
|
|
7
|
+
const manager = React.useContext(index.PropsContext);
|
|
8
|
+
if (!manager) {
|
|
9
|
+
throw new Error("[@creopse/react] CreopseProvider not found in tree");
|
|
10
|
+
}
|
|
11
|
+
const [props, setProps] = React.useState(manager.getState().props);
|
|
12
|
+
React.useEffect(() => {
|
|
13
|
+
const unsubscribe = manager.subscribe((newProps) => {
|
|
14
|
+
setProps(newProps);
|
|
15
|
+
});
|
|
16
|
+
return unsubscribe;
|
|
17
|
+
}, [manager]);
|
|
18
|
+
return props;
|
|
19
|
+
};
|
|
20
|
+
const useContent = () => {
|
|
21
|
+
const { request } = index.useApi();
|
|
22
|
+
const { fileUrl } = index.useHelper();
|
|
23
|
+
const page = react.usePage();
|
|
24
|
+
const resolveSections = React.useContext(index.ResolveSectionsContext);
|
|
25
|
+
const getProps = React.useCallback(
|
|
26
|
+
() => page.props,
|
|
27
|
+
[page.props]
|
|
28
|
+
);
|
|
29
|
+
const pageData = React.useMemo(
|
|
30
|
+
() => page.props.pageData,
|
|
31
|
+
[page.props.pageData]
|
|
32
|
+
);
|
|
33
|
+
const newsArticle = React.useMemo(
|
|
34
|
+
() => page.props.article,
|
|
35
|
+
[page.props.article]
|
|
36
|
+
);
|
|
37
|
+
const newsCategory = React.useMemo(
|
|
38
|
+
() => page.props.category,
|
|
39
|
+
[page.props.category]
|
|
40
|
+
);
|
|
41
|
+
const newsTag = React.useMemo(
|
|
42
|
+
() => page.props.tag,
|
|
43
|
+
[page.props.tag]
|
|
44
|
+
);
|
|
45
|
+
const contentModelItem = React.useMemo(
|
|
46
|
+
() => page.props.contentModelItem,
|
|
47
|
+
[page.props.contentModelItem]
|
|
48
|
+
);
|
|
49
|
+
const getSectionData = React.useCallback(
|
|
50
|
+
(key) => {
|
|
51
|
+
if (!key) return null;
|
|
52
|
+
const keyParts = key.split("__");
|
|
53
|
+
const slug = keyParts.length ? keyParts[0] : "";
|
|
54
|
+
const linkId = keyParts.length > 1 ? keyParts[1] : "";
|
|
55
|
+
return page.props.pageData?.sections?.find(
|
|
56
|
+
(section) => section.slug === slug && section.pivot?.linkId === linkId
|
|
57
|
+
)?.pivot?.data || null;
|
|
58
|
+
},
|
|
59
|
+
[page.props.pageData?.sections]
|
|
60
|
+
);
|
|
61
|
+
const getSectionRootData = React.useCallback(
|
|
62
|
+
(key) => getSectionData(key)?.index,
|
|
63
|
+
[getSectionData]
|
|
64
|
+
);
|
|
65
|
+
const getSectionSettings = React.useCallback(
|
|
66
|
+
(key) => {
|
|
67
|
+
if (!key) return null;
|
|
68
|
+
const keyParts = key.split("__");
|
|
69
|
+
const slug = keyParts.length ? keyParts[0] : "";
|
|
70
|
+
const linkId = keyParts.length > 1 ? keyParts[1] : "";
|
|
71
|
+
return page.props.pageData?.sections?.find(
|
|
72
|
+
(section) => section.slug === slug && section.pivot?.linkId === linkId
|
|
73
|
+
)?.pivot?.settings || null;
|
|
74
|
+
},
|
|
75
|
+
[page.props.pageData?.sections]
|
|
76
|
+
);
|
|
77
|
+
const getSectionSettingsGroup = React.useCallback(
|
|
78
|
+
(key, group) => {
|
|
79
|
+
const settings = getSectionSettings(key);
|
|
80
|
+
return settings?.[group];
|
|
81
|
+
},
|
|
82
|
+
[getSectionSettings]
|
|
83
|
+
);
|
|
84
|
+
const getSectionSetting = React.useCallback(
|
|
85
|
+
(key, group, name) => {
|
|
86
|
+
const settings = getSectionSettingsGroup(key, group);
|
|
87
|
+
return settings?.[name];
|
|
88
|
+
},
|
|
89
|
+
[getSectionSettingsGroup]
|
|
90
|
+
);
|
|
91
|
+
const getSectionsInOrder = React.useCallback(() => {
|
|
92
|
+
const sections = pageData?.sections;
|
|
93
|
+
const sectionsOrder = pageData?.sectionsOrder;
|
|
94
|
+
const sectionsOrdered = [];
|
|
95
|
+
if (Array.isArray(sections) && Array.isArray(sectionsOrder)) {
|
|
96
|
+
for (const section of sectionsOrder) {
|
|
97
|
+
if (section) {
|
|
98
|
+
const item = sections.find(
|
|
99
|
+
(value) => `${value.slug}__${value.pivot?.linkId}` === section
|
|
100
|
+
);
|
|
101
|
+
if (item) {
|
|
102
|
+
sectionsOrdered.push(item);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return sectionsOrdered.length ? sectionsOrdered : sections || [];
|
|
108
|
+
}, [pageData?.sections, pageData?.sectionsOrder]);
|
|
109
|
+
const getFinalPageSections = React.useCallback(() => {
|
|
110
|
+
const sections = getSectionsInOrder();
|
|
111
|
+
const sectionsDisabled = pageData?.sectionsDisabled;
|
|
112
|
+
return sections.filter((section) => {
|
|
113
|
+
return !sectionsDisabled?.includes(
|
|
114
|
+
`${section.slug}__${section.pivot?.linkId}`
|
|
115
|
+
);
|
|
116
|
+
});
|
|
117
|
+
}, [getSectionsInOrder, pageData?.sectionsDisabled]);
|
|
118
|
+
const getAnySectionData = React.useCallback(
|
|
119
|
+
async (sectionSlug, pageSlug, linkId = "default") => {
|
|
120
|
+
const task = await request({
|
|
121
|
+
url: `section-data/${sectionSlug}/source/${pageSlug}/link/${linkId}`
|
|
122
|
+
});
|
|
123
|
+
if (task.success && task.result) {
|
|
124
|
+
return task.result.data;
|
|
125
|
+
}
|
|
126
|
+
return null;
|
|
127
|
+
},
|
|
128
|
+
[request]
|
|
129
|
+
);
|
|
130
|
+
const getComponents = React.useCallback(() => {
|
|
131
|
+
const components = {};
|
|
132
|
+
if (!resolveSections) {
|
|
133
|
+
throw new Error("[@creopse/react] resolveSections is required");
|
|
134
|
+
}
|
|
135
|
+
const files = resolveSections();
|
|
136
|
+
for (const [key, value] of Object.entries(files)) {
|
|
137
|
+
const componentName = key.replace(/^\.\/(.*)\.\w+$/, "$1");
|
|
138
|
+
const parts = componentName.split("/");
|
|
139
|
+
const name = parts[parts.length - 1]?.split(".")[0];
|
|
140
|
+
components[name] = value.default;
|
|
141
|
+
}
|
|
142
|
+
return components;
|
|
143
|
+
}, [resolveSections]);
|
|
144
|
+
const getContentModel = React.useCallback(
|
|
145
|
+
(name) => {
|
|
146
|
+
return page.props?.contentModels?.find(
|
|
147
|
+
(contentModel) => contentModel.name === name
|
|
148
|
+
);
|
|
149
|
+
},
|
|
150
|
+
[page.props?.contentModels]
|
|
151
|
+
);
|
|
152
|
+
const formatContentModelItemData = React.useCallback(
|
|
153
|
+
(item) => {
|
|
154
|
+
const { index: _, ...rest } = item.contentModelData;
|
|
155
|
+
return {
|
|
156
|
+
...item,
|
|
157
|
+
data: {
|
|
158
|
+
...item.contentModelData?.index,
|
|
159
|
+
...rest
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
},
|
|
163
|
+
[]
|
|
164
|
+
);
|
|
165
|
+
const getContentModelItems = React.useCallback(
|
|
166
|
+
async (name, filterByIsActive = true) => {
|
|
167
|
+
const task = await request({
|
|
168
|
+
url: `content-model/items?contentModelName=${name}${filterByIsActive ? "&isActive=true" : ""}`
|
|
169
|
+
});
|
|
170
|
+
if (task.success && task.result) {
|
|
171
|
+
return (task.result.data || []).map(
|
|
172
|
+
(item) => formatContentModelItemData(item)
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
return [];
|
|
176
|
+
},
|
|
177
|
+
[request, formatContentModelItemData]
|
|
178
|
+
);
|
|
179
|
+
const getPaginatedContentModelItems = React.useCallback(
|
|
180
|
+
async (name, pageSize, filterByIsActive = true) => {
|
|
181
|
+
const task = await request({
|
|
182
|
+
url: `content-model/items?pageSize=${pageSize}&contentModelName=${name}${filterByIsActive ? "&isActive=true" : ""}`
|
|
183
|
+
});
|
|
184
|
+
if (task.success && task.result) {
|
|
185
|
+
const items = (task.result.data?.items || []).map(
|
|
186
|
+
(item) => formatContentModelItemData(item)
|
|
187
|
+
);
|
|
188
|
+
const total = task.result.data?.meta?.total || 0;
|
|
189
|
+
const currentPage = task.result.data?.meta?.currentPage || 1;
|
|
190
|
+
return {
|
|
191
|
+
items,
|
|
192
|
+
total,
|
|
193
|
+
currentPage
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
return {
|
|
197
|
+
items: [],
|
|
198
|
+
total: 0,
|
|
199
|
+
currentPage: 1
|
|
200
|
+
};
|
|
201
|
+
},
|
|
202
|
+
[request, formatContentModelItemData]
|
|
203
|
+
);
|
|
204
|
+
const getMenu = React.useCallback(
|
|
205
|
+
(name, filterByIsActive = true) => {
|
|
206
|
+
const menu = page.props?.menus?.find((menu2) => menu2.name === name);
|
|
207
|
+
if (menu) {
|
|
208
|
+
menu.items = index.cloneDeep(
|
|
209
|
+
menu.items?.filter((item) => !filterByIsActive || item.isActive)?.sort((a, b) => a.position - b.position)
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
return menu;
|
|
213
|
+
},
|
|
214
|
+
[page.props?.menus]
|
|
215
|
+
);
|
|
216
|
+
const getMenuByLocation = React.useCallback(
|
|
217
|
+
(name, filterByIsActive = true) => {
|
|
218
|
+
const menu = page.props?.menus?.find(
|
|
219
|
+
(menu2) => menu2.location?.name === name
|
|
220
|
+
);
|
|
221
|
+
if (menu) {
|
|
222
|
+
menu.items = index.cloneDeep(
|
|
223
|
+
menu.items?.filter((item) => !filterByIsActive || item.isActive)?.sort((a, b) => a.position - b.position)
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
return menu;
|
|
227
|
+
},
|
|
228
|
+
[page.props?.menus]
|
|
229
|
+
);
|
|
230
|
+
const getMenuItems = React.useCallback(
|
|
231
|
+
(name, filterByIsVisible = true) => {
|
|
232
|
+
return getMenu(name)?.items?.filter((item) => !filterByIsVisible || item.isVisible)?.sort((a, b) => a.position - b.position);
|
|
233
|
+
},
|
|
234
|
+
[getMenu]
|
|
235
|
+
);
|
|
236
|
+
const getMenuItemById = React.useCallback(
|
|
237
|
+
(id) => {
|
|
238
|
+
const menuItems = [];
|
|
239
|
+
const menus = page.props?.menus || [];
|
|
240
|
+
menus.forEach((menu) => {
|
|
241
|
+
if (Array.isArray(menu.items)) menuItems.push(...menu.items);
|
|
242
|
+
});
|
|
243
|
+
return menuItems.find((item) => item.id === id);
|
|
244
|
+
},
|
|
245
|
+
[page.props?.menus]
|
|
246
|
+
);
|
|
247
|
+
const getMenuItemsByLocation = React.useCallback(
|
|
248
|
+
(name, filterByIsVisible = true) => {
|
|
249
|
+
return getMenuByLocation(name)?.items?.filter((item) => !filterByIsVisible || item.isVisible)?.sort((a, b) => a.position - b.position);
|
|
250
|
+
},
|
|
251
|
+
[getMenuByLocation]
|
|
252
|
+
);
|
|
253
|
+
const getMenuGroups = React.useCallback(
|
|
254
|
+
(name, byLocation = false, filterByIsVisible = true) => {
|
|
255
|
+
const groups = [];
|
|
256
|
+
const items = byLocation ? getMenuItemsByLocation(name, filterByIsVisible) : getMenuItems(name, filterByIsVisible);
|
|
257
|
+
if (items) {
|
|
258
|
+
for (const item of items) {
|
|
259
|
+
const groupAlreadyExists = groups.find(
|
|
260
|
+
(group) => group.id === item.menuItemGroupId
|
|
261
|
+
);
|
|
262
|
+
if (!groupAlreadyExists) {
|
|
263
|
+
const group = page.props?.menuItemGroups?.find(
|
|
264
|
+
(group2) => group2.id === item.menuItemGroupId
|
|
265
|
+
);
|
|
266
|
+
if (group) groups.push(group);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return groups;
|
|
271
|
+
},
|
|
272
|
+
[getMenuItems, getMenuItemsByLocation, page.props?.menuItemGroups]
|
|
273
|
+
);
|
|
274
|
+
const getMenuItemsByGroup = React.useCallback(
|
|
275
|
+
(name, groupId, byLocation = false, filterByIsVisible = true) => {
|
|
276
|
+
const items = byLocation ? getMenuItemsByLocation(name, filterByIsVisible) : getMenuItems(name, filterByIsVisible);
|
|
277
|
+
return items?.filter((item) => item.menuItemGroupId === groupId);
|
|
278
|
+
},
|
|
279
|
+
[getMenuItems, getMenuItemsByLocation]
|
|
280
|
+
);
|
|
281
|
+
const getMenuGroupedItems = React.useCallback(
|
|
282
|
+
(name, byLocation = false, filterByIsVisible = true) => {
|
|
283
|
+
const groups = getMenuGroups(name, byLocation);
|
|
284
|
+
return groups.map((group) => ({
|
|
285
|
+
group,
|
|
286
|
+
items: getMenuItemsByGroup(
|
|
287
|
+
name,
|
|
288
|
+
group.id || 0,
|
|
289
|
+
byLocation,
|
|
290
|
+
filterByIsVisible
|
|
291
|
+
)
|
|
292
|
+
}));
|
|
293
|
+
},
|
|
294
|
+
[getMenuGroups, getMenuItemsByGroup]
|
|
295
|
+
);
|
|
296
|
+
const getMenuUngroupedItems = React.useCallback(
|
|
297
|
+
(name, byLocation = false, filterByIsVisible = true) => {
|
|
298
|
+
const items = byLocation ? getMenuItemsByLocation(name, filterByIsVisible) : getMenuItems(name, filterByIsVisible);
|
|
299
|
+
return items?.filter((item) => !item.menuItemGroupId);
|
|
300
|
+
},
|
|
301
|
+
[getMenuItems, getMenuItemsByLocation]
|
|
302
|
+
);
|
|
303
|
+
const getAppInformationValue = React.useCallback(
|
|
304
|
+
(key, type = "string") => {
|
|
305
|
+
const appInformation = page.props.appInformation;
|
|
306
|
+
const setting = appInformation.find((s) => s.key === key);
|
|
307
|
+
let value = "";
|
|
308
|
+
switch (type) {
|
|
309
|
+
case "number":
|
|
310
|
+
value = setting && parseInt(setting.value) && !isNaN(parseInt(setting.value)) ? parseInt(setting.value) : 0;
|
|
311
|
+
break;
|
|
312
|
+
case "boolean":
|
|
313
|
+
value = setting && !isNaN(parseInt(setting.value)) && parseInt(setting.value) > 0;
|
|
314
|
+
break;
|
|
315
|
+
case "object":
|
|
316
|
+
value = setting && setting.value ? JSON.parse(setting.value) : {};
|
|
317
|
+
break;
|
|
318
|
+
case "array":
|
|
319
|
+
value = setting && setting.value ? JSON.parse(setting.value) : [];
|
|
320
|
+
break;
|
|
321
|
+
default:
|
|
322
|
+
value = setting && setting.value ? setting.value : "";
|
|
323
|
+
break;
|
|
324
|
+
}
|
|
325
|
+
return value;
|
|
326
|
+
},
|
|
327
|
+
[page.props.appInformation]
|
|
328
|
+
);
|
|
329
|
+
const appPrimaryColor = React.useMemo(() => {
|
|
330
|
+
const primaryColor = getAppInformationValue(
|
|
331
|
+
"primaryColor"
|
|
332
|
+
);
|
|
333
|
+
return primaryColor || "#005B97";
|
|
334
|
+
}, [getAppInformationValue]);
|
|
335
|
+
const appSecondaryColor = React.useMemo(() => {
|
|
336
|
+
const secondaryColor = getAppInformationValue(
|
|
337
|
+
"secondaryColor"
|
|
338
|
+
);
|
|
339
|
+
return secondaryColor || "#1E9CD7";
|
|
340
|
+
}, [getAppInformationValue]);
|
|
341
|
+
const appAccentColor = React.useMemo(() => {
|
|
342
|
+
const accentColor = getAppInformationValue("accentColor");
|
|
343
|
+
return accentColor || "#FF6501";
|
|
344
|
+
}, [getAppInformationValue]);
|
|
345
|
+
const icon = React.useMemo(() => {
|
|
346
|
+
const icon2 = getAppInformationValue("icon");
|
|
347
|
+
return fileUrl(icon2) || "";
|
|
348
|
+
}, [getAppInformationValue, fileUrl]);
|
|
349
|
+
const logo = React.useMemo(() => {
|
|
350
|
+
const logo2 = getAppInformationValue("logo");
|
|
351
|
+
return fileUrl(logo2) || "";
|
|
352
|
+
}, [getAppInformationValue, fileUrl]);
|
|
353
|
+
return {
|
|
354
|
+
logo,
|
|
355
|
+
icon,
|
|
356
|
+
page,
|
|
357
|
+
pageData,
|
|
358
|
+
newsArticle,
|
|
359
|
+
newsCategory,
|
|
360
|
+
newsTag,
|
|
361
|
+
contentModelItem,
|
|
362
|
+
getProps,
|
|
363
|
+
getMenu,
|
|
364
|
+
getMenuByLocation,
|
|
365
|
+
getMenuItems,
|
|
366
|
+
getMenuItemById,
|
|
367
|
+
getMenuItemsByLocation,
|
|
368
|
+
getMenuGroups,
|
|
369
|
+
getMenuItemsByGroup,
|
|
370
|
+
getMenuGroupedItems,
|
|
371
|
+
getMenuUngroupedItems,
|
|
372
|
+
getSectionData,
|
|
373
|
+
getSectionSettings,
|
|
374
|
+
getSectionSettingsGroup,
|
|
375
|
+
getSectionSetting,
|
|
376
|
+
getAnySectionData,
|
|
377
|
+
getSectionsInOrder,
|
|
378
|
+
getFinalPageSections,
|
|
379
|
+
getSectionRootData,
|
|
380
|
+
getComponents,
|
|
381
|
+
getContentModel,
|
|
382
|
+
getContentModelItems,
|
|
383
|
+
getPaginatedContentModelItems,
|
|
384
|
+
getAppInformationValue,
|
|
385
|
+
appAccentColor,
|
|
386
|
+
appPrimaryColor,
|
|
387
|
+
appSecondaryColor
|
|
388
|
+
};
|
|
389
|
+
};
|
|
390
|
+
const useNewsletter = () => {
|
|
391
|
+
const { request } = index.useApi();
|
|
392
|
+
const [isLoading, setIsLoading] = React.useState(false);
|
|
393
|
+
const subscribe = React.useCallback(
|
|
394
|
+
async (type, contact, successCallback, errorCallback) => {
|
|
395
|
+
setIsLoading(true);
|
|
396
|
+
const task = await request({
|
|
397
|
+
url: `newsletter/${type}s`,
|
|
398
|
+
method: "post",
|
|
399
|
+
data: type === "email" ? { email: contact } : { phone: contact }
|
|
400
|
+
});
|
|
401
|
+
if (task.failure && task.error) {
|
|
402
|
+
if (errorCallback) errorCallback(task.error?.response?.data);
|
|
403
|
+
} else {
|
|
404
|
+
if (successCallback) successCallback();
|
|
405
|
+
}
|
|
406
|
+
setIsLoading(false);
|
|
407
|
+
},
|
|
408
|
+
[request]
|
|
409
|
+
);
|
|
410
|
+
const subscribeEmail = React.useCallback(
|
|
411
|
+
async (email, successCallback, errorCallback) => {
|
|
412
|
+
await subscribe("email", email, successCallback, errorCallback);
|
|
413
|
+
},
|
|
414
|
+
[subscribe]
|
|
415
|
+
);
|
|
416
|
+
const subscribePhone = React.useCallback(
|
|
417
|
+
async (phone, successCallback, errorCallback) => {
|
|
418
|
+
await subscribe(
|
|
419
|
+
"phone",
|
|
420
|
+
phone.replace(/\s+/g, ""),
|
|
421
|
+
successCallback,
|
|
422
|
+
errorCallback
|
|
423
|
+
);
|
|
424
|
+
},
|
|
425
|
+
[subscribe]
|
|
426
|
+
);
|
|
427
|
+
return {
|
|
428
|
+
subscribePhone,
|
|
429
|
+
subscribeEmail,
|
|
430
|
+
isLoading
|
|
431
|
+
};
|
|
432
|
+
};
|
|
433
|
+
exports.useApi = index.useApi;
|
|
434
|
+
exports.useConfig = index.useConfig;
|
|
435
|
+
exports.useHelper = index.useHelper;
|
|
436
|
+
exports.useContent = useContent;
|
|
437
|
+
exports.useNewsletter = useNewsletter;
|
|
438
|
+
exports.useProps = useProps;
|