From 650bd125ee2203a42014215983e2aefbe0f25bda Mon Sep 17 00:00:00 2001 From: Huong Do Date: Mon, 24 Mar 2025 11:33:50 +0700 Subject: [PATCH 01/10] Remove redundant containsHazardousMaterials --- .../WooShippingHazmatDetailView.swift | 8 ++++---- .../WooShippingHazmatRow.swift | 13 +++++-------- .../WooShippingCreateLabelsView.swift | 3 +-- .../WooShippingCreateLabelsViewModel.swift | 1 - 4 files changed, 10 insertions(+), 15 deletions(-) diff --git a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Hazmat Section/WooShippingHazmatDetailView.swift b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Hazmat Section/WooShippingHazmatDetailView.swift index b7100622ded..a0d52d8fb3c 100644 --- a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Hazmat Section/WooShippingHazmatDetailView.swift +++ b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Hazmat Section/WooShippingHazmatDetailView.swift @@ -11,8 +11,8 @@ struct WooShippingHazmatDetailView: View { @State private var isShowingCategoryList = false - init(isHazardous: Bool, selectedCategory: ShippingLabelHazmatCategory?) { - self.isHazardous = isHazardous + init(selectedCategory: ShippingLabelHazmatCategory?) { + self.isHazardous = selectedCategory != nil self.selectedCategory = selectedCategory } @@ -29,6 +29,7 @@ struct WooShippingHazmatDetailView: View { Toggle(isOn: $isHazardous) { Text(Localization.toggleLabel) } + .tint(Color.accentColor) Button(Localization.selectCategory) { isShowingCategoryList = true @@ -177,6 +178,5 @@ private extension WooShippingHazmatDetailView { } #Preview { - WooShippingHazmatDetailView(isHazardous: true, - selectedCategory: .airEligibleEthanol) + WooShippingHazmatDetailView(selectedCategory: .airEligibleEthanol) } diff --git a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Hazmat Section/WooShippingHazmatRow.swift b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Hazmat Section/WooShippingHazmatRow.swift index 8b2293380c6..ddb899bb8f6 100644 --- a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Hazmat Section/WooShippingHazmatRow.swift +++ b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Hazmat Section/WooShippingHazmatRow.swift @@ -4,16 +4,15 @@ struct WooShippingHazmatRow: View { /// Whether the interactions (navigation/setting selection) are enabled. private let enabled: Bool - @Binding private var isHazardous: Bool + private let isHazardous: Bool @Binding private var selectedCategory: ShippingLabelHazmatCategory? @State private var isShowingDetailView = false - init(isHazardous: Binding, - selectedCategory: Binding, + init(selectedCategory: Binding, enabled: Bool) { - self._isHazardous = isHazardous + isHazardous = selectedCategory.wrappedValue != nil self._selectedCategory = selectedCategory self.enabled = enabled } @@ -37,8 +36,7 @@ struct WooShippingHazmatRow: View { .buttonStyle(.plain) .disabled(!enabled) .sheet(isPresented: $isShowingDetailView) { - WooShippingHazmatDetailView(isHazardous: isHazardous, - selectedCategory: selectedCategory) + WooShippingHazmatDetailView(selectedCategory: selectedCategory) } } } @@ -67,8 +65,7 @@ private extension WooShippingHazmatRow { } #Preview { - WooShippingHazmatRow(isHazardous: .constant(false), - selectedCategory: .constant(nil), + WooShippingHazmatRow(selectedCategory: .constant(nil), enabled: true) .padding() } diff --git a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsView.swift b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsView.swift index 8feb1d61f4f..4f486599515 100644 --- a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsView.swift +++ b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsView.swift @@ -110,8 +110,7 @@ private extension WooShippingCreateLabelsView { WooShippingItems(viewModel: viewModel.items) - WooShippingHazmatRow(isHazardous: $viewModel.containsHazardousMaterials, - selectedCategory: $viewModel.hazmatCategory, + WooShippingHazmatRow(selectedCategory: $viewModel.hazmatCategory, enabled: !viewModel.canViewLabel) WooShippingCustomsRow(informationIsCompleted: viewModel.customsInformationIsCompleted, diff --git a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModel.swift b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModel.swift index 8378e1d641b..797836c60e9 100644 --- a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModel.swift +++ b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModel.swift @@ -21,7 +21,6 @@ final class WooShippingCreateLabelsViewModel: ObservableObject { private var subscriptions: Set = [] private var debounceDuration: Double = 1 - @Published var containsHazardousMaterials = false @Published var hazmatCategory: ShippingLabelHazmatCategory? @Published var labelPurchaseErrorNotice: Notice? From d6221a11b1fdf5c45f2df92fa320aafc0e36a143 Mon Sep 17 00:00:00 2001 From: Huong Do Date: Mon, 24 Mar 2025 11:40:08 +0700 Subject: [PATCH 02/10] Dismiss detail row upon category selection --- .../WooShippingHazmatDetailView.swift | 11 ++++++++--- .../WooShippingHazmatRow.swift | 7 +++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Hazmat Section/WooShippingHazmatDetailView.swift b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Hazmat Section/WooShippingHazmatDetailView.swift index a0d52d8fb3c..0ba1db3465b 100644 --- a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Hazmat Section/WooShippingHazmatDetailView.swift +++ b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Hazmat Section/WooShippingHazmatDetailView.swift @@ -11,9 +11,13 @@ struct WooShippingHazmatDetailView: View { @State private var isShowingCategoryList = false - init(selectedCategory: ShippingLabelHazmatCategory?) { + private let selectionHandler: (ShippingLabelHazmatCategory?) -> Void + + init(selectedCategory: ShippingLabelHazmatCategory?, + selectionHandler: @escaping (ShippingLabelHazmatCategory?) -> Void) { self.isHazardous = selectedCategory != nil self.selectedCategory = selectedCategory + self.selectionHandler = selectionHandler } var body: some View { @@ -66,7 +70,8 @@ struct WooShippingHazmatDetailView: View { .sheet(isPresented: $isShowingCategoryList) { WooShippingHazmatCategoryList(selectedItem: selectedCategory, selectionHandler: { category in - // TODO: dismiss view + selectionHandler(category) + dismiss() }) } } @@ -178,5 +183,5 @@ private extension WooShippingHazmatDetailView { } #Preview { - WooShippingHazmatDetailView(selectedCategory: .airEligibleEthanol) + WooShippingHazmatDetailView(selectedCategory: .airEligibleEthanol, selectionHandler: { _ in }) } diff --git a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Hazmat Section/WooShippingHazmatRow.swift b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Hazmat Section/WooShippingHazmatRow.swift index ddb899bb8f6..df502f01894 100644 --- a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Hazmat Section/WooShippingHazmatRow.swift +++ b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Hazmat Section/WooShippingHazmatRow.swift @@ -4,7 +4,7 @@ struct WooShippingHazmatRow: View { /// Whether the interactions (navigation/setting selection) are enabled. private let enabled: Bool - private let isHazardous: Bool + @State private var isHazardous: Bool @Binding private var selectedCategory: ShippingLabelHazmatCategory? @@ -36,7 +36,10 @@ struct WooShippingHazmatRow: View { .buttonStyle(.plain) .disabled(!enabled) .sheet(isPresented: $isShowingDetailView) { - WooShippingHazmatDetailView(selectedCategory: selectedCategory) + WooShippingHazmatDetailView(selectedCategory: selectedCategory) { selectedCategory in + self.selectedCategory = selectedCategory + isHazardous = selectedCategory != nil + } } } } From 2a21d2393564fd77e5b2e6e00473023f543de4f1 Mon Sep 17 00:00:00 2001 From: Huong Do Date: Mon, 24 Mar 2025 11:51:35 +0700 Subject: [PATCH 03/10] Display selected category in HAZMAT row --- .../WooShippingHazmatRow.swift | 46 +++++++++++++------ 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Hazmat Section/WooShippingHazmatRow.swift b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Hazmat Section/WooShippingHazmatRow.swift index df502f01894..f9e0c02c80f 100644 --- a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Hazmat Section/WooShippingHazmatRow.swift +++ b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Hazmat Section/WooShippingHazmatRow.swift @@ -18,23 +18,38 @@ struct WooShippingHazmatRow: View { } var body: some View { - Button(action: { - isShowingDetailView = true - }) { - AdaptiveStack { - Text(Localization.hazmatLabel) - .bodyStyle() - Spacer() - Text(isHazardous ? Localization.yes : Localization.no) - .secondaryBodyStyle() - Image(uiImage: .chevronImage) - .secondaryBodyStyle() - .renderedIf(enabled) + VStack { + Button(action: { + isShowingDetailView = true + }) { + AdaptiveStack { + Text(Localization.hazmatLabel) + .bodyStyle() + Spacer() + Text(isHazardous ? Localization.yes : Localization.no) + .secondaryBodyStyle() + Image(uiImage: .chevronImage) + .secondaryBodyStyle() + .renderedIf(enabled) + } + } + .buttonStyle(.plain) + .disabled(!enabled) + + if let category = selectedCategory { + Text(category.localizedName) + .captionStyle() + .frame(maxWidth: .infinity, alignment: .leading) + .multilineTextAlignment(.leading) + .padding(Layout.categoryPadding) + .background( + Color(.quaternarySystemFill) + .clipShape(RoundedRectangle(cornerSize: .init(width: Layout.backgroundRadius, + height: Layout.backgroundRadius))) + ) } - .padding(.vertical, Layout.verticalPadding) } - .buttonStyle(.plain) - .disabled(!enabled) + .padding(.vertical, Layout.verticalPadding) .sheet(isPresented: $isShowingDetailView) { WooShippingHazmatDetailView(selectedCategory: selectedCategory) { selectedCategory in self.selectedCategory = selectedCategory @@ -48,6 +63,7 @@ private extension WooShippingHazmatRow { enum Layout { static let backgroundRadius: CGFloat = 8 static let verticalPadding: CGFloat = 24 + static let categoryPadding: CGFloat = 16 } enum Localization { From 3577da620e60c6497f264a85b896857188e57fea Mon Sep 17 00:00:00 2001 From: Huong Do Date: Mon, 24 Mar 2025 12:33:40 +0700 Subject: [PATCH 04/10] Observe HAZMAT change to send a notice with undo button --- .../WooShippingCreateLabelsViewModel.swift | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModel.swift b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModel.swift index 797836c60e9..298ffdc2a6e 100644 --- a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModel.swift +++ b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModel.swift @@ -22,6 +22,7 @@ final class WooShippingCreateLabelsViewModel: ObservableObject { private var debounceDuration: Double = 1 @Published var hazmatCategory: ShippingLabelHazmatCategory? + @Published var hazmatNotice: Notice? @Published var labelPurchaseErrorNotice: Notice? @@ -228,6 +229,7 @@ final class WooShippingCreateLabelsViewModel: ObservableObject { observeSelectedPackage() observeForLabelRates() observeForCustomsForm() + observeHAZMATChanges() Task { await loadRequiredData() } @@ -569,6 +571,21 @@ private extension WooShippingCreateLabelsViewModel { .store(in: &subscriptions) } + func observeHAZMATChanges() { + $hazmatCategory + .scan((nil, nil)) { (previous: (current: ShippingLabelHazmatCategory?, previous: ShippingLabelHazmatCategory?), + newValue: ShippingLabelHazmatCategory?) in + return (current: newValue, previous: previous.current) + } + .map { [weak self] (newValue, oldValue) in + let noticeTitle = newValue != nil ? Localization.hazmatSet : Localization.hazmatRemoved + return Notice(title: noticeTitle, actionTitle: Localization.undo, actionHandler: { + self?.hazmatCategory = oldValue + }) + } + .assign(to: &$hazmatNotice) + } + func observeForCustomsForm() { $selectedOriginAddress.combineLatest($destinationAddress) .map { (originAddress, destinationAddress) -> Bool in @@ -695,6 +712,24 @@ private extension WooShippingCreateLabelsViewModel { value: "Retry", comment: "Button to retry label purchase when an error occurs") } + + static let hazmatSet = NSLocalizedString( + "wooShipping.createLabels.hazmatSet", + value: "Hazardous materials category set", + comment: "Notice when a hazardous materials category is set on the shipping label creation screen" + ) + + static let hazmatRemoved = NSLocalizedString( + "wooShipping.createLabels.hazmatRemoved", + value: "Remove hazardous materials category", + comment: "Notice when a hazardous materials category is removed on the shipping label creation screen" + ) + + static let undo = NSLocalizedString( + "wooShipping.createLabels.undo", + value: "Undo", + comment: "Button to undo a change on the shipping label creation screen" + ) } } From 3600398144601759f2b06ebec399de54fb9847a2 Mon Sep 17 00:00:00 2001 From: Huong Do Date: Mon, 24 Mar 2025 12:34:03 +0700 Subject: [PATCH 05/10] Display notice and hide bottom sheet when hazmat changes --- .../WooShippingCreateLabelsView.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsView.swift b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsView.swift index 4f486599515..1847d0bcce0 100644 --- a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsView.swift +++ b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsView.swift @@ -60,7 +60,7 @@ struct WooShippingCreateLabelsView: View { } } .safeAreaInset(edge: .bottom) { - if viewModel.state == .ready { + if viewModel.state == .ready && viewModel.hazmatNotice == nil { expandableBottomSheet } } @@ -92,6 +92,7 @@ struct WooShippingCreateLabelsView: View { WooShippingCustomsForm(viewModel: viewModel.customsFormViewModel) } .notice($viewModel.labelPurchaseErrorNotice, autoDismiss: false) + .notice($viewModel.hazmatNotice) } } } From d156040a018cbd2916561ed4862abe91e64f8e48 Mon Sep 17 00:00:00 2001 From: Huong Do Date: Mon, 24 Mar 2025 12:49:40 +0700 Subject: [PATCH 06/10] Drop first event of Hazmat category to avoid showing notice initially --- .../WooShippingCreateLabelsViewModel.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModel.swift b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModel.swift index 298ffdc2a6e..c1185ec1df7 100644 --- a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModel.swift +++ b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModel.swift @@ -573,7 +573,9 @@ private extension WooShippingCreateLabelsViewModel { func observeHAZMATChanges() { $hazmatCategory - .scan((nil, nil)) { (previous: (current: ShippingLabelHazmatCategory?, previous: ShippingLabelHazmatCategory?), + .dropFirst() + .scan((nil, nil)) { (previous: (current: ShippingLabelHazmatCategory?, + previous: ShippingLabelHazmatCategory?), newValue: ShippingLabelHazmatCategory?) in return (current: newValue, previous: previous.current) } From 827e50610ba4bb1bd86a827dd1a7cf4ceb9f1509 Mon Sep 17 00:00:00 2001 From: Huong Do Date: Mon, 24 Mar 2025 12:49:55 +0700 Subject: [PATCH 07/10] Send hazmat category to create label --- .../WooShippingCreateLabelsViewModel.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModel.swift b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModel.swift index c1185ec1df7..a889a3627da 100644 --- a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModel.swift +++ b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModel.swift @@ -650,7 +650,7 @@ private extension WooShippingCreateLabelsViewModel { height: Double(packageData.height) ?? 0, weight: weight, isLetter: WooShippingPackageType(rawValue: packageData.packageType) == .envelope, - hazmatCategory: nil, // Hazmat support will be added in a future milestone + hazmatCategory: hazmatCategory?.rawValue, customsForm: customsForm) } } From 0fbfd320ffa47e547f796d8f14f1d43db49a0dce Mon Sep 17 00:00:00 2001 From: Huong Do Date: Mon, 24 Mar 2025 13:10:07 +0700 Subject: [PATCH 08/10] Add test for hazmat notice --- .../WooShippingCreateLabelsViewModelTests.swift | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModelTests.swift b/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModelTests.swift index 0757519ce04..f17223db588 100644 --- a/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModelTests.swift +++ b/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModelTests.swift @@ -924,6 +924,20 @@ final class WooShippingCreateLabelsViewModelTests: XCTestCase { // Then XCTAssertNotNil(viewModel.addressToEdit) } + + func test_hazmatNotice_is_updated_after_setting_new_hazmat_category() { + // Given + let viewModel = WooShippingCreateLabelsViewModel(order: Order.fake()) + XCTAssertNil(viewModel.hazmatNotice) + + // When + viewModel.hazmatCategory = .class1 + + // Then + waitUntil { + viewModel.hazmatNotice != nil + } + } } private extension WooShippingCreateLabelsViewModelTests { From c8d2672d1faad7e758ade997ac5ed814ee188a40 Mon Sep 17 00:00:00 2001 From: Huong Do Date: Mon, 24 Mar 2025 13:25:58 +0700 Subject: [PATCH 09/10] Add test for purchasing label with hazmat --- ...ooShippingCreateLabelsViewModelTests.swift | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModelTests.swift b/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModelTests.swift index f17223db588..402696c1689 100644 --- a/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModelTests.swift +++ b/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModelTests.swift @@ -743,6 +743,41 @@ final class WooShippingCreateLabelsViewModelTests: XCTestCase { XCTAssertFalse(viewModel.isPurchasingLabel) } + func test_purchaseLabel_sets_hazmat_category_correctly() { + // Given + var encodedHazmat: [String: Any]? + let stores = MockStoresManager(sessionManager: .testingInstance) + let viewModel = WooShippingCreateLabelsViewModel(order: Order.fake().copy(shippingAddress: Address.fake()), + selectedOriginAddress: WooShippingOriginAddress.fake(), + selectedPackage: samplePackageData(), + selectedRate: sampleSelectedRate(), + stores: stores) + stores.whenReceivingAction(ofType: WooShippingAction.self) { action in + switch action { + case let .purchaseShippingLabel(_, _, _, _, package, _, _, _, completion): + encodedHazmat = package.encodedHazmat() + completion(.success(ShippingLabel.fake())) + case let .loadLabelRates(_, _, _, _, packages, completion): + completion(packages, .success([])) + case .loadAccountSettings(_, let completion): + completion(.success(self.settings)) + case .loadPackages, .loadOriginAddresses, .verifyDestinationAddress, .loadConfig: + break + default: + XCTFail("Unexpected action: \(action)") + } + } + + // When + viewModel.hazmatCategory = .class3 + viewModel.purchaseLabel() + + // Then + let shipmentDetails = encodedHazmat?["shipment_0"] as? [String: Any] + XCTAssertEqual(shipmentDetails?["isHazmat"] as? Bool, true) + XCTAssertEqual(shipmentDetails?["category"] as? String, ShippingLabelHazmatCategory.class3.rawValue) + } + func test_selectPackage_sets_selectedPackage_with_package_data() { // Given let viewModel = WooShippingCreateLabelsViewModel(order: Order.fake()) From 4d36e350705427066dcd4f99490b70ce8ac8d01e Mon Sep 17 00:00:00 2001 From: Huong Do Date: Mon, 24 Mar 2025 13:37:33 +0700 Subject: [PATCH 10/10] Remove isHazardous state from Hazmat row --- .../WooShipping Hazmat Section/WooShippingHazmatRow.swift | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Hazmat Section/WooShippingHazmatRow.swift b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Hazmat Section/WooShippingHazmatRow.swift index f9e0c02c80f..5b2419ee9df 100644 --- a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Hazmat Section/WooShippingHazmatRow.swift +++ b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Hazmat Section/WooShippingHazmatRow.swift @@ -4,15 +4,12 @@ struct WooShippingHazmatRow: View { /// Whether the interactions (navigation/setting selection) are enabled. private let enabled: Bool - @State private var isHazardous: Bool - @Binding private var selectedCategory: ShippingLabelHazmatCategory? @State private var isShowingDetailView = false init(selectedCategory: Binding, enabled: Bool) { - isHazardous = selectedCategory.wrappedValue != nil self._selectedCategory = selectedCategory self.enabled = enabled } @@ -26,7 +23,7 @@ struct WooShippingHazmatRow: View { Text(Localization.hazmatLabel) .bodyStyle() Spacer() - Text(isHazardous ? Localization.yes : Localization.no) + Text(selectedCategory != nil ? Localization.yes : Localization.no) .secondaryBodyStyle() Image(uiImage: .chevronImage) .secondaryBodyStyle() @@ -53,7 +50,6 @@ struct WooShippingHazmatRow: View { .sheet(isPresented: $isShowingDetailView) { WooShippingHazmatDetailView(selectedCategory: selectedCategory) { selectedCategory in self.selectedCategory = selectedCategory - isHazardous = selectedCategory != nil } } }