Skip to content
Snippets Groups Projects
Commit 2bd54573 authored by Dariusz Rybicki's avatar Dariusz Rybicki
Browse files

Add error alert to MyContactFeature

parent 97727f6f
No related branches found
No related tags found
2 merge requests!102Release 1.0.0,!98Messenger example - register, confirm, and unregister user facts
import ComposableArchitecture
extension AlertState {
public static func error(_ message: String) -> AlertState<MyContactAction> {
AlertState<MyContactAction>(
title: TextState("Error"),
message: TextState(message),
buttons: []
)
}
}
...@@ -16,18 +16,21 @@ public struct MyContactState: Equatable { ...@@ -16,18 +16,21 @@ public struct MyContactState: Equatable {
contact: XXModels.Contact? = nil, contact: XXModels.Contact? = nil,
focusedField: Field? = nil, focusedField: Field? = nil,
email: String = "", email: String = "",
phone: String = "" phone: String = "",
alert: AlertState<MyContactAction>? = nil
) { ) {
self.contact = contact self.contact = contact
self.focusedField = focusedField self.focusedField = focusedField
self.email = email self.email = email
self.phone = phone self.phone = phone
self.alert = alert
} }
public var contact: XXModels.Contact? public var contact: XXModels.Contact?
@BindableState public var focusedField: Field? @BindableState public var focusedField: Field?
@BindableState public var email: String @BindableState public var email: String
@BindableState public var phone: String @BindableState public var phone: String
public var alert: AlertState<MyContactAction>?
} }
public enum MyContactAction: Equatable, BindableAction { public enum MyContactAction: Equatable, BindableAction {
...@@ -38,6 +41,8 @@ public enum MyContactAction: Equatable, BindableAction { ...@@ -38,6 +41,8 @@ public enum MyContactAction: Equatable, BindableAction {
case registerPhoneTapped case registerPhoneTapped
case unregisterPhoneTapped case unregisterPhoneTapped
case loadFactsTapped case loadFactsTapped
case didFail(String)
case alertDismissed
case binding(BindingAction<MyContactState>) case binding(BindingAction<MyContactState>)
} }
...@@ -108,6 +113,14 @@ public let myContactReducer = Reducer<MyContactState, MyContactAction, MyContact ...@@ -108,6 +113,14 @@ public let myContactReducer = Reducer<MyContactState, MyContactAction, MyContact
case .loadFactsTapped: case .loadFactsTapped:
return .none return .none
case .didFail(let failure):
state.alert = .error(failure)
return .none
case .alertDismissed:
state.alert = nil
return .none
case .binding(_): case .binding(_):
return .none return .none
} }
......
...@@ -115,6 +115,7 @@ public struct MyContactView: View { ...@@ -115,6 +115,7 @@ public struct MyContactView: View {
.task { viewStore.send(.start) } .task { viewStore.send(.start) }
.onChange(of: viewStore.focusedField) { focusedField = $0 } .onChange(of: viewStore.focusedField) { focusedField = $0 }
.onChange(of: focusedField) { viewStore.send(.set(\.$focusedField, $0)) } .onChange(of: focusedField) { viewStore.send(.set(\.$focusedField, $0)) }
.alert(store.scope(state: \.alert), dismiss: .alertDismissed)
} }
} }
} }
......
...@@ -119,4 +119,22 @@ final class MyContactFeatureTests: XCTestCase { ...@@ -119,4 +119,22 @@ final class MyContactFeatureTests: XCTestCase {
store.send(.loadFactsTapped) store.send(.loadFactsTapped)
} }
func testErrorAlert() {
let store = TestStore(
initialState: MyContactState(),
reducer: myContactReducer,
environment: .unimplemented
)
let failure = "Something went wrong"
store.send(.didFail(failure)) {
$0.alert = .error(failure)
}
store.send(.alertDismissed) {
$0.alert = nil
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment