Skip to content

Commit

Permalink
render wall of faces #219
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Jan 15, 2025
1 parent 9b93f69 commit bf8f05f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 65 deletions.
47 changes: 24 additions & 23 deletions lib/app/user.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule App.User do
use Ecto.Schema
alias App.{Repo}
import Ecto.Changeset
import Ecto.{Changeset, Query}
require Logger
alias __MODULE__

Expand Down Expand Up @@ -43,7 +43,7 @@ defmodule App.User do
# `user` map must include the `id` and `login` fields
def get_user_from_api(user) do
data = App.GitHub.user(user.login)
# prolly wouldn't do this in a "real" app ... feel free to refactor.
# Not super happy about this crude error handling ... feel free to refactor.
if Map.has_key?(data, :status) && data.status == "404" do
# IO.inspect(" - - - - - - - - - - - - - - - - - #{user.login}")
# dbg(user)
Expand All @@ -60,30 +60,12 @@ defmodule App.User do
end
end

# Next: get list of org members
# get user for each in the list
# map data to our table
# insert data

# tidy data before insertion
def map_github_user_fields_to_table(u) do
%{
id: u.id,
Map.merge(u, %{
avatar_url: String.split(u.avatar_url, "?") |> List.first,
bio: u.bio,
blog: u.blog,
company: clean_company(u.company),
created_at: u.created_at,
email: u.email,
followers: u.followers,
following: u.following,
hireable: u.hireable,
location: u.location,
login: u.login,
name: u.name,
public_repos: u.public_repos,
two_factor_authentication: false,
updated_at: u.updated_at
}
})
end

def clean_company(company) do
Expand Down Expand Up @@ -115,4 +97,23 @@ defmodule App.User do
two_factor_authentication: false
}, u)
end

# def list_users do
# User
# |> limit(20)
# |> order_by(desc: :inserted_at)
# |> Repo.all()
# end

def list_users_avatars do
from(u in User, select: %{avatar_url: u.avatar_url})
# |> limit(20)
|> order_by(desc: :inserted_at)
# |> distinct(true)
|> Repo.all()
# return a list of urls not a list of maps
|> Enum.reduce([], fn u, acc ->
[u.avatar_url | acc]
end)
end
end
9 changes: 8 additions & 1 deletion lib/app_web/live/app_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ defmodule AppWeb.AppLive do
avatar_url: "#{@img}128895421", name: "Alexander the Greatest",
bio: "Love learning how to code with my crew of cool cats!",
created_at: "2010-02-02T08:44:49Z", company: "ideaq"}
{:ok, assign(socket, %{data: p})}
# NEXT: prepend avatars to list ...


{:ok, assign(socket, %{data: p, avatars: App.User.list_users_avatars()})}
end

def handle_event("sync", value, socket) do
Expand Down Expand Up @@ -64,4 +67,8 @@ defmodule AppWeb.AppLive do
def truncate_bio(bio) do
Useful.truncate(bio, 29, " ...")
end

def tiny_avatar(src) do
"#{src}?s=40"
end
end
6 changes: 4 additions & 2 deletions lib/app_web/live/app_live.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class="w-20 bg-green-600 hover:bg-green-800 text-white font-bold py-2 px-4 round
</div>
</div>

<div id="faces">
<p>hello</p>
<div id="faces" class="px-4 mb-4">
<span :for={src <- @avatars} class="w-max">
<img src={tiny_avatar(src)} class="float-left w-8">
</span>
</div>
54 changes: 15 additions & 39 deletions test/app/user_test.exs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
defmodule App.UserTest do
use App.DataCase
# alias App.User
alias App.User

test "App.User.create/1" do
user = %{
def user do
%{
avatar_url: "https://avatars.githubusercontent.com/u/4185328?v=4",
bio: "Co-founder @dwyl",
blog: "https://www.twitter.com/iteles",
Expand All @@ -22,6 +22,10 @@ defmodule App.UserTest do
public_gists: 0,
public_repos: 31
}
end

test "App.User.create/1" do
user = user()
assert {:ok, inserted_user} = App.User.create(user)
assert inserted_user.name == user.name
end
Expand All @@ -32,6 +36,7 @@ defmodule App.UserTest do
end

test "get_user_from_api/1 unhappy path (kittenking)" do
# ref: https://github.com/dwyl/who/issues/216
user = %{
id: 53072918,
type: "User",
Expand All @@ -50,40 +55,11 @@ defmodule App.UserTest do
data = App.User.dummy_data(%{id: 42})
assert data.company == "good"
end
end

%{
organizations_url: "https://api.github.com/users/iteles/orgs",
following: 80,
login: "iteles",
public_repos: 31,
received_events_url: "https://api.github.com/users/iteles/received_events",
bio: "Co-founder @dwyl \r\n",
user_view_type: "public",
company: "@dwyl",
gravatar_id: "",
twitter_username: nil,
following_url: "https://api.github.com/users/iteles/following{/other_user}",
created_at: "2013-04-17T21:10:06Z",
followers: 400,
site_admin: false,
blog: "http://www.twitter.com/iteles",
starred_url: "https://api.github.com/users/iteles/starred{/owner}{/repo}",
public_gists: 0,
hireable: true,
gists_url: "https://api.github.com/users/iteles/gists{/gist_id}",
events_url: "https://api.github.com/users/iteles/events{/privacy}",
followers_url: "https://api.github.com/users/iteles/followers",
node_id: "MDQ6VXNlcjQxODUzMjg=",
url: "https://api.github.com/users/iteles",
id: 4185328,
name: "Ines Teles Correia",
subscriptions_url: "https://api.github.com/users/iteles/subscriptions",
html_url: "https://github.com/iteles",
location: "London, UK",
type: "User",
avatar_url: "https://avatars.githubusercontent.com/u/4185328?v=4",
email: nil,
repos_url: "https://api.github.com/users/iteles/repos",
updated_at: "2024-08-05T22:59:09Z"
}
test "list_users_avatars/0" do
user = user() |> User.map_github_user_fields_to_table()
User.create(user)
list = App.User.list_users_avatars()
assert length(list) > 0
end
end

0 comments on commit bf8f05f

Please sign in to comment.