From 813031c8234633f57bfa4049980183df65c4d35e Mon Sep 17 00:00:00 2001 From: pulipakaa24 Date: Fri, 20 Mar 2026 02:40:51 -0500 Subject: [PATCH] Some messy stuff cleaned up in Inventory UI --- LabWise/ChemicalDetailView.swift | 54 +++++++++++++++++-- LabWise/DashboardView.swift | 3 +- LabWise/InventoryView.swift | 6 +-- LabWiseKit/Sources/LabWiseKit/APIClient.swift | 10 ---- 4 files changed, 53 insertions(+), 20 deletions(-) diff --git a/LabWise/ChemicalDetailView.swift b/LabWise/ChemicalDetailView.swift index 669adc6..05c6142 100644 --- a/LabWise/ChemicalDetailView.swift +++ b/LabWise/ChemicalDetailView.swift @@ -1,6 +1,52 @@ import SwiftUI import LabWiseKit +private let isoDateFormatter: ISO8601DateFormatter = { + let f = ISO8601DateFormatter() + f.formatOptions = [.withFullDate] + return f +}() + +private let isoTimestampFormatter: ISO8601DateFormatter = { + let f = ISO8601DateFormatter() + f.formatOptions = [.withInternetDateTime, .withFractionalSeconds] + return f +}() + +private let displayDateFormatter: DateFormatter = { + let f = DateFormatter() + f.dateStyle = .medium + f.timeStyle = .none + return f +}() + +private let displayTimestampFormatter: DateFormatter = { + let f = DateFormatter() + f.dateStyle = .medium + f.timeStyle = .short + return f +}() + +private func formatDate(_ iso: String?) -> String { + guard let iso else { return "" } + if let date = isoDateFormatter.date(from: iso) { + return displayDateFormatter.string(from: date) + } + // Fallback: try full timestamp (e.g. "2025-03-01T00:00:00.000Z") + if let date = isoTimestampFormatter.date(from: iso) { + return displayDateFormatter.string(from: date) + } + return iso +} + +private func formatTimestamp(_ iso: String?) -> String { + guard let iso else { return "" } + if let date = isoTimestampFormatter.date(from: iso) { + return displayTimestampFormatter.string(from: date) + } + return iso +} + struct ChemicalDetailView: View { @State private var chemical: Chemical @State private var showEdit = false @@ -56,8 +102,8 @@ struct ChemicalDetailView: View { if let lot = chemical.lotNumber { LabeledContent("Lot #", value: lot) } - if let exp = chemical.expirationDate { - LabeledContent("Expiration", value: exp) + if let exp = chemical.expirationDate, !exp.isEmpty { + LabeledContent("Expiration", value: formatDate(exp)) } if let barcode = chemical.barcode { LabeledContent("Barcode", value: barcode) @@ -76,10 +122,10 @@ struct ChemicalDetailView: View { Section("Record") { if let created = chemical.createdAt { - LabeledContent("Created", value: created) + LabeledContent("Created", value: formatTimestamp(created)) } if let updated = chemical.updatedAt { - LabeledContent("Updated", value: updated) + LabeledContent("Updated", value: formatTimestamp(updated)) } } } diff --git a/LabWise/DashboardView.swift b/LabWise/DashboardView.swift index 4fc02c1..b34f4b0 100644 --- a/LabWise/DashboardView.swift +++ b/LabWise/DashboardView.swift @@ -65,8 +65,7 @@ final class DashboardViewModel { do { chemicals = try await client.list() } catch { - print("[DashboardViewModel] load error: \(error)") - errorMessage = "Failed to load: \(error)" + errorMessage = "Failed to load dashboard" } } diff --git a/LabWise/InventoryView.swift b/LabWise/InventoryView.swift index 593ac7e..fe237f5 100644 --- a/LabWise/InventoryView.swift +++ b/LabWise/InventoryView.swift @@ -16,8 +16,7 @@ final class InventoryViewModel { do { chemicals = try await client.list() } catch { - print("[InventoryViewModel] loadChemicals error: \(error)") - errorMessage = "Failed to load chemicals: \(error)" + errorMessage = "Failed to load chemicals" } } @@ -26,8 +25,7 @@ final class InventoryViewModel { try await client.delete(id: chemical.id) chemicals.removeAll { $0.id == chemical.id } } catch { - print("[InventoryViewModel] delete error: \(error)") - errorMessage = "Failed to delete: \(error)" + errorMessage = "Failed to delete" } } } diff --git a/LabWiseKit/Sources/LabWiseKit/APIClient.swift b/LabWiseKit/Sources/LabWiseKit/APIClient.swift index 64e6110..3f860b9 100644 --- a/LabWiseKit/Sources/LabWiseKit/APIClient.swift +++ b/LabWiseKit/Sources/LabWiseKit/APIClient.swift @@ -45,9 +45,6 @@ public final class APIClient: Sendable { req.httpMethod = method req.setValue(contentType, forHTTPHeaderField: "Content-Type") - let cookieNames = HTTPCookieStorage.shared.cookies(for: url)?.map(\.name) ?? [] - print("[APIClient] \(method) \(path) — cookies: \(cookieNames)") - if let body { do { req.httpBody = try JSONEncoder.api.encode(body) @@ -60,27 +57,20 @@ public final class APIClient: Sendable { do { (data, response) = try await session.data(for: req) } catch { - print("[APIClient] Network error on \(method) \(path): \(error)") throw APIError.networkError(error) } guard let http = response as? HTTPURLResponse else { - print("[APIClient] Non-HTTP response on \(method) \(path)") throw APIError.networkError(URLError(.badServerResponse)) } - print("[APIClient] \(method) \(path) → \(http.statusCode)") - if http.statusCode == 401 { - print("[APIClient] 401 — clearing session and signalling unauthorized") clearSessionCookies() onUnauthorized?() throw APIError.unauthorized } guard (200..<300).contains(http.statusCode) else { - let body = String(data: data, encoding: .utf8) ?? "" - print("[APIClient] HTTP \(http.statusCode) on \(method) \(path): \(body)") throw APIError.httpError(statusCode: http.statusCode, data: data) }