Skip to content

Commit 0ece230

Browse files
authored
Merge pull request #166 from IBM/gateway_ui_reload
UI reload issue
2 parents 408649e + e626a2b commit 0ece230

File tree

3 files changed

+42
-42
lines changed

3 files changed

+42
-42
lines changed

mcpgateway/admin.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ async def admin_get_gateway(gateway_id: int, db: Session = Depends(get_db), user
733733

734734

735735
@admin_router.post("/gateways")
736-
async def admin_add_gateway(request: Request, db: Session = Depends(get_db), user: str = Depends(require_auth)) -> RedirectResponse:
736+
async def admin_add_gateway(request: Request, db: Session = Depends(get_db), user: str = Depends(require_auth)) -> JSONResponse:
737737
"""Add a gateway via the admin UI.
738738
739739
Expects form fields:
@@ -763,19 +763,21 @@ async def admin_add_gateway(request: Request, db: Session = Depends(get_db), use
763763
auth_header_key=form.get("auth_header_key", ""),
764764
auth_header_value=form.get("auth_header_value", ""),
765765
)
766-
root_path = request.scope.get("root_path", "")
767766
try:
768767
await gateway_service.register_gateway(db, gateway)
769-
return RedirectResponse(f"{root_path}/admin#gateways", status_code=303)
768+
return JSONResponse(
769+
content={"message": "Gateway registered successfully!", "success": True},
770+
status_code=200,
771+
)
772+
770773
except Exception as ex:
771774
if isinstance(ex, GatewayConnectionError):
772-
return RedirectResponse(f"{root_path}/admin#gateways", status_code=502)
775+
return JSONResponse(content={"message": str(ex), "success": False}, status_code=502)
773776
if isinstance(ex, ValueError):
774-
return RedirectResponse(f"{root_path}/admin#gateways", status_code=400)
777+
return JSONResponse(content={"message": str(ex), "success": False}, status_code=400)
775778
if isinstance(ex, RuntimeError):
776-
return RedirectResponse(f"{root_path}/admin#gateways", status_code=500)
777-
778-
return RedirectResponse(f"{root_path}/admin#gateways", status_code=500)
779+
return JSONResponse(content={"message": str(ex), "success": False}, status_code=500)
780+
return JSONResponse(content={"message": str(ex), "success": False}, status_code=500)
779781

780782

781783
@admin_router.post("/gateways/{gateway_id}/edit")

mcpgateway/static/admin.js

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -147,43 +147,42 @@ document.addEventListener("DOMContentLoaded", function () {
147147
}
148148
});
149149

150-
document
151-
.getElementById("add-gateway-form")
152-
.addEventListener("submit", (e) => {
153-
e.preventDefault();
150+
document.getElementById("add-gateway-form")
151+
.addEventListener("submit", async (e) => {
152+
e.preventDefault();
154153

155-
const form = e.target;
156-
const formData = new FormData(form);
154+
const form = e.target;
155+
const formData = new FormData(form);
157156

158-
const status = document.getElementById("status-gateways");
159-
const loading = document.getElementById("add-gateway-loading");
157+
const status = document.getElementById("status-gateways");
158+
const loading = document.getElementById("add-gateway-loading");
160159

161-
// Show loading and clear previous status
162-
loading.style.display = "block";
163-
status.textContent = "";
164-
status.classList.remove("error-status");
160+
// Show loading and clear previous status
161+
loading.style.display = "block";
162+
status.textContent = "";
163+
status.classList.remove("error-status");
165164

166-
fetch(`${window.ROOT_PATH}/admin/gateways`, {
167-
method: "POST",
168-
body: formData,
169-
})
170-
.then((response) => {
171-
if (!response.ok) {
172-
status.textContent = "Connection failed!";
165+
try {
166+
const response = await fetch(`${window.ROOT_PATH}/admin/gateways`, {
167+
method: "POST",
168+
body: formData,
169+
});
170+
171+
let result = await response.json();
172+
if (!result.success) {
173+
alert(result.message || "An error occurred");
174+
} else {
175+
window.location.href = `${window.ROOT_PATH}/admin#gateways`; // Redirect on success
176+
}
177+
178+
} catch (error) {
179+
console.error("Error:", error);
180+
status.textContent = error.message || "An error occurred!";
173181
status.classList.add("error-status");
174-
} else {
175-
location.reload(); // Will exit before hiding spinner
182+
} finally {
183+
loading.style.display = "none"; // Hide loading spinner
176184
}
177-
})
178-
.catch((error) => {
179-
console.error("Error:", error);
180-
status.textContent = "An error occurred!";
181-
status.classList.add("error-status");
182-
})
183-
.finally(() => {
184-
loading.style.display = "none"; // Hide loading
185185
});
186-
});
187186

188187

189188
document
@@ -305,7 +304,7 @@ document.addEventListener("DOMContentLoaded", function () {
305304
if (!result.success) {
306305
alert(result.message || "An error occurred");
307306
} else {
308-
window.location.href = "/admin#tools"; // Redirect on success
307+
window.location.href = `${window.ROOT_PATH}/admin#tools`; // Redirect on success
309308
}
310309
} catch (error) {
311310
console.error("Fetch error:", error);

tests/unit/mcpgateway/test_admin.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -561,9 +561,8 @@ async def test_admin_add_gateway(self, mock_register_gateway, mock_request, mock
561561

562562
# Assert
563563
mock_register_gateway.assert_called_once()
564-
assert isinstance(result, RedirectResponse)
565-
assert result.status_code == 303
566-
assert "/admin#gateways" in result.headers["location"]
564+
assert isinstance(result, JSONResponse)
565+
assert result.status_code == 200
567566

568567
@patch.object(GatewayService, "update_gateway")
569568
async def test_admin_edit_gateway(self, mock_update_gateway, mock_request, mock_db):

0 commit comments

Comments
 (0)