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

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
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
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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 record...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement