neurodynamic

neurodynamic

LiveView architecture decisions: LiveViews vs LiveComponents and where and how to store data?

I’m working on a social network with posts and comments using LiveView, and I’m trying to figure out how and when to use the different constructs LiveView provides. There are a few different pages where you will see lists of posts: a main feed with friends’ posts, a user profile page with that user’s posts, and on the show page for a single post. Posts behave similarly in all these cases (i.e. they can be viewed and edited on all three and on the feed and user profile pages new posts can be created).

Here’s a diagram of the rough relationships of the different parts I’m trying to organize:

To me, it seems logical for each blue box that isn’t the top-level LiveView to be its own LiveComponent because all of them will be used in more than one place, but this presents some questions:

How does the comments stream get updated after the new comment form is submitted?

My impression is that there is no direct way to communicate from a child to a parent when the parent isn’t the top-level LiveView, so I would have to send an event from the new comment form to the top-level LiveView and then call send_update to get it to the post component. That seems a bit convoluted, but I’m not sure there’s another way?

Also I’ve been hearing and reading about the choice between storing posts as a list of post ids and populating them in preload versus populating them in the top level parent and passing a whole post object to each Post component, and I’m not sure how to think about that decision either (or how that decision impacts if or where to use streams for posts and/or comments).

I could just handle everything in the top-level LiveView and avoid this issue altogether, but that would mean duplicating a fair amount of functionality between the top-level LiveViews for the post show page, the main feed page, and the user profile page.

And having thought through all of the above as well as I can, I’m just…not sure what I’m supposed to be doing here or how I’m supposed to be thinking about it. Any thoughts or advice are appreciated. How would you organize this? How would you use LiveComponents vs other constructs and what data would you put where?

Most Liked

Eiji

Eiji

There are few things that could be changed:

posts/show.ex LiveViewposts/index.ex LiveView
postposts/show.ex LiveView
commentposts/:post_id/:comment_id as posts/comments/show.ex LiveView

posts → uses table function component
comments → uses table function component

comment and post forms should be LiveComponent

This way you have flat structure:

  1. One “top” LiveView (post index, post show or comment show)
  2. table function component uses attr for sending a specific message to the parent LiveView
  3. PostForm and CommentForm are LiveComponent just like in generators

Also you can take a look at Events section for targeting a specific parent.

Where Next?

Popular in Questions 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
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
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

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement