;; BEGIN SAMPLE

(ns statecharts-samples.rf-integration-service
  (:require [statecharts.core :as fsm :refer [assign]]
            [re-frame.core :as rf]
            [statecharts.integrations.re-frame :as fsm.rf]))

(def friends-path [(rf/path :friends)])

(declare friends-service)

(rf/reg-event-fx
 :friends/init
 (fn []
  ( 1 )
   nil))

(defn load-friends []
  (send-http-request
   {:uri "/api/get-friends.json"
    :method :get
    :on-success #(fsm/send friends-service {:type :success-load :data %})
    :on-failure #(fsm/send friends-service {:type :fail-load :data %})}))

(defn on-friends-loaded [state {:keys [data]}]
  (assoc state :friends (:friends data)))

(defn on-friends-load-failed [state {:keys [data]}]
  (assoc state :error (:status data)))

(def friends-machine
  (fsm/machine
   {:id      :friends
    :initial :loading
    :states
    {:loading     {:entry load-friends
                   :on    {:success-load {:actions (assign on-friends-loaded)
                                          :target  :loaded}
                           :fail-load    {:actions (assign on-friends-load-failed)
                                          :target  :load-failed}}}
     :loaded      {}
     :load-failed {}}}))

(defonce friends-service (fsm/service friends-machine))

  ( 2 )

(defn ^:dev/after-load after-reload []
  ( 3 )

;; END SAMPLE

 BEGIN SAMPLE
 END SAMPLE

(ns statecharts-samples.rf-integration-service
  (:require [statecharts.core :as fsm :refer [assign]]
            [re-frame.core :as rf]
            [statecharts.integrations.re-frame :as fsm.rf]))

(def friends-path [(rf/path :friends)])

(declare friends-service)

(rf/reg-event-fx
 :friends/init
 (fn []
  ( 1 )
   nil))

(defn load-friends []
  (send-http-request
   {:uri "/api/get-friends.json"
    :method :get
    :on-success #(fsm/send friends-service {:type :success-load :data %})
    :on-failure #(fsm/send friends-service {:type :fail-load :data %})}))

(defn on-friends-loaded [state {:keys [data]}]
  (assoc state :friends (:friends data)))

(defn on-friends-load-failed [state {:keys [data]}]
  (assoc state :error (:status data)))

(def friends-machine
  (fsm/machine
   {:id      :friends
    :initial :loading
    :states
    {:loading     {:entry load-friends
                   :on    {:success-load {:actions (assign on-friends-loaded)
                                          :target  :loaded}
                           :fail-load    {:actions (assign on-friends-load-failed)
                                          :target  :load-failed}}}
     :loaded      {}
     :load-failed {}}}))

(defonce friends-service (fsm/service friends-machine))

  ( 2 )

(defn ^:dev/after-load after-reload []
  ( 3 )

