Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Shipping labels] Set selected package from "Add Package" flow #14558

Merged
merged 3 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ extension ShippingLabelPackageSelected: Encodable {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(id, forKey: .id)
try container.encode(boxID, forKey: .boxID)
try container.encode(boxID.isEmpty ? "0" : boxID, forKey: .boxID)
try container.encode(length, forKey: .length)
try container.encode(width, forKey: .width)
try container.encode(height, forKey: .height)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ final class WooShippingAddCustomPackageViewModel: ObservableObject {
}

private var packageDataFromCurrentData: WooShippingPackageDataRepresentable {
return WooShippingPackageData(id: UUID().uuidString,
return WooShippingPackageData(id: packageTemplateName,
name: packageTemplateName,
length: fieldValues[.length] ?? "",
width: fieldValues[.width] ?? "",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import SwiftUI

struct WooShippingPackageAndRatePlaceholder: View {
/// Action to perform when a package is selected.
let onSelectPackage: (WooShippingPackageDataRepresentable) -> Void

@State private var showAddPackage: Bool = false

var body: some View {
VStack(spacing: .zero) {
Button {
Expand All @@ -25,7 +29,7 @@ struct WooShippingPackageAndRatePlaceholder: View {
.roundedBorder(cornerRadius: Layout.borderCornerRadius, lineColor: Color(.border), lineWidth: Layout.borderLineWidth, dashed: true)
.sheet(isPresented: $showAddPackage) {
WooShippingAddPackageView { packageData in
// TODO: use packageData
onSelectPackage(packageData)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy that we can finally remove this TODO 🥳

showAddPackage = false
}
}
Expand Down Expand Up @@ -58,6 +62,6 @@ private extension WooShippingPackageAndRatePlaceholder {
}

#Preview {
WooShippingPackageAndRatePlaceholder()
WooShippingPackageAndRatePlaceholder(onSelectPackage: { _ in })
.padding()
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct WooShippingCreateLabelsView: View {
WooShippingServiceView(viewModel: shippingService)
.padding(.horizontal, -16)
} else {
WooShippingPackageAndRatePlaceholder()
WooShippingPackageAndRatePlaceholder(onSelectPackage: viewModel.selectPackage)
}
}
.padding(16)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,24 @@ final class WooShippingCreateLabelsViewModel: ObservableObject {
self.stores = stores
}

/// Handles package selection for the shipping label.
/// Selecting a package also refreshes the available rates for the shipping service.
func selectPackage(_ packageData: WooShippingPackageDataRepresentable) {
// For now we support purchasing labels in a single package for a single shipment.
// In future milestones we can handle an array of packages with unique IDs for each shipment.
let package = ShippingLabelPackageSelected(id: "shipment_0",
boxID: packageData.id,
length: Double(packageData.length) ?? 0,
width: Double(packageData.width) ?? 0,
height: Double(packageData.height) ?? 0,
weight: itemsDataSource.items.map(\.weight).reduce(0, +) + (Double(packageData.weight) ?? 0),
isLetter: WooShippingPackageType(rawValue: packageData.packageType) == .envelope,
hazmatCategory: nil, // Hazmat support will be added in a future milestone
customsForm: nil) // Customs form support will be added in a future milestone
selectedPackage = package
shippingService?.loadLabelRates(for: package)
}

/// Purchases a shipping label with the provided label details and settings.
func purchaseLabel() {
guard isPurchaseButtonEnabled, !isPurchasingLabel, let originSiteAddress, let destinationAddress, let selectedPackage, let selectedRate else {
Expand Down Expand Up @@ -175,13 +193,6 @@ final class WooShippingCreateLabelsViewModel: ObservableObject {

// MARK: Utils
private extension WooShippingCreateLabelsViewModel {
/// Handles changes to the selected package.
/// Selecting a package also refreshes the available rates for the shipping service.
func handleNewSelectedPackage(_ selectedPackage: ShippingLabelPackageSelected) {
self.selectedPackage = selectedPackage
shippingService?.loadLabelRates(for: selectedPackage)
}

/// Provides the formatted label and amount for a shipping rate, based on the provided base rate.
func formatShippingRate(name: String, rate: Double, basedOn baseRate: Double? = nil) -> (title: String, amount: String) {
let amount = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,34 @@ final class WooShippingCreateLabelsViewModelTests: XCTestCase {
// Check isPurchaseLabel is false after purchase
XCTAssertFalse(viewModel.isPurchasingLabel)
}

func test_selectPackage_sets_selectedPackage_with_package_data() {
// Given
let packageData = WooShippingPackageData(id: "small_flat_box",
name: "Small Flat Rate Box",
length: "21.91",
width: "13.65",
height: "4.13",
dimensionsUnit: "cm",
weight: "0",
weightUnit: "kg",
source: .predefined("usps"),
packageType: "box")
let viewModel = WooShippingCreateLabelsViewModel(order: Order.fake())

// When
viewModel.selectPackage(packageData)

// Then
XCTAssertNotNil(viewModel.selectedPackage)
XCTAssertEqual(viewModel.selectedPackage?.id, "shipment_0")
XCTAssertEqual(viewModel.selectedPackage?.boxID, "small_flat_box")
XCTAssertEqual(viewModel.selectedPackage?.length, 21.91)
XCTAssertEqual(viewModel.selectedPackage?.width, 13.65)
XCTAssertEqual(viewModel.selectedPackage?.height, 4.13)
XCTAssertEqual(viewModel.selectedPackage?.weight, 0)
XCTAssertEqual(viewModel.selectedPackage?.isLetter, false)
}
}

private extension WooShippingCreateLabelsViewModelTests {
Expand Down