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

[MBL-1328] Pledge Button Active/Inactive States #2019

Merged
merged 5 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 54 additions & 6 deletions Library/ViewModels/PostCampaignCheckoutViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ public class PostCampaignCheckoutViewModel: PostCampaignCheckoutViewModelType,
.skipNil()

let context = initialData.map(\.context)
let project = initialData.map(\.project)
let checkoutId = initialData.map(\.checkoutId)
let baseReward = initialData.map(\.rewards).map(\.first)
let baseReward = initialData.map(\.rewards).map(\.first).skipNil()
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the case where the first reward is nil? I thought we always had at least one reward (i.e. the no-reward award.)


let configurePaymentMethodsData = Signal.merge(
initialData,
Expand All @@ -109,14 +110,40 @@ public class PostCampaignCheckoutViewModel: PostCampaignCheckoutViewModelType,
.map { _ in AppEnvironment.current.currentUser }
.map(isNotNil)

let notChangingPaymentMethod = context.map { context in
context.isUpdating && context != .changePaymentMethod
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you change the payment method on a post-campaign pledge? I thought they were immutable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah yes thank you!

}
.filter(isTrue)

let paymentMethodSelected = self.creditCardSelectedProperty.signal.skipNil()
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Since this gets passed into the CTA to indicate whether or not it is enabled, does the CTA disable ApplePay as well? Technically we could still run an ApplePay transaction before the payment method is selected (since in that case, you select a card via the ApplePay dialog), but I also don't think that's a big deal.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I set this up so that it doesn't impact apple pay for this reason. Which, is what we want here.


let paymentMethodChangedAndValid = Signal.merge(
notChangingPaymentMethod.mapConst(false),
Signal.combineLatest(
project,
baseReward,
paymentMethodSelected.map { (source: PaymentSourceSelected, _, _) in source },
context
)
.map(paymentMethodValid)
)

let isEnabled = Signal.merge(
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: This could have a more descriptive name like pledgeButtonEnabled.

self.viewDidLoadProperty.signal.mapConst(false)
.take(until: paymentMethodChangedAndValid.ignoreValues()),
paymentMethodChangedAndValid
)
.skipRepeats()

self.configurePledgeViewCTAContainerView = Signal.combineLatest(
isLoggedIn,
isEnabled,
context
)
.map { isLoggedIn, context in
.map { isLoggedIn, isEnabled, context in
PledgeViewCTAContainerViewData(
isLoggedIn: isLoggedIn,
isEnabled: true, // Pledge button never needs to be disabled on checkout page.
Copy link
Contributor

Choose a reason for hiding this comment

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

Well that's a convenient comment πŸ˜†

isEnabled: isEnabled,
context: context,
willRetryPaymentMethod: false // Only retry in the `fixPaymentMethod` context.
)
Expand Down Expand Up @@ -394,9 +421,7 @@ public class PostCampaignCheckoutViewModel: PostCampaignCheckoutViewModelType,

let thanksPageData = Signal.combineLatest(initialData, baseReward)
.map { initialData, baseReward -> ThanksPageData? in
guard let reward = baseReward else { return nil }

return (initialData.project, reward, nil, initialData.total)
(initialData.project, baseReward, nil, initialData.total)
}

self.checkoutComplete = thanksPageData.skipNil()
Expand Down Expand Up @@ -514,3 +539,26 @@ public class PostCampaignCheckoutViewModel: PostCampaignCheckoutViewModelType,
public var inputs: PostCampaignCheckoutViewModelInputs { return self }
public var outputs: PostCampaignCheckoutViewModelOutputs { return self }
}

private func paymentMethodValid(
project: Project,
reward: Reward,
paymentSource: PaymentSourceSelected,
context: PledgeViewContext
) -> Bool {
guard
let backedPaymentSourceId = project.personalization.backing?.paymentSource?.id,
Copy link
Contributor

Choose a reason for hiding this comment

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

Same question here, I thought these were immutable?

context.isUpdating,
userIsBacking(reward: reward, inProject: project)
else {
return true
Copy link
Contributor

Choose a reason for hiding this comment

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

Won't this always return true if you haven't backed this project already?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh. yeah I guess that it true. I keep forgetting we won't be updating late pledges

}

if project.personalization.backing?.status == .errored {
return true
} else if backedPaymentSourceId != paymentSource.savedCreditCardId {
return true
}

return false
}
60 changes: 60 additions & 0 deletions Library/ViewModels/PostCampaignCheckoutViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,24 @@ final class PostCampaignCheckoutViewModelTests: TestCase {
fileprivate let checkoutComplete = TestObserver<ThanksPageData, Never>()
fileprivate let processingViewIsHidden = TestObserver<Bool, Never>()
fileprivate let validateCheckoutSuccess = TestObserver<PaymentSourceValidation, Never>()

private let configurePledgeViewCTAContainerViewIsLoggedIn = TestObserver<Bool, Never>()
private let configurePledgeViewCTAContainerViewIsEnabled = TestObserver<Bool, Never>()
private let configurePledgeViewCTAContainerViewContext = TestObserver<PledgeViewContext, Never>()

override func setUp() {
super.setUp()
self.vm.goToApplePayPaymentAuthorization.observe(self.goToApplePayPaymentAuthorization.observer)
self.vm.checkoutComplete.observe(self.checkoutComplete.observer)
self.vm.processingViewIsHidden.observe(self.processingViewIsHidden.observer)
self.vm.validateCheckoutSuccess.observe(self.validateCheckoutSuccess.observer)

self.vm.outputs.configurePledgeViewCTAContainerView.map { $0.0 }
.observe(self.configurePledgeViewCTAContainerViewIsLoggedIn.observer)
self.vm.outputs.configurePledgeViewCTAContainerView.map { $0.1 }
.observe(self.configurePledgeViewCTAContainerViewIsEnabled.observer)
self.vm.outputs.configurePledgeViewCTAContainerView.map { $0.2 }
.observe(self.configurePledgeViewCTAContainerViewContext.observer)
}

func testApplePayAuthorization_noReward_isCorrect() {
Expand Down Expand Up @@ -385,8 +396,14 @@ final class PostCampaignCheckoutViewModelTests: TestCase {
self.vm.viewDidLoad()

let paymentSource = PaymentSourceSelected.paymentIntentClientSecret("123")

self.vm.inputs
.creditCardSelected(source: paymentSource, paymentMethodId: "123", isNewPaymentMethod: true)

self.configurePledgeViewCTAContainerViewIsLoggedIn.assertValues([false, false])
self.configurePledgeViewCTAContainerViewIsEnabled.assertValues([false, true])
self.configurePledgeViewCTAContainerViewContext.assertValues([.latePledge, .latePledge])

self.vm.inputs.submitButtonTapped()

self.processingViewIsHidden.assertLastValue(false)
Expand All @@ -400,6 +417,7 @@ final class PostCampaignCheckoutViewModelTests: TestCase {

self.checkoutComplete.assertDidEmitValue()
self.processingViewIsHidden.assertLastValue(true)

}
}

Expand Down Expand Up @@ -431,6 +449,11 @@ final class PostCampaignCheckoutViewModelTests: TestCase {
let paymentSource = PaymentSourceSelected.paymentIntentClientSecret("123")
self.vm.inputs
.creditCardSelected(source: paymentSource, paymentMethodId: "123", isNewPaymentMethod: true)

self.configurePledgeViewCTAContainerViewIsLoggedIn.assertValues([false, false])
self.configurePledgeViewCTAContainerViewIsEnabled.assertValues([false, true])
self.configurePledgeViewCTAContainerViewContext.assertValues([.latePledge, .latePledge])

self.vm.inputs.submitButtonTapped()

self.processingViewIsHidden.assertLastValue(false)
Expand Down Expand Up @@ -471,6 +494,11 @@ final class PostCampaignCheckoutViewModelTests: TestCase {
let paymentSource = PaymentSourceSelected.paymentIntentClientSecret("123")
self.vm.inputs
.creditCardSelected(source: paymentSource, paymentMethodId: "123", isNewPaymentMethod: true)

self.configurePledgeViewCTAContainerViewIsLoggedIn.assertValues([false, false])
self.configurePledgeViewCTAContainerViewIsEnabled.assertValues([false, true])
self.configurePledgeViewCTAContainerViewContext.assertValues([.latePledge, .latePledge])

self.vm.inputs.submitButtonTapped()

self.processingViewIsHidden.assertLastValue(false)
Expand All @@ -485,4 +513,36 @@ final class PostCampaignCheckoutViewModelTests: TestCase {
self.processingViewIsHidden.assertLastValue(true)
}
}

func testPledgeViewCTA_LoggedIn_State() {
let mockService = MockService(serverConfig: ServerConfig.staging)

withEnvironment(apiService: mockService, currentUser: .template) {
let project = Project.cosmicSurgery
let reward = Reward.noReward |> Reward.lens.minimum .~ 5

let data = PostCampaignCheckoutData(
project: project,
rewards: [reward],
selectedQuantities: [:],
bonusAmount: 0,
total: 5,
shipping: nil,
refTag: nil,
context: .latePledge,
checkoutId: "0"
)

self.vm.inputs.configure(with: data)
self.vm.inputs.viewDidLoad()

let paymentSource = PaymentSourceSelected.paymentIntentClientSecret("123")
self.vm.inputs
.creditCardSelected(source: paymentSource, paymentMethodId: "123", isNewPaymentMethod: true)

self.configurePledgeViewCTAContainerViewIsLoggedIn.assertValues([true, true])
self.configurePledgeViewCTAContainerViewIsEnabled.assertValues([false, true])
self.configurePledgeViewCTAContainerViewContext.assertValues([.latePledge, .latePledge])
}
}
}