fix: add connection test feedback in server form
- Show error message when connection test fails - Disable test button while testing - Provide clear feedback when API key or hostname are invalid - Reset error message on successful connection
This commit is contained in:
@@ -20,6 +20,8 @@ struct ServerFormView: View {
|
|||||||
@State private var hostname: String
|
@State private var hostname: String
|
||||||
@State private var apiKey: String
|
@State private var apiKey: String
|
||||||
@State private var connectionOK: Bool = false
|
@State private var connectionOK: Bool = false
|
||||||
|
@State private var testingConnection: Bool = false
|
||||||
|
@State private var connectionError: String = ""
|
||||||
|
|
||||||
@Environment(\.dismiss) private var dismiss
|
@Environment(\.dismiss) private var dismiss
|
||||||
|
|
||||||
@@ -55,6 +57,12 @@ struct ServerFormView: View {
|
|||||||
SecureField("API Key", text: $apiKey)
|
SecureField("API Key", text: $apiKey)
|
||||||
.textFieldStyle(RoundedBorderTextFieldStyle())
|
.textFieldStyle(RoundedBorderTextFieldStyle())
|
||||||
|
|
||||||
|
if !connectionError.isEmpty {
|
||||||
|
Text(connectionError)
|
||||||
|
.foregroundColor(.red)
|
||||||
|
.font(.caption)
|
||||||
|
}
|
||||||
|
|
||||||
HStack {
|
HStack {
|
||||||
Button("Cancel") {
|
Button("Cancel") {
|
||||||
dismiss()
|
dismiss()
|
||||||
@@ -65,6 +73,8 @@ struct ServerFormView: View {
|
|||||||
await testConnection()
|
await testConnection()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.disabled(hostname.isEmpty || apiKey.isEmpty || testingConnection)
|
||||||
|
|
||||||
Button("Save") {
|
Button("Save") {
|
||||||
saveServer()
|
saveServer()
|
||||||
updateServer()
|
updateServer()
|
||||||
@@ -102,9 +112,21 @@ struct ServerFormView: View {
|
|||||||
let host = hostname.trimmingCharacters(in: .whitespacesAndNewlines)
|
let host = hostname.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
let key = apiKey.trimmingCharacters(in: .whitespacesAndNewlines)
|
let key = apiKey.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
|
|
||||||
|
await MainActor.run {
|
||||||
|
testingConnection = true
|
||||||
|
connectionError = ""
|
||||||
|
}
|
||||||
|
|
||||||
let reachable = await PingService.ping(hostname: host, apiKey: key)
|
let reachable = await PingService.ping(hostname: host, apiKey: key)
|
||||||
await MainActor.run {
|
await MainActor.run {
|
||||||
connectionOK = reachable
|
testingConnection = false
|
||||||
|
if reachable {
|
||||||
|
connectionOK = true
|
||||||
|
connectionError = ""
|
||||||
|
} else {
|
||||||
|
connectionOK = false
|
||||||
|
connectionError = "Connection failed. Check hostname and API key."
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//
|
//
|
||||||
// guard let url = URL(string: "https://\(host)/api/v2/ping") else {
|
// guard let url = URL(string: "https://\(host)/api/v2/ping") else {
|
||||||
|
|||||||
Reference in New Issue
Block a user