User: Admin: Difference between revisions

From ElixirBlocks
Jump to: navigation, search
No edit summary
No edit summary
Line 7: Line 7:
To understand web socket configuration we will:
To understand web socket configuration we will:


1. Create a one way data connection between the two LiveViews and explain clearly the relationship between  
1. Create a '''one way data connection''' between the two LiveViews and explain clearly the relationship between the code pieces with a focus on data flow.


2. Edit the code to create a two-way connection between both live views.
2. Edit the code to '''create a two-way''' connection between both live views.


3. Combine both LiveViews into one live view to send and receive.
3. Combine both LiveViews into one live view to send and receive.


4. Write code to store realtime chat content to a database.
4. Write code to store real time chat content to a database.


This may seem like pointless overkill and maybe it is. However, this is what has worked for me.


== PUB SUB ==
== PUB SUB ==
Line 41: Line 43:
</source>
</source>


 
We will ultimately weave these little pieces of code into a LiveView and create a chat application.
 
We will weave these little pieces of code into a LiveView to create a chat application.


== Create Two Minimalistic Vanilla LiveViews ==
== Create Two Minimalistic Vanilla LiveViews ==

Revision as of 19:29, 16 June 2023

Create two working minimalistic LiveViews.

One is named SendView and the other is ReceiveView .

The SendView consist of a single form field to send data to RecieveView. ReceiveView contains code to renders SendView data to its page.

To understand web socket configuration we will:

1. Create a one way data connection between the two LiveViews and explain clearly the relationship between the code pieces with a focus on data flow.

2. Edit the code to create a two-way connection between both live views.

3. Combine both LiveViews into one live view to send and receive.

4. Write code to store real time chat content to a database.


This may seem like pointless overkill and maybe it is. However, this is what has worked for me.

PUB SUB

Before we begin, I will share three small pieces of code with you. These are the main characters, or main components for what we will build.

AppWeb.Endpoint.subscribe()
AppWeb.Endpoint.broadcast()
def handle_info(msg, socket) do
    {:noreply, assign(socket)}
end


def handle_event(msg, params, socket) do
    {:noreply, socket}
end

We will ultimately weave these little pieces of code into a LiveView and create a chat application.

Create Two Minimalistic Vanilla LiveViews

In router.ex create the following routes:


  scope "/", AppWeb do
    pipe_through :browser
    live "/send", SendLive, :home
    live "/receive", ReceiveLive, :home
  end



  • How to Create Database Seed Data.
  • How to Use Generators in a Real World Project
  • How to Work with Database Data via Basic Commands and Custom Change sets.
  • How to Render Database Data to LiveView (via Context Commands).
  • How to Create a Chat using PubSub.
  • How to Create Event Handlers in LiveView
  • How to Create a Dynamic Route from Scratch.
  • How to Use Elixirs Template language
  • How to Work with Modal
  • How to Use CSS From Scratch (Without bundler)
  • How to use JS hooks

_________________________________________________

https://elixirschool.com/blog/live-view-live-component



Note:

mix phx.gen.html Alerts Alert alerts title:string content:text enabled:boolean


defmodule AppWeb.PageLive do
   use AppWeb, :live_view  
   alias App.Alerts
   def mount(_params, _session, socket)  do
	 {:ok, assign(socket, alerts: Alerts.list_alerts())}  
   end

   def render(assigns) do
     ~H"""
        <%= for alert <- @alerts do %>
        	<%= if alert.enabled  do %>
              <div><%= alert.content%></div>
              <% end %>

        <% end %>

	 """ 
   end

end

#_____________________________

  def check_for_single_selection(data) do 

      list = App.Alerts.list_alerts()

      IO.inspect list

      result = Enum.filter(list, fn x -> x.enabled == true end)
      IO.inspect "WORK_____________________________"  
      IO.inspect result 
      IO.inspect "WORK_____________________________"

      data

  end

__________________________________________________

Complex pattern matching https://www.youtube.com/watch?v=gCVWrM5BNVE

Genservers (and related, agent,task etc)


__________________________________________________

ECTO

query = from "artists", select:[:name]

Verbose:

Ecto.Query.from("artists", select[:name])


Repo.all(query)

  1. => [%{name: "Miles Davis"}, %{name: "Bill Evans"}]



Repo.to_sql(:all, query)