Allow users to add a github username via the UI.

This commit is contained in:
James Harton 2018-04-08 11:17:58 +12:00
parent 4a3670f676
commit 9075cfedf5
8 changed files with 76 additions and 41 deletions

View file

@ -15,7 +15,7 @@ defmodule Faces.Gallery.GithubUserData do
}} }}
iex> GitHubUserData.get("thisUserReallyDoesntExist") iex> GitHubUserData.get("thisUserReallyDoesntExist")
{:error, "404 while retrieving thisUserReallyDoesntExist from Github: Not Found"} {:error, "404 while retrieving \"thisUserReallyDoesntExist\" from Github: Not Found"}
""" """
def get(username) do def get(username) do
with {200, user_data, _} <- get_user_from_github(username), with {200, user_data, _} <- get_user_from_github(username),
@ -27,10 +27,10 @@ defmodule Faces.Gallery.GithubUserData do
{:error, reason} {:error, reason}
{i, %{"message" => message}, _} when is_integer(i) -> {i, %{"message" => message}, _} when is_integer(i) ->
{:error, "#{i} while retrieving #{username} from Github: #{message}"} {:error, "#{i} while retrieving #{inspect(username)} from Github: #{message}"}
{i, _, _} when is_integer(i) -> {i, _, _} when is_integer(i) ->
{:error, "#{i} while retrieving #{username} from Github"} {:error, "#{i} while retrieving #{inspect(username)} from Github"}
end end
end end

View file

@ -0,0 +1,20 @@
defmodule FacesWeb.FaceController do
use FacesWeb, :controller
alias Faces.Gallery
def index(conn, _params) do
render(conn, "index.html", people: Gallery.list_people())
end
def create(conn, %{"username" => username}) do
case Gallery.import_user(username) do
{:ok, _user} ->
render(conn, "index.html", people: Gallery.list_people())
{:error, reason} ->
conn
|> put_flash(:error, reason)
|> render("index.html", people: Gallery.list_people())
end
end
end

View file

@ -1,8 +0,0 @@
defmodule FacesWeb.PageController do
use FacesWeb, :controller
alias Faces.Gallery
def index(conn, _params) do
render(conn, "index.html", people: Gallery.list_people())
end
end

View file

@ -2,21 +2,22 @@ defmodule FacesWeb.Router do
use FacesWeb, :router use FacesWeb, :router
pipeline :browser do pipeline :browser do
plug :accepts, ["html"] plug(:accepts, ["html"])
plug :fetch_session plug(:fetch_session)
plug :fetch_flash plug(:fetch_flash)
plug :protect_from_forgery plug(:protect_from_forgery)
plug :put_secure_browser_headers plug(:put_secure_browser_headers)
end end
pipeline :api do pipeline :api do
plug :accepts, ["json"] plug(:accepts, ["json"])
end end
scope "/", FacesWeb do scope "/", FacesWeb do
pipe_through :browser # Use the default browser stack # Use the default browser stack
pipe_through(:browser)
get "/", PageController, :index resources("/", FaceController)
end end
# Other scopes may use custom stacks. # Other scopes may use custom stacks.

View file

@ -0,0 +1,41 @@
<div class="row">
<div class="col-sm">
<h1>Face Gallery</h1>
<%= if get_flash(@conn, :error) do %>
<p class="alert alert-danger" role="alert"><%= get_flash(@conn, :error) %></p>
<% end %>
</div>
</div>
<div class="row" id="face-gallery">
<%= for person <- @people do %>
<div class="col-sm-3 mb-3">
<div class="card">
<img class="card-img-top" src="<%= person.avatar_url %>" alt="<%= person.name %>">
<div class="card-body">
<h5 class="card-title"><%= person.name %></h5>
<p class="card-text"><%= person.location %></p>
</div>
</div>
</div>
<% end %>
<div class="col-sm-3 mb-3">
<div class="card">
<div class="card-header">
<h5 class="card-title">Add Face</h5>
</div>
<div class="card-body">
<%= form_for @conn, face_path(@conn, :create), fn f -> %>
<div class="form-group">
<label for="username">Github Username</label>
<%= text_input f, :username, class: "form-control" %>
</div>
<%= submit "Add", class: "btn btn-primary" %>
<% end %>
</div>
</div>
</div>
</div>

View file

@ -1,19 +0,0 @@
<div class="row">
<div class="col-sm">
<h1>Face Gallery</h1>
</div>
</div>
<div class="row" id="face-gallery">
<%= for person <- @people do %>
<div class="col-sm-3 m-3">
<div class="card">
<img class="card-img-top" src="<%= person.avatar_url %>" alt="<%= person.name %>">
<div class="card-body">
<h5 class="card-title"><%= person.name %></h5>
<p class="card-text"><%= person.location %></p>
</div>
</div>
</div>
<% end %>
</div>

View file

@ -0,0 +1,3 @@
defmodule FacesWeb.FaceView do
use FacesWeb, :view
end

View file

@ -1,3 +0,0 @@
defmodule FacesWeb.PageView do
use FacesWeb, :view
end