@@ -5,7 +5,7 @@ import Dispatch
55
66public struct TwoFactor : ReducerProtocol , Sendable {
77 public struct State : Equatable {
8- @PresentationState public var alert : AlertState < Never > ?
8+ @PresentationState public var alert : AlertState < Action . Alert > ?
99 @BindingState public var code = " "
1010 public var isFormValid = false
1111 public var isTwoFactorRequestInFlight = false
@@ -16,29 +16,44 @@ public struct TwoFactor: ReducerProtocol, Sendable {
1616 }
1717 }
1818
19- public enum Action : BindableAction , Equatable {
20- case alert( PresentationAction < Never > )
21- case binding( BindingAction < State > )
22- case submitButtonTapped
19+ public enum Action : Equatable {
20+ case alert( PresentationAction < Alert > )
2321 case twoFactorResponse( TaskResult < AuthenticationResponse > )
22+ case view( View )
23+
24+ public enum Alert : Equatable { }
25+
26+ public enum View : BindableAction , Equatable {
27+ case binding( BindingAction < State > )
28+ case submitButtonTapped
29+ }
2430 }
2531
2632 @Dependency ( \. authenticationClient) var authenticationClient
2733
2834 public init ( ) { }
2935
3036 public var body : some ReducerProtocol < State , Action > {
31- BindingReducer ( )
37+ BindingReducer ( action : / Action . view )
3238 Reduce { state, action in
3339 switch action {
3440 case . alert:
3541 return . none
3642
37- case . binding:
43+ case let . twoFactorResponse( . failure( error) ) :
44+ state. alert = AlertState { TextState ( error. localizedDescription) }
45+ state. isTwoFactorRequestInFlight = false
46+ return . none
47+
48+ case . twoFactorResponse( . success) :
49+ state. isTwoFactorRequestInFlight = false
50+ return . none
51+
52+ case . view( . binding) :
3853 state. isFormValid = state. code. count >= 4
3954 return . none
4055
41- case . submitButtonTapped:
56+ case . view ( . submitButtonTapped) :
4257 state. isTwoFactorRequestInFlight = true
4358 return . task { [ code = state. code, token = state. token] in
4459 . twoFactorResponse(
@@ -47,15 +62,6 @@ public struct TwoFactor: ReducerProtocol, Sendable {
4762 }
4863 )
4964 }
50-
51- case let . twoFactorResponse( . failure( error) ) :
52- state. alert = AlertState { TextState ( error. localizedDescription) }
53- state. isTwoFactorRequestInFlight = false
54- return . none
55-
56- case . twoFactorResponse( . success) :
57- state. isTwoFactorRequestInFlight = false
58- return . none
5965 }
6066 }
6167 . ifLet ( \. $alert, action: / Action. alert)
0 commit comments