editing all fields works and app is actually functional with profile edits

This commit is contained in:
2026-04-10 22:20:12 -05:00
parent b8880af200
commit b79d591395
8 changed files with 69 additions and 52 deletions

View File

@@ -120,9 +120,9 @@ final class AddChemicalViewModel {
func loadProfile() async {
guard !isEditing else { return }
if let profile = try? await profileClient.get() {
piFirstName = profile.piFirstName
bldgCode = profile.bldgCode
lab = profile.lab
piFirstName = profile.piFirstName ?? ""
bldgCode = profile.bldgCode ?? ""
lab = profile.lab ?? ""
contact = profile.contact ?? ""
}
}

View File

@@ -61,11 +61,16 @@ struct InventoryView: View {
ProgressView("Loading...")
.frame(maxWidth: .infinity, maxHeight: .infinity)
} else if viewModel.chemicals.isEmpty {
ContentUnavailableView(
"No Chemicals",
systemImage: "flask",
description: Text("Add your first chemical using the + button.")
)
ScrollView {
ContentUnavailableView(
"No Chemicals",
systemImage: "flask",
description: Text("Add your first chemical using the + button.")
)
}
.refreshable {
await viewModel.loadChemicals()
}
} else {
List {
ForEach(viewModel.chemicals) { chemical in

View File

@@ -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 ?? ""
}
}

View File

@@ -499,9 +499,9 @@ final class LabelScannerViewModel {
func loadProfile() async {
if let profile = try? await profileClient.get() {
piFirstName = profile.piFirstName
bldgCode = profile.bldgCode
lab = profile.lab
piFirstName = profile.piFirstName ?? ""
bldgCode = profile.bldgCode ?? ""
lab = profile.lab ?? ""
contact = profile.contact ?? ""
}
}