Taehyeon-Kim / Taehyeon-Kim.github.io

The life of recording is unbreakable
https://taehyeon-kim.github.io
MIT License
1 stars 0 forks source link

TCA101: Alert #25

Open Taehyeon-Kim opened 9 months ago

Taehyeon-Kim commented 9 months ago

Feature

import SwiftUI
import ComposableArchitecture

@Reducer
struct SignInFeature {
  struct State: Equatable {
    // ...
    @PresentationState var alert: AlertState<AlertAction>?
    // ...
  }

  enum Action {
    case alert(PresentationAction<AlertAction>)
  }

  enum AlertAction {
    case confirm
  }

  var body: some Reducer<State, Action> {
    Reduce { state, action in
      switch action {
      case .alert:
        state.alert = nil
        return .none

      case let .displayError(error):
        state.alert = AlertState(title: {
          TextState(error.localizedDescription)
        })
        return .none
    }
    .ifLet(\.$alert, action: /Action.alert)
  }
}

View

.alert(store: self.store.scope(state: \.$alert, action: { .alert($0) }))

Links:

https://github.com/pointfreeco/swift-composable-architecture/discussions/2518 https://pointfreeco.github.io/swift-composable-architecture/0.45.0/documentation/composablearchitecture/alertstate/