Fork me on GitHub
#re-frame
<
2020-03-25
>
Ramon Rios11:03:22

Hello everyone! I'm trying to, in a click of a span, replace the field from span to a input.

(defn input-text-brio-nr [brio-nr]
  [:input {:type "text"
           :value brio-nr
           :on-change #(reset! brio-nr (-> % .-target .-value))}])

(defn brio-nr-formatter
  [brio-nr]
  [:span  [:a {:on-click #(input-text-brio-nr brio-nr)} brio-nr]])
On my first attempt, it does not work as expected. So have someone tried to do this? I'm using the brio-nr-formatter on a re-frame datatable
(defn show-table []
  [t/table
   :ogm-table
   [:ogm/search-results]
   [(t/column {:key [:customer]
               :label (ml/tx "Customer")})
    (t/column {:key [:policy]
               :label (ml/tx "Policy")})
    (t/column {:key [:term]
               :label (ml/tx "Term")})
    (t/column {:key [:settlement]
               :label (ml/tx "Bruto")})
    (t/column {:key [:brio-nr]
               :label (ml/tx "Brio Nr")
               :render-fn brio-nr-formatter})
    (t/column {:key [:brio-nr]
               :label (ml/tx "Verified")})]])

Lu14:03:23

Why not having an if in your view to either display the input or the span based on some arbitrary value you set in the state. Something like edit-mode?

☝️ 8
Ramon Rios16:03:40

I ended up doing that. Thanks

Ramon Rios16:03:11

Now, what i'm trying to do it's the reverse. When i change the focus on the field, it will back to be a span

Ramon Rios17:03:46

I did that, but it's doing in all of the columns and not in only one

Lu09:03:13

That’s because when you save the edit-mode? Value in your state, the path you choose in your map has to be unique. You can save it for example under {:edit-mode? {“unique-id” true}}

Lu09:03:32

So when your state changes and the table is rerendered, only one single cell will effectively become an input and not all of them :)

Ramon Rios09:03:44

Do you know how can i send the id of the object or more data into the render function?

Ramon Rios09:03:58

I was looking on documentation yesterday trying to do that

Ramon Rios09:03:12

And also, a huge thanks for the tips

Lu09:03:14

Lemme read up the docs of the lib you’re using and I’ll get back to you later :)

Ramon Rios09:03:05

thanks a lot

Lu09:03:52

Np 😄😄

Ramon Rios09:03:27

(defn input-text-brio-nr [brio-nr]
  [:input {:type "text"
           :value brio-nr
           :on-blur #(rf/dispatch [:ogm/set-edit-brio-nr false])}])

(defn brio-nr-formatter
  [brio-nr cust]
  (let [editing (rf/subscribe [:ogm/edit-brio-nr])
        cid (:customer-id cust)]
    (println "this is cust" cid)
    (if @editing
      [input-text-brio-nr brio-nr]
      [:span [:a {:on-click #(rf/dispatch [:ogm/set-edit-brio-nr true])} brio-nr]])))

Ramon Rios09:03:11

I got the id, just thinking in how implement your idea of unique way

Lu10:03:21

You can pass cust to your dispatch function and save the true boolean under for example:

(assoc-in db [:edit-mode? cust] true)

Lu10:03:17

and in your subscription you get the current mode out like this: (get-in db [:edit-mode? cust])

Lu10:03:52

Also, pass cust down to the input as well. Its on-blur event will take cust and will do:

(update db :edit-mode? dissoc cust)

Lu10:03:23

And this should work for all the table rows independently 🙂

Ramon Rios10:03:23

Thanks, i'll give a try

Lu14:03:49

So yeah you can simply call in your :render-fn a wrapper that returns your span or input depending on a re-frame subscription

Oz15:03:36

Hello there, I'm trying to do some Web-Socket communication, and there seems to be a bottleneck somewhere. Ideally, I would want to send 1000s of requests and get 1000s of responses within a few seconds. Google says it should be possible with a million so thousands are probably not science fiction. Now, here is how it works: 1. Each requests starts by dispatching an event that triggers a :ws-send effect, 2. When a response comes through the socket, it triggers a :proscess-response event. 3. This :process-response event converts the response to edn and triggers another event :ws-command

Oz15:03:10

4. :ws-command finds out from the data which db event to dispatch. 5. and this final event updates the app-db with the data we got from the response.

Oz15:03:40

Now, this is maybe a little rube-goldebergesque, but I'm not sure what influences performance the most.

p-himik16:03:56

Why not just profile it in the browser?

Oz16:03:08

good call 🙂

coby18:03:14

How do folks generally organize their namespaces as a re-frame codebase grows? I have all my view code under a my-project.components.* umbrella of namespaces, but wondering about other stuff. The re-frame template defines my-project.subs and my-project.events, but as it grows I think I will need to break down each into features/component groups, while still keeping the event/subscription keywords in the higher-level namespaces, like this:

(ns my-project.events
 (:require
  [my-project.events.locations :as locations]
  [my-project.events.providers :as providers]
  ;; etc.
  [re-frame.core :as r]))

(r/reg-event-fx ::search-locations! locations/search!)
(r/reg-event-fx ::search-providers! providers/search!)
This way I maintain a single entry point each for components into events/subs. Thoughts?

lilactown18:03:30

I like having subs and events in one namespace related to the feature I’m working on

8
coby18:03:07

do you mean one combined namespace for both subs and events?

coby18:03:40

(ns my-project.locations
 (:require
  [re-frame.core :as r]))

(defn search! [] ,,, )
(defn update-results [] ,,, )

(r/reg-event-fx ::search! search!)
(r/reg-sub ::search-results update-results)
so something like that?

coby18:03:17

I could see how that could be nicer...it's already a bit of a pain jumping between views/subs/events

lilactown18:03:03

yeah exactly

lilactown18:03:22

often, if I want to change the way I’m storing something in the app-db, I’ll need to update both events and subs

💯 4
lilactown18:03:40

it’s nice if they’re located together

✔️ 4