|
1 | | -import {Widget, WidgetLayoutItem} from "@/types"; |
| 1 | +import { Widget, WidgetLayoutItem } from "@/types"; |
2 | 2 | import axios from "axios"; |
3 | 3 |
|
4 | 4 | export interface WidgetsLayouts { |
@@ -49,35 +49,81 @@ export async function initializeWidgets(): Promise<{ |
49 | 49 | xs: [], |
50 | 50 | xxs: [] |
51 | 51 | }; |
| 52 | + const defaultWidgetIds = widgetsDBResponse.data?.widgetsDB?.defaultWidgets as string[] ?? []; |
52 | 53 |
|
53 | 54 | const widgetsResponse = await axios.get(`${window.routePrefix}/widgets-get-all`); |
54 | 55 | const allWidgets = widgetsResponse.data.widgets as Widget[]; |
55 | 56 |
|
56 | | - const dbWidgetIds = widgetsDB.map(w => w.id).sort(); |
57 | | - const storedWidgetIds = storedWidgets.map(w => w.id).sort(); |
58 | | - |
59 | | - // Check if the widget IDs in the database and localStorage match |
60 | | - const idsMatch = |
61 | | - dbWidgetIds.length === storedWidgetIds.length && |
62 | | - dbWidgetIds.every((id, index) => id === storedWidgetIds[index]); |
| 57 | + // Check if user has saved widgets in database (no defaultWidgetIds means user has saved widgets) |
| 58 | + const hasSavedWidgets = widgetsDB.length > 0 && widgetsDB.some(w => w.added === true) && defaultWidgetIds.length === 0; |
63 | 59 |
|
64 | 60 | let finalWidgetsDB = widgetsDB; |
65 | 61 | let finalLayoutDB = layoutDB; |
66 | 62 |
|
67 | | - if (idsMatch && storedWidgets.length > 0) { |
68 | | - finalWidgetsDB = storedWidgets; |
69 | | - finalLayoutDB = storedLayout; |
70 | | - } else { |
| 63 | + // If using default widgets, generate layout on frontend |
| 64 | + if (!hasSavedWidgets && defaultWidgetIds.length > 0) { |
| 65 | + const addedWidgets: Widget[] = []; |
| 66 | + let x = 0, y = 0; |
| 67 | + |
| 68 | + // Mark default widgets as added |
| 69 | + allWidgets.forEach(widget => { |
| 70 | + if (defaultWidgetIds.includes(widget.id.split("__")[0])) { |
| 71 | + widget.added = true; |
| 72 | + addedWidgets.push(widget); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + // Generate layout for all breakpoints |
| 77 | + const generatedLayout: WidgetsLayouts = { lg: [], md: [], sm: [], xs: [], xxs: [] }; |
| 78 | + |
| 79 | + addedWidgets.forEach(widget => { |
| 80 | + const w = widget.size?.w || 1; |
| 81 | + const h = widget.size?.h || 1; |
| 82 | + const layoutItem: WidgetLayoutItem = { x, y, w, h, i: widget.id, id: widget.id }; |
| 83 | + |
| 84 | + generatedLayout.lg.push(layoutItem); |
| 85 | + generatedLayout.md.push(layoutItem); |
| 86 | + generatedLayout.sm.push({ ...layoutItem, w: Math.min(w, 4) }); |
| 87 | + generatedLayout.xs.push({ ...layoutItem, w: Math.min(w, 3) }); |
| 88 | + generatedLayout.xxs.push({ ...layoutItem, w: 2 }); |
| 89 | + |
| 90 | + x += w; |
| 91 | + if (x >= 12) { |
| 92 | + x = 0; |
| 93 | + y += h; |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + finalWidgetsDB = addedWidgets; |
| 98 | + finalLayoutDB = generatedLayout; |
| 99 | + |
| 100 | + // Clear localStorage when using defaults |
| 101 | + localStorage.removeItem('widgetsData'); |
| 102 | + } else if (hasSavedWidgets && storedData) { |
| 103 | + // Only use localStorage if user has saved widgets in database |
| 104 | + const dbWidgetIds = widgetsDB.map(w => w.id).sort(); |
| 105 | + const storedWidgetIds = storedWidgets.map(w => w.id).sort(); |
| 106 | + |
| 107 | + const idsMatch = |
| 108 | + dbWidgetIds.length === storedWidgetIds.length && |
| 109 | + dbWidgetIds.every((id, index) => id === storedWidgetIds[index]); |
| 110 | + |
| 111 | + if (idsMatch && storedWidgets.length > 0) { |
| 112 | + finalWidgetsDB = storedWidgets; |
| 113 | + finalLayoutDB = storedLayout; |
| 114 | + } |
| 115 | + |
| 116 | + // Update localStorage with current data |
71 | 117 | const dataToStore = { |
72 | | - widgets: widgetsDB, |
73 | | - layout: layoutDB |
| 118 | + widgets: finalWidgetsDB, |
| 119 | + layout: finalLayoutDB |
74 | 120 | }; |
75 | 121 | localStorage.setItem('widgetsData', JSON.stringify(dataToStore)); |
76 | 122 | } |
77 | 123 |
|
78 | 124 | const initWidgets = allWidgets.map(widget => { |
79 | 125 | const findItem = finalWidgetsDB.find((e: any) => e.id === widget.id); |
80 | | - return findItem && findItem.added ? {...widget, added: true} : widget; |
| 126 | + return findItem && findItem.added ? { ...widget, added: true } : widget; |
81 | 127 | }); |
82 | 128 |
|
83 | 129 | return { |
|
0 commit comments