Browse Source

feat: add menu

master
Inhji Y. 10 months ago
parent
commit
038717b811
  1. 12
      apps/tomie_web/assets/css/home/navigation.css
  2. 7
      apps/tomie_web/lib/tomie_web/controllers/home_controller.ex
  3. 25
      apps/tomie_web/lib/tomie_web/plugs/menu_path_plug.ex
  4. 2
      apps/tomie_web/lib/tomie_web/router.ex
  5. 5
      apps/tomie_web/lib/tomie_web/templates/home/menu.html.eex
  6. 4
      apps/tomie_web/lib/tomie_web/templates/layout/home.html.eex

12
apps/tomie_web/assets/css/home/navigation.css

@ -20,7 +20,6 @@ nav#main-navigation a {
color: #333;
}
nav#main-navigation .brand {
font-weight: bold;
color: #333;
@ -29,4 +28,15 @@ nav#main-navigation .brand {
nav#main-navigation .brand::after {
content: '.';
color: var(--primary-color);
}
nav#main-navigation .menu {
font-size: 1.5rem;
padding: 0.25rem 0.5rem;
background: var(--primary-color);
}
nav#main-navigation .menu a {
color: var(--main-bg-color);
}

7
apps/tomie_web/lib/tomie_web/controllers/home_controller.ex

@ -30,6 +30,13 @@ defmodule TomieWeb.HomeController do
render(conn, "about.html", page_title: "About")
end
def menu(%{request_path: path, req_headers: headers} = conn, _params) do
IO.inspect(path)
IO.inspect(headers)
render(conn, "menu.html", page_title: "Menu")
end
def project(conn, %{"name" => name}) do
pretty_name = String.capitalize(name)

25
apps/tomie_web/lib/tomie_web/plugs/menu_path_plug.ex

@ -0,0 +1,25 @@
defmodule TomieWeb.MenuPathPlug do
import Plug.Conn, only: [assign: 3]
alias TomieWeb.Router.Helpers, as: Routes
def init(opts), do: opts
def call(%{request_path: path, req_headers: headers} = conn, _opts) do
menu_path =
if path === Routes.home_path(conn, :menu) do
get_referrer_path(headers)
else
Routes.home_path(conn, :menu)
end
assign(conn, :menu_path, menu_path)
end
defp get_referrer_path(headers) do
headers
|> Enum.into(%{})
|> Map.get("referer")
|> URI.parse()
|> Map.get(:path)
end
end

2
apps/tomie_web/lib/tomie_web/router.ex

@ -13,6 +13,7 @@ defmodule TomieWeb.Router do
pipeline :home do
plug :accepts, ["html"]
plug TomieWeb.MenuPathPlug
end
pipeline :api do
@ -39,6 +40,7 @@ defmodule TomieWeb.Router do
get "/", HomeController, :index
get "/about", HomeController, :about
get "/menu", HomeController, :menu
get "/project/:name", HomeController, :project
get "/bookmark/:id", HomeController, :bookmark

5
apps/tomie_web/lib/tomie_web/templates/home/menu.html.eex

@ -0,0 +1,5 @@
<h1>Menu</h1>
<ul>
<li><%= link "About Me", to: Routes.home_path(@conn, :about) %></li>
</ul>

4
apps/tomie_web/lib/tomie_web/templates/layout/home.html.eex

@ -22,8 +22,8 @@
</head>
<body>
<nav id="main-navigation">
<span class="brand"><a href="/">Inhji</a></span>
<span><a href="/about">About</a></span>
<span class="brand"><%= link "Inhji", to: Routes.home_path(@conn, :index) %></span>
<span class="menu"><%= link "Menu", to: @menu_path %></span>
</nav>
<main id="wrapper">

Loading…
Cancel
Save