Skip to content

Commit f52e058

Browse files
UI fix api in project view (#11191)
* Add project id for post requests as well in the params * Replace leftover api calls to getAPI calls * ui: don't remove values from request if the value is null or empty string * Address comments * Apply suggestions from code review * Apply suggestions from code review Co-authored-by: Suresh Kumar Anaparti <[email protected]> * fixup * Return null if guiTheme requests fails --------- Co-authored-by: Suresh Kumar Anaparti <[email protected]>
1 parent e8ab0ae commit f52e058

File tree

7 files changed

+47
-16
lines changed

7 files changed

+47
-16
lines changed

ui/src/api/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function postAPI (command, data = {}) {
4747
params.append('response', 'json')
4848
if (data) {
4949
Object.entries(data).forEach(([key, value]) => {
50-
if (value !== undefined && value !== null && value !== '') {
50+
if (value !== undefined && value !== null) {
5151
params.append(key, value)
5252
}
5353
})

ui/src/components/view/ImageDeployInstanceButton.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
</template>
4242

4343
<script>
44-
import { api } from '@/api'
44+
import { getAPI } from '@/api'
4545
import ResourceIcon from '@/components/view/ResourceIcon'
4646
4747
export default {
@@ -100,7 +100,7 @@ export default {
100100
this.itemCount = 0
101101
this.fetchLoading = true
102102
this.zones = []
103-
api(this.imageApi, params).then(json => {
103+
getAPI(this.imageApi, params).then(json => {
104104
const imageResponse = json?.[this.imageApi.toLowerCase() + 'response']?.[this.$route.meta.name] || []
105105
this.zones = imageResponse.map(i => ({
106106
id: i.zoneid,
@@ -122,7 +122,7 @@ export default {
122122
}
123123
const zoneids = this.zones.map(z => z.id)
124124
this.loading = true
125-
api('listZones', { showicon: true, ids: zoneids.join(',') }).then(json => {
125+
getAPI('listZones', { showicon: true, ids: zoneids.join(',') }).then(json => {
126126
this.zones = json.listzonesresponse.zone || []
127127
}).finally(() => {
128128
this.loading = false

ui/src/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import {
4444
import { VueAxios } from './utils/request'
4545
import directives from './utils/directives'
4646
import Cookies from 'js-cookie'
47-
import { api } from '@/api'
47+
import { getAPI } from '@/api'
4848
import { applyCustomGuiTheme } from './utils/guiTheme'
4949

5050
vueApp.use(VueAxios, router)
@@ -106,7 +106,7 @@ fetch('config.json?ts=' + Date.now())
106106
let domainid = null
107107

108108
if (userid !== undefined && Cookies.get('sessionkey')) {
109-
await api('listUsers', { userid: userid }).then(response => {
109+
await getAPI('listUsers', { userid: userid }).then(response => {
110110
accountid = response.listusersresponse.user[0].accountid
111111
domainid = response.listusersresponse.user[0].domainid
112112
})

ui/src/utils/guiTheme.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// under the License.
1717

1818
import { vueProps } from '@/vue-app'
19-
import { api } from '@/api'
19+
import { getAPI } from '@/api'
2020

2121
export async function applyCustomGuiTheme (accountid, domainid) {
2222
await fetch('config.json').then(response => response.json()).then(config => {
@@ -45,10 +45,13 @@ export async function applyCustomGuiTheme (accountid, domainid) {
4545
}
4646

4747
async function fetchGuiTheme (params) {
48-
return await api('listGuiThemes', params).then(response => {
48+
return await getAPI('listGuiThemes', params).then(response => {
4949
if (response.listguithemesresponse.guiThemes) {
5050
return response.listguithemesresponse.guiThemes[0]
5151
}
52+
}).catch(error => {
53+
console.error('Error fetching GUI theme:', error)
54+
return null
5255
})
5356
}
5457

ui/src/utils/request.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,15 @@ const err = (error) => {
149149
service.interceptors.request.use(config => {
150150
source = sourceToken.getSource()
151151
config.cancelToken = source.token
152+
153+
handleGetRequestParams(config)
154+
155+
handlePostRequestParams(config)
156+
157+
return config
158+
}, err)
159+
160+
function handleGetRequestParams (config) {
152161
if (config && config.params) {
153162
config.params.response = 'json'
154163
const project = vueProps.$localStorage.get(CURRENT_PROJECT)
@@ -160,11 +169,30 @@ service.interceptors.request.use(config => {
160169
}
161170
}
162171
if (config.params.ignoreproject !== undefined) {
163-
config.params.ignoreproject = null
172+
delete config.params.ignoreproject
164173
}
165174
}
166-
return config
167-
}, err)
175+
}
176+
177+
function handlePostRequestParams (config) {
178+
if (config && config.data && config.data instanceof URLSearchParams) {
179+
const project = vueProps.$localStorage.get(CURRENT_PROJECT)
180+
const command = config.data.get('command')
181+
const hasProjectId = config.data.has('projectid')
182+
const ignoreProject = config.data.has('ignoreproject')
183+
184+
if (!hasProjectId && !ignoreProject && project && project.id) {
185+
if (command === 'listTags') {
186+
config.data.append('projectid', '-1')
187+
} else if (command !== 'assignVirtualMachine') {
188+
config.data.append('projectid', project.id)
189+
}
190+
}
191+
if (config.data.has('ignoreproject')) {
192+
config.data.delete('ignoreproject')
193+
}
194+
}
195+
}
168196

169197
// response interceptor
170198
service.interceptors.response.use((response) => {

ui/src/views/compute/KubernetesAddNodes.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666

6767
<script>
6868
import { ref, reactive, toRaw } from 'vue'
69-
import { api } from '@/api'
69+
import { getAPI, postAPI } from '@/api'
7070
import TooltipLabel from '@/components/widgets/TooltipLabel'
7171
7272
export default {
@@ -112,7 +112,7 @@ export default {
112112
callListVms (accountId, domainId) {
113113
return new Promise((resolve) => {
114114
this.volumes = []
115-
api('listVirtualMachines', {
115+
getAPI('listVirtualMachines', {
116116
accountId: accountId,
117117
domainId: domainId,
118118
details: 'min',
@@ -172,7 +172,7 @@ export default {
172172
},
173173
addNodesToKubernetesCluster (params) {
174174
return new Promise((resolve, reject) => {
175-
api('addNodesToKubernetesCluster', params).then(json => {
175+
postAPI('addNodesToKubernetesCluster', params).then(json => {
176176
const jobId = json.addnodestokubernetesclusterresponse.jobid
177177
return resolve(jobId)
178178
}).catch(error => {

ui/src/views/compute/KubernetesRemoveNodes.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
<script>
5656
import { ref, reactive, toRaw } from 'vue'
57-
import { api } from '@/api'
57+
import { postAPI } from '@/api'
5858
import TooltipLabel from '@/components/widgets/TooltipLabel'
5959
6060
export default {
@@ -137,7 +137,7 @@ export default {
137137
},
138138
removeNodesFromKubernetesCluster (params) {
139139
return new Promise((resolve, reject) => {
140-
api('removeNodesFromKubernetesCluster', params).then(json => {
140+
postAPI('removeNodesFromKubernetesCluster', params).then(json => {
141141
const jobId = json.removenodesfromkubernetesclusterresponse.jobid
142142
return resolve(jobId)
143143
}).catch(error => {

0 commit comments

Comments
 (0)