Fork me on GitHub
#re-frame
<
2017-05-30
>
fabrao02:05:58

Hello all, I dumb question, is there any way to get information from session after http-xhrio call?

udit04:05:46

I have this component which consists retrieves form data from the server and displays it and allows editing as well. So in my app-db has the structure as follows:

{ :form-data {...}
  :editable-form-data {...}}
The editable-form-data stores the UI changes which haven’t been persisted yet. So at the initial load of the component I make the xhr call to server to get the form-data which also needs to be copied over to editable-form-data field. Right now there a db-event which assocs the result of xhr call to both the fields in app-db. This works but it feels hacky to be honest. Are there any suggestions on doing this better?

danielcompton08:05:25

That seems pretty sensible. You could also make use of local state in your reagent components to keep the edited state but that's probably harder

danielneal10:05:16

Is there a kind of dual/equivalent of the effectful handlers for subscriptions that side effect or call out to javascript land. I'm looking at making a subscription for window dimensions in react native

curlyfry11:05:30

danielneal: I don't believe there is anything like that. Is there a reason the dimensions cannot be put into the app-db and have the subscription use that?

mikethompson11:05:59

@U051H1KL1 I dunno about native, but in the browser you'd do this:

(.addEventListner js/window 
    (fn [event] 
       (dispatch [:resize  XXX])))
Then, in the event handler for`:resize`, you'd put the new size details into app-db And then subscribe from app-db as normal. The XXX would have to be replaced with code that got the current size information and added it to the event being dispatched.

danielneal11:05:16

ah cool, yep, I think it will work in native just like that

danielneal11:05:33

there's a similar global event listener I can add

cmal13:05:26

Hi, I found that in projects generated by re-frame-template , there is a (defn ^:export init [] ... ) entry function. May I ask which library need this entry? re-frame reagent or cljsbuild? Thanks!

cmal13:05:42

Ahh… I see, it is called by the js in index.html in app.core.init().

lsnape15:05:22

Does anyone know of a place in the reagent / re-frame docs that warns against out-of-date subscription values being used in event handlers?

lsnape15:05:34

In other words, always use the app-db as a source of truth, not the values that are liable to change during the lifecycle of a reagent component.

jfntn15:05:55

Using re-frame.test/run-test-sync I’m getting ERROR You can't call dispatch-sync within an event handler. for handlers that return :dispatch fxs. Is there a workaround that doesn’t involve running async?

Doug Kirk16:05:48

If an interceptor places a map into the co/effect part of the context, does that persist to the next dispatch call, or is the db the only thing that persists?

fabrao17:05:13

Hello all, I´m doing this

(reg-event-fx
 :processar-login
 (fn
   [{:keys [db]} [_ usuario senha]]
   {:db (assoc db :opcoes-dialogo {:dialogo-ativo :carregando
                                   :titulo "AGUARDE !!!"
                                   :tipo :normal
                                   :mensagem "Validando credenciais ..."
                                   })
    :http-xhrio {:method :get
                 :uri "/login"
                 :params {:usuario usuario :senha senha}
                 :format          (ajax/url-request-format)
                 :response-format (ajax/json-response-format {:keywords? true})  
                 :on-success [:login-ok]
                 :on-failure [:login-erro]}
    }))
Then in /login I´m returning a JSON that contains ok or error for user and password validation. When ok, I set into session the value for authenticate 'ok' in response from server. Is there any way to see session :authenticate 'ok' in :on-sucess callback?

negaduck18:05:44

@fabrao, I’m not sure I understand the question. Do you want to read the json? What about the following?

(re-frame/reg-event-db
 :login-ok
 (fn [db [_ result]]
   ;; do whatever you want with the result here
   ;; like (assoc db :authenticate (:authenticate result))
   ))

fabrao18:05:35

I want to get the session value passed from server

bradleesand18:05:34

that link above (the right one) should help you. basically, the result is passed into the :on-success handler as the last parameter

negaduck18:05:23

the question is probably about reading a response header when :response-format is set to ajax/json-response-format

danielgrosse21:05:13

I asked a question in off-topic which is related to re-frame, so I link it here too. https://clojurians.slack.com/archives/C03RZGPG3/p1496177736706470

mikethompson22:05:17

@jfntn we're looking into it. Stu will create a ticket

mikethompson22:05:56

@kirked context is created fresh each event

negaduck22:05:41

@fabrao, apparently, there is no easy answer, look into cljs-ajax interceptors: https://github.com/JulianBirch/cljs-ajax/blob/master/docs/interceptors.md

jfntn22:05:13

@mikethompson cool, thanks for the update, running async works for now

mikethompson23:05:35

@jfntn there's work currently happening on a branch, which we hope to release in the next couple of days