Skip to content

Commit 09357aa

Browse files
authored
Merge pull request #346 from opengovern/ui-changes
feat: update set deafult layout and dashbord save
2 parents 7eb24c4 + 926666e commit 09357aa

File tree

4 files changed

+78
-50
lines changed

4 files changed

+78
-50
lines changed

Diff for: services/webui/package-lock.json

+17-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: services/webui/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
"swagger-typescript-api": "^13.0.23",
7474
"swr": "^2.2.4",
7575
"typescript": "^4.9.5",
76+
"uuid": "^11.1.0",
7677
"web-vitals": "^2.1.4"
7778
},
7879
"scripts": {

Diff for: services/webui/src/App.tsx

-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ const GetLayout = (meResponse :any) => {
106106
})
107107
}
108108
const SetDefaultLayout = (layout: any, meResponse: any) => {
109-
console.log(layout)
110109
let url = ''
111110
if (window.location.origin === 'http://localhost:3000') {
112111
url = window.__RUNTIME_CONFIG__.REACT_APP_BASE_URL
@@ -131,7 +130,6 @@ const SetDefaultLayout = (layout: any, meResponse: any) => {
131130
is_default: true,
132131
is_private: true,
133132
}
134-
console.log(body)
135133

136134
axios
137135
.post(`${url}/main/core/api/v4/layout/set/widgets`, body, config)

Diff for: services/webui/src/components/widgets/WidgetLayout/index.tsx

+60-44
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import { array } from 'prop-types'
2424
import Shortcuts from '../Shortcuts'
2525
import Integrations from '../Integrations'
2626
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'
2929
const COMPONENT_MAPPING = {
3030
table: TableWidget,
3131
chart: ChartWidget,
@@ -121,7 +121,9 @@ export default function WidgetLayout({
121121
id: layout?.id,
122122
is_default: layout.is_default,
123123
user_id: me?.username,
124-
layout_config: layout_config,
124+
widget_ids: layout_config?.map((widget: any) => {
125+
return widget?.id
126+
}),
125127
name: layout.name,
126128
description: layout.description,
127129
is_private: layout.is_private,
@@ -134,43 +136,57 @@ export default function WidgetLayout({
134136
console.log(err)
135137
})
136138
}
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)
147148

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+
}
156189

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-
}
174190
const HandleRemoveItemByID = (id: string) => {
175191
const newItems = items.filter((item: any) => item.id !== id)
176192
setItems(newItems)
@@ -323,7 +339,7 @@ export default function WidgetLayout({
323339
return
324340
}
325341
const newItem = {
326-
id: `${selectedAddItem}-${items?.length}`,
342+
id: uuid(),
327343
data: {
328344
componentId: selectedAddItem,
329345
props: widgetProps,
@@ -351,7 +367,7 @@ export default function WidgetLayout({
351367
}
352368
if (id == 'integration') {
353369
const new_item = {
354-
id: 'integration',
370+
id: uuid(),
355371
data: {
356372
componentId: 'integration',
357373
title: 'Integrations',
@@ -366,7 +382,7 @@ export default function WidgetLayout({
366382
}
367383
if (id == 'shortcut') {
368384
const new_item = {
369-
id: 'shortcut',
385+
id: uuid(),
370386
data: {
371387
componentId: 'shortcut',
372388
title: 'Shortcuts',
@@ -381,7 +397,7 @@ export default function WidgetLayout({
381397
}
382398
if (id == 'sre') {
383399
const new_item = {
384-
id: 'sre',
400+
id: uuid(),
385401
data: {
386402
componentId: 'sre',
387403
title: 'SRE',
@@ -493,7 +509,7 @@ export default function WidgetLayout({
493509
// description={}
494510
actions={
495511
<div className="flex flex-row gap-2">
496-
{/* <ButtonDropdown
512+
<ButtonDropdown
497513
items={[
498514
{ id: 'add', text: 'Add new dashboard' },
499515

@@ -547,7 +563,7 @@ export default function WidgetLayout({
547563
ariaLabel="Board item settings"
548564
>
549565
Add Widget
550-
</ButtonDropdown> */}
566+
</ButtonDropdown>
551567
</div>
552568
}
553569
>

0 commit comments

Comments
 (0)