editing all fields works and app is actually functional with profile edits
This commit is contained in:
@@ -24,12 +24,18 @@ final class ProfileViewModel {
|
||||
func load() async {
|
||||
isLoading = true
|
||||
defer { isLoading = false }
|
||||
do {
|
||||
let p = try await profileClient.get()
|
||||
|
||||
async let profileFetch = profileClient.get()
|
||||
async let userFetch = authClient.fetchCurrentUser()
|
||||
|
||||
if let p = try? await profileFetch {
|
||||
profile = p
|
||||
populateFields(from: p)
|
||||
} catch {
|
||||
// Profile may not exist yet — that's OK
|
||||
}
|
||||
if let user = try? await userFetch {
|
||||
await MainActor.run {
|
||||
AppState.shared.currentUser = user
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,17 +44,17 @@ final class ProfileViewModel {
|
||||
defer { isSaving = false }
|
||||
do {
|
||||
let trimmedName = displayName.trimmingCharacters(in: .whitespaces)
|
||||
if !trimmedName.isEmpty && trimmedName != AppState.shared.currentUser?.name {
|
||||
if trimmedName != (AppState.shared.currentUser?.name ?? "") {
|
||||
let updatedUser = try await authClient.updateName(trimmedName)
|
||||
await MainActor.run {
|
||||
AppState.shared.currentUser = updatedUser
|
||||
}
|
||||
}
|
||||
let body = UserProfileUpsertBody(
|
||||
piFirstName: piFirstName,
|
||||
bldgCode: bldgCode,
|
||||
lab: lab,
|
||||
contact: contact.isEmpty ? nil : contact
|
||||
piFirstName: piFirstName.trimmingCharacters(in: .whitespaces).isEmpty ? nil : piFirstName.trimmingCharacters(in: .whitespaces),
|
||||
bldgCode: bldgCode.trimmingCharacters(in: .whitespaces).isEmpty ? nil : bldgCode.trimmingCharacters(in: .whitespaces),
|
||||
lab: lab.trimmingCharacters(in: .whitespaces).isEmpty ? nil : lab.trimmingCharacters(in: .whitespaces),
|
||||
contact: contact.trimmingCharacters(in: .whitespaces).isEmpty ? nil : contact.trimmingCharacters(in: .whitespaces)
|
||||
)
|
||||
let updated = try await profileClient.upsert(body)
|
||||
profile = updated
|
||||
@@ -80,9 +86,9 @@ final class ProfileViewModel {
|
||||
}
|
||||
|
||||
private func populateFields(from p: UserProfile) {
|
||||
piFirstName = p.piFirstName
|
||||
bldgCode = p.bldgCode
|
||||
lab = p.lab
|
||||
piFirstName = p.piFirstName ?? ""
|
||||
bldgCode = p.bldgCode ?? ""
|
||||
lab = p.lab ?? ""
|
||||
contact = p.contact ?? ""
|
||||
}
|
||||
}
|
||||
@@ -125,6 +131,18 @@ struct ProfileView: View {
|
||||
}
|
||||
}
|
||||
|
||||
Section {
|
||||
Button(role: .destructive) {
|
||||
Task { await viewModel.signOut() }
|
||||
} label: {
|
||||
HStack {
|
||||
Spacer()
|
||||
Text("Sign Out")
|
||||
Spacer()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Section {
|
||||
Button(role: .destructive) {
|
||||
viewModel.showDeleteConfirm = true
|
||||
@@ -139,17 +157,10 @@ struct ProfileView: View {
|
||||
} footer: {
|
||||
Text("Permanently deletes your account and all data including chemicals and protocols.")
|
||||
}
|
||||
|
||||
Section {
|
||||
Button(role: .destructive) {
|
||||
Task { await viewModel.signOut() }
|
||||
} label: {
|
||||
HStack {
|
||||
Spacer()
|
||||
Text("Sign Out")
|
||||
Spacer()
|
||||
}
|
||||
}
|
||||
}
|
||||
.refreshable {
|
||||
if !viewModel.isEditing {
|
||||
await viewModel.load()
|
||||
}
|
||||
}
|
||||
.navigationTitle("Profile")
|
||||
@@ -173,9 +184,9 @@ struct ProfileView: View {
|
||||
viewModel.isEditing = false
|
||||
viewModel.displayName = appState.currentUser?.name ?? ""
|
||||
if let p = viewModel.profile {
|
||||
viewModel.piFirstName = p.piFirstName
|
||||
viewModel.bldgCode = p.bldgCode
|
||||
viewModel.lab = p.lab
|
||||
viewModel.piFirstName = p.piFirstName ?? ""
|
||||
viewModel.bldgCode = p.bldgCode ?? ""
|
||||
viewModel.lab = p.lab ?? ""
|
||||
viewModel.contact = p.contact ?? ""
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user