0

How do we dispatch an async action with ReSwift with parameters?

I have created this async action:

func searchItems(state: AppState, store: Store<AppState>) -> Action? {
    var items = [Artwork]()
    HttpHelper.post(url: serverApi.search.rawValue, params: ["term": ""]) { data in
        if let data = data as? [[String: Any]] {
            for row in data {
                items.append(Artwork(data: row))
            }
            DispatchQueue.main.async {
                store.dispatch(UpdateItems(items: items))
            }
        }
    }
    return nil
}

To dispatch an action, I used

store.dispatch(searchItems)

Yet I do not know how to attach the search terms with the action.

Julian Gong
  • 360
  • 2
  • 8

1 Answers1

0

Figured it out. Here is an example:

import ReSwift

struct GetShowroom: Action {

    let code: String

    init(code: String) {
        self.code = code
        getIt()
    }

    func getIt() {
        store.dispatch(UpdateLoading(loading: .Showroom(code: self.code)))
        HttpHelper.post(to: .getShowroom, params: ["code": self.code]) { res in
            if let data = res as? [String: Any] {
                store.dispatch(UpdateShowroom(showroom: Showroom(code: self.code, data: data)))
            }
        }
    }

}
Julian Gong
  • 360
  • 2
  • 8