Skip to content

Commit e43f932

Browse files
committed
Fetch and display resource on UI
1 parent b8f95ef commit e43f932

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

WooCommerce/Classes/ViewModels/Booking Details/AppointmentDetailsContent.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Foundation
2-
import struct Networking.Booking
2+
import Yosemite
33

44
extension BookingDetailsViewModel {
55
struct AppointmentDetailsContent {
@@ -14,17 +14,21 @@ extension BookingDetailsViewModel {
1414

1515
let rows: [Row]
1616

17-
init(_ booking: Booking) {
17+
init(_ booking: Booking, resource: BookingResource?) {
1818
let appointmentDate = booking.startDate.toString(dateStyle: .short, timeStyle: .none, timeZone: BookingListTab.utcTimeZone)
1919
let appointmentTimeFrame = [
2020
booking.startDate.toString(dateStyle: .none, timeStyle: .short, timeZone: BookingListTab.utcTimeZone),
2121
booking.endDate.toString(dateStyle: .none, timeStyle: .short, timeZone: BookingListTab.utcTimeZone)
2222
].joined(separator: " - ")
2323

24+
let resourceRow: Row? = {
25+
guard booking.resourceID > 0 else { return nil }
26+
return Row(title: Localization.appointmentDetailsAssignedStaffTitle, value: resource?.name ?? "-")
27+
}()
2428
rows = [
2529
Row(title: Localization.appointmentDetailsDateRowTitle, value: appointmentDate),
2630
Row(title: Localization.appointmentDetailsTimeRowTitle, value: appointmentTimeFrame),
27-
Row(title: Localization.appointmentDetailsAssignedStaffTitle, value: "Marianne Renoir"), /// Temporarily hardcoded
31+
resourceRow,
2832
Row(title: Localization.appointmentDetailsLocationTitle, value: "238 Willow Creek Drive, Montgomery ..."), /// Temporarily hardcoded
2933
Row(
3034
title: Localization.appointmentDetailsDurationTitle,
@@ -37,7 +41,7 @@ extension BookingDetailsViewModel {
3741
title: Localization.appointmentDetailsPriceTitle,
3842
value: BookingDetailsViewModel.formatPrice(for: booking, priceString: booking.cost)
3943
)
40-
]
44+
].compactMap { $0 }
4145
}
4246
}
4347
}

WooCommerce/Classes/ViewModels/Booking Details/BookingDetailsViewModel.swift

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import Foundation
22
import Yosemite
3+
import protocol Storage.StorageManagerType
34

45
final class BookingDetailsViewModel: ObservableObject {
56
private let stores: StoresManager
67

78
private var booking: Booking
9+
private var bookingResource: BookingResource?
810

911
// EntityListener: Update / Deletion Notifications.
1012
///
@@ -15,24 +17,28 @@ final class BookingDetailsViewModel: ObservableObject {
1517
let navigationTitle: String
1618
@Published private(set) var sections: [Section] = []
1719

18-
init(booking: Booking, stores: StoresManager = ServiceLocator.stores) {
20+
init(booking: Booking,
21+
stores: StoresManager = ServiceLocator.stores,
22+
storage: StorageManagerType = ServiceLocator.storageManager) {
1923
self.booking = booking
2024
self.stores = stores
2125

2226
navigationTitle = Self.navigationTitle(for: booking)
23-
setupSections(with: booking)
27+
let resource = storage.viewStorage.loadBookingResource(siteID: booking.siteID, resourceID: booking.resourceID)?.toReadOnly()
28+
self.bookingResource = resource
29+
setupSections(with: booking, resource: resource)
2430
configureEntityListener()
2531
}
2632

27-
private func setupSections(with booking: Booking) {
33+
private func setupSections(with booking: Booking, resource: BookingResource?) {
2834
let headerContent = HeaderContent(booking)
2935
let headerSection = Section(
3036
content: .header(headerContent)
3137
)
3238

3339
let appointmentDetailsSection = Section(
3440
header: .title(Localization.appointmentDetailsSectionHeaderTitle.uppercased()),
35-
content: .appointmentDetails(AppointmentDetailsContent(booking))
41+
content: .appointmentDetails(AppointmentDetailsContent(booking, resource: resource))
3642
)
3743

3844
let customerSection: Section? = {
@@ -75,6 +81,9 @@ final class BookingDetailsViewModel: ObservableObject {
7581

7682
extension BookingDetailsViewModel {
7783
func syncData() async {
84+
if let resource = await fetchResource() {
85+
self.bookingResource = resource // only update resource if fetching succeeds
86+
}
7887
await syncBooking()
7988
}
8089
}
@@ -84,7 +93,7 @@ private extension BookingDetailsViewModel {
8493
entityListener.onUpsert = { [weak self] booking in
8594
guard let self else { return }
8695
self.booking = booking
87-
self.setupSections(with: booking)
96+
self.setupSections(with: booking, resource: bookingResource)
8897
}
8998
}
9099

@@ -96,6 +105,25 @@ private extension BookingDetailsViewModel {
96105
}
97106
}
98107

108+
@MainActor
109+
func fetchResource() async -> BookingResource? {
110+
do {
111+
return try await withCheckedThrowingContinuation { continuation in
112+
stores.dispatch(BookingAction.fetchResource(siteID: booking.siteID, resourceID: booking.resourceID) { result in
113+
switch result {
114+
case .success(let resource):
115+
continuation.resume(returning: resource)
116+
case .failure(let error):
117+
continuation.resume(throwing: error)
118+
}
119+
})
120+
}
121+
} catch {
122+
DDLogError("⛔️ Error fetching resource for Booking: \(error)")
123+
return nil
124+
}
125+
}
126+
99127
@MainActor
100128
func retrieveBooking() async throws {
101129
try await withCheckedThrowingContinuation { continuation in

0 commit comments

Comments
 (0)