-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
124 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,84 @@ | ||
defmodule AppWeb.AppLive do | ||
use AppWeb, :live_view | ||
|
||
def mount(_params, _session, socket) do | ||
{:ok, socket} | ||
@topic "live" | ||
@img "https://avatars.githubusercontent.com/u/" | ||
|
||
def mount(_session, _params, socket) do | ||
if connected?(socket) do | ||
AppWeb.Endpoint.subscribe(@topic) # subscribe to the channel | ||
end | ||
p = %{id: 1, login: "Alex", avatar_url: "#{@img}1"} | ||
{:ok, assign(socket, %{data: p})} | ||
end | ||
|
||
def handle_event("inc", _value, socket) do | ||
dbg(socket.assigns) | ||
val = socket.assigns.data.id + 1 | ||
p = %{id: val, login: "Alex #{val}", avatar_url: "#{@img}#{val}"} | ||
new_state = assign(socket, %{data: p}) | ||
broadcast("inc", new_state.assigns) | ||
|
||
{:noreply, new_state} | ||
end | ||
|
||
def handle_event("dec", _, socket) do | ||
val = socket.assigns.val - 1 | ||
p = %{id: val, login: "Alex", avatar_url: "#{@img}#{val}"} | ||
new_state = assign(socket, %{data: p}) | ||
broadcast("dec", new_state.assigns) | ||
{:noreply, new_state} | ||
end | ||
|
||
def handle_event("sync", _value, socket) do | ||
IO.inspect("sync") | ||
sync(socket) | ||
# val = socket.assigns.val + 1 | ||
# p = %{login: "Alex", avatar_url: "#{@img}#{val}"} | ||
# new_state = assign(socket, %{val: val, data: p}) | ||
# broadcast("inc", new_state.assigns) | ||
|
||
# :timer.apply_interval(1000, IO, :puts, ["weeeee"]) | ||
|
||
{:noreply, socket} | ||
end | ||
|
||
def handle_event("update", _value, socket) do | ||
IO.inspect("- - - - - - - - - - - - - - - - - - - handle_event: update") | ||
{:noreply, socket} | ||
end | ||
|
||
# update `data` by broadcasting it as the profiles are crawled: | ||
def sync(socket) do | ||
|
||
dbg(socket.assigns) | ||
list = App.GitHub.org_user_list("dwyl") | ||
# dbg(list) | ||
# Iterate through the list of people | ||
# Enum.map(list, Task.async( fn u -> | ||
# dbg(u) | ||
# end)) | ||
list | ||
|> Stream.with_index | ||
|> Enum.map(fn {u, i} -> | ||
IO.inspect("- - - Enum.map u.login: #{i}: #{u.login}") | ||
data = App.User.get_user_from_api(u.login) | ||
data = AuthPlug.Helpers.strip_struct_metadata(data) | ||
new_state = assign(socket, %{data: data}) | ||
Task.start(fn -> | ||
:timer.sleep(300 + 100 * i) | ||
AppWeb.Endpoint.broadcast(@topic, "update", new_state.assigns) | ||
end) | ||
end) | ||
|
||
{:noreply, socket} | ||
end | ||
|
||
def handle_info(msg, socket) do | ||
{:noreply, assign(socket, data: msg.payload.data)} | ||
end | ||
|
||
def broadcast(msg, assigns) do | ||
AppWeb.Endpoint.broadcast_from(self(), @topic, msg, assigns) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,28 @@ | ||
<h1 class="text-3xl text-white py-3 text-center | ||
bg-gradient-to-r from-green-400 to-blue-500 rounded-lg shadow-lg"> | ||
hello david | ||
</h1> | ||
</h1> | ||
|
||
<div> | ||
<h1 class="text-4xl font-bold text-center"> | ||
The count is: <%= @data.id %> </h1> | ||
|
||
<p class="text-center"> | ||
<button phx-click="dec" | ||
class="w-20 bg-red-500 hover:bg-red-600">-</button> | ||
<button phx-click="inc" | ||
class="w-20 bg-green-500 hover:bg-green-600">+</button> | ||
</p> | ||
</div> | ||
|
||
<h1>{ @data.login }</h1> | ||
|
||
<img style="height:auto;" src={@data.avatar_url} | ||
width="260" height="260" | ||
class="rounded-full"> | ||
|
||
|
||
<button phx-click="sync" | ||
class="w-20 bg-green-600 hover:bg-green-800 text-white font-bold py-2 px-4 rounded"> | ||
Sync | ||
</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ defmodule AppWeb.Router do | |
|
||
scope "/", AppWeb do | ||
pipe_through :browser | ||
|
||
live "/", AppLive | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters