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