joshp

joshp

Passing a struct to @assigns for a LiveComponent, good idea or not?

I’m updating an app from 1.5 to 1.7 and changing to components/heex, and it’s led me to a question about what the Right Way™ to do something is… Here is the current state of things:

I have a component that generates SVG code representing a gauge. Think speedometer 0-60 kinda thing. This object is updated every 15 seconds with fresh data and redraws.

Currently the way I have things organized to have a module in AppWeb/views/gauge_arc.ex, which contains a struct, a type, and helper functions, including a “build_from_values()” function which takes the numerical values, and populates the remaining fields of the struct with values like “svg_bg_path_string” and “svg_fill_path_string” and “main_label_text”.

In my LiveView I fetch the data, create a new %GaugeArc{} with the basic values, pass that to GaugeArc.build_from_values() to fill in the computed values, and store the entire %GaugeArc{} into the assigns in a variable indicating what the gauge represents. (my page has several GaugeArcs on it)
The LiveView then calls a LiveComponent for each arc, passing in the correct struct from assigns, and the component pulls out the values it needs from the computed fields via @gaugeArc.svg_bg_path_string and the like.

All in all, in phx1.5 land, it’s actually been a pretty reasonable way to move a bunch of data into a liveview, from a code organization perspective.

My questions though:

  1. I feel like passing a 30-field struct into @assigns for a component is probably… not recommended? The final LiveComponent only uses 13 of them, 3 of which should never change for the life of the component. My assumption is that the correct way to do this in 1.7 would be to create a component with 13 individual attrs, but this still leaves the problem of how to handle getting that data into the assigns in the LiveView itself. 13 attrs each for 4 or more individual arcs. I could have mount/1 create @arc1_val1, @arc1_val2, … variables, but that seems inelegant. Is that the recommended solution for something like this, or was I on the right track with a custom struct to pass into @assigns?
  2. In 1.7 LiveComponent world, where is the preferred place for the code for these sorts of custom structs or helpers? They are firmly display-only, and generate HTML/SVG code, so I felt that they belonged in AppWeb, not over in App, but I’m not quite sure where to put them in the directory structure. Putting the struct/type/functions directly inside the app_web/components/gauge_arc.ex didn’t seem right. A “helpers” directory perhaps? Curious to see what others do for this.

It’s worth noting that this is a very low-impact project, it will never have more than maybe 10 concurrent users, so sacrificing some performance was never a problem. I’m just trying to think through the Right Way™ so that if I ever run into a bigger project with similar needs I’ve got some ideas, and a better understanding of how te work with change tracking rather than against it. :slight_smile:

Most Liked

sodapopcan

sodapopcan

Naw, definitely a case for a live component! I asked because I wasn’t sure whether you really need to keep all your state in the LiveView itself or not. If you did then a live component might not be needed.

Where Next?

Popular in Questions Top

skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement