@@ -24,8 +24,8 @@ import { array } from 'prop-types'
24
24
import Shortcuts from '../Shortcuts'
25
25
import Integrations from '../Integrations'
26
26
import SRE from '../KPI_Cards'
27
- import { APIToWidget , WidgetAPI , WidgetToAPI } from '../../../utilities/widget'
28
-
27
+ import { APIToWidget , Widget , WidgetAPI , WidgetToAPI } from '../../../utilities/widget'
28
+ import { v4 as uuid } from 'uuid'
29
29
const COMPONENT_MAPPING = {
30
30
table : TableWidget ,
31
31
chart : ChartWidget ,
@@ -121,7 +121,9 @@ export default function WidgetLayout({
121
121
id : layout ?. id ,
122
122
is_default : layout . is_default ,
123
123
user_id : me ?. username ,
124
- layout_config : layout_config ,
124
+ widget_ids : layout_config ?. map ( ( widget : any ) => {
125
+ return widget ?. id
126
+ } ) ,
125
127
name : layout . name ,
126
128
description : layout . description ,
127
129
is_private : layout . is_private ,
@@ -134,43 +136,57 @@ export default function WidgetLayout({
134
136
console . log ( err )
135
137
} )
136
138
}
137
- const GetDefaultLayout = ( ) => {
138
- setLayoutLoading ( true )
139
- let url = ''
140
- if ( window . location . origin === 'http://localhost:3000' ) {
141
- url = window . __RUNTIME_CONFIG__ . REACT_APP_BASE_URL
142
- } else {
143
- url = window . location . origin
144
- }
145
- // @ts -ignore
146
- const token = JSON . parse ( localStorage . getItem ( 'openg_auth' ) ) . token
139
+ const GetDefaultLayout = ( ) => {
140
+ const id = layout ?. id
141
+ axios
142
+ . get (
143
+ `https://raw.githubusercontent.com/opengovern/platform-configuration/refs/heads/main/default_layout.json`
144
+ )
145
+ . then ( ( res ) => {
146
+ SetDefaultLayoutWithDashbord ( res ?. data , me , id )
147
+ setLayout ( res ?. data )
147
148
148
- const config = {
149
- headers : {
150
- Authorization : `Bearer ${ token } ` ,
151
- } ,
152
- }
153
- const body = {
154
- user_id : me ?. username ,
155
- }
149
+ setLayoutLoading ( false )
150
+ } )
151
+ . catch ( ( err ) => {
152
+ setLayoutLoading ( false )
153
+ } )
154
+ }
155
+ const SetDefaultLayoutWithDashbord = ( layout : any , meResponse : any , id : string ) => {
156
+ let url = ''
157
+ if ( window . location . origin === 'http://localhost:3000' ) {
158
+ url = window . __RUNTIME_CONFIG__ . REACT_APP_BASE_URL
159
+ } else {
160
+ url = window . location . origin
161
+ }
162
+ // @ts -ignore
163
+ const token = JSON . parse ( localStorage . getItem ( 'openg_auth' ) ) . token
164
+
165
+ const config = {
166
+ headers : {
167
+ Authorization : `Bearer ${ token } ` ,
168
+ } ,
169
+ }
170
+ const body = {
171
+ user_id : meResponse ?. username ,
172
+ widgets : layout ?. widgets ?. map ( ( widget : Widget ) => {
173
+ return WidgetToAPI ( widget , meResponse ?. username , true , true )
174
+ } ) ,
175
+ name : layout ?. name ,
176
+ description : layout ?. description ,
177
+ is_default : true ,
178
+ is_private : true ,
179
+ id : id ,
180
+ }
181
+
182
+ axios
183
+ . post ( `${ url } /main/core/api/v4/layout/set/widgets` , body , config )
184
+ . then ( ( res ) => { } )
185
+ . catch ( ( err ) => {
186
+ console . log ( err )
187
+ } )
188
+ }
156
189
157
- axios
158
- . post ( `${ url } /main/core/api/v4/layout/get-default` , body , config )
159
- . then ( ( res ) => {
160
- const layout = res ?. data
161
- layout . widgets = layout ?. widgets ?. map ( ( widget : WidgetAPI ) => {
162
- return APIToWidget ( widget )
163
- } )
164
- setLayout ( res ?. data )
165
- setLayoutLoading ( false )
166
- } )
167
- . catch ( ( err ) => {
168
- console . log ( err )
169
- // check if error is 404
170
- GetDefaultLayout ( )
171
- setLayoutLoading ( false )
172
- } )
173
- }
174
190
const HandleRemoveItemByID = ( id : string ) => {
175
191
const newItems = items . filter ( ( item : any ) => item . id !== id )
176
192
setItems ( newItems )
@@ -323,7 +339,7 @@ export default function WidgetLayout({
323
339
return
324
340
}
325
341
const newItem = {
326
- id : ` ${ selectedAddItem } - ${ items ?. length } ` ,
342
+ id : uuid ( ) ,
327
343
data : {
328
344
componentId : selectedAddItem ,
329
345
props : widgetProps ,
@@ -351,7 +367,7 @@ export default function WidgetLayout({
351
367
}
352
368
if ( id == 'integration' ) {
353
369
const new_item = {
354
- id : 'integration' ,
370
+ id : uuid ( ) ,
355
371
data : {
356
372
componentId : 'integration' ,
357
373
title : 'Integrations' ,
@@ -366,7 +382,7 @@ export default function WidgetLayout({
366
382
}
367
383
if ( id == 'shortcut' ) {
368
384
const new_item = {
369
- id : 'shortcut' ,
385
+ id : uuid ( ) ,
370
386
data : {
371
387
componentId : 'shortcut' ,
372
388
title : 'Shortcuts' ,
@@ -381,7 +397,7 @@ export default function WidgetLayout({
381
397
}
382
398
if ( id == 'sre' ) {
383
399
const new_item = {
384
- id : 'sre' ,
400
+ id : uuid ( ) ,
385
401
data : {
386
402
componentId : 'sre' ,
387
403
title : 'SRE' ,
@@ -493,7 +509,7 @@ export default function WidgetLayout({
493
509
// description={}
494
510
actions = {
495
511
< div className = "flex flex-row gap-2" >
496
- { /* <ButtonDropdown
512
+ < ButtonDropdown
497
513
items = { [
498
514
{ id : 'add' , text : 'Add new dashboard' } ,
499
515
@@ -547,7 +563,7 @@ export default function WidgetLayout({
547
563
ariaLabel = "Board item settings"
548
564
>
549
565
Add Widget
550
- </ButtonDropdown> */ }
566
+ </ ButtonDropdown >
551
567
</ div >
552
568
}
553
569
>
0 commit comments