Skip to content

Commit 40670e4

Browse files
committed
Add Postage Stamp page
1 parent 304ecf0 commit 40670e4

File tree

8 files changed

+147
-0
lines changed

8 files changed

+147
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
defmodule ComponentsGuideWeb.GraphicsController do
2+
use ComponentsGuideWeb, :controller
3+
require Logger
4+
5+
def index(conn, _params) do
6+
render(conn, "index.html", article: "intro")
7+
end
8+
9+
@articles ["postage-stamp"]
10+
11+
def show(conn, %{"id" => article}) when article in @articles do
12+
render(conn, "index.html", article: article)
13+
end
14+
15+
def show(conn, _), do: raise Phoenix.Router.NoRouteError, conn: conn, router: ComponentsGuideWeb.Router
16+
end
17+
18+
defmodule ComponentsGuideWeb.GraphicsView do
19+
use ComponentsGuideWeb, :view
20+
use ComponentsGuideWeb.Snippets
21+
alias ComponentsGuideWeb.ThemeView
22+
23+
def header_styles() do
24+
ThemeView.banner_styles(:web_standards)
25+
end
26+
end

apps/components_guide_web/lib/components_guide_web/router.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ defmodule ComponentsGuideWeb.Router do
4545
WebStandardsController,
4646
only: [:index, :show]
4747
)
48+
49+
resources(
50+
"/graphics",
51+
GraphicsController,
52+
only: [:index, :show]
53+
)
4854

4955
live("/color", ColorLive, :index)
5056
live("/color/:definition", ColorLive, :show)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<header style="<%= header_styles() %>">
2+
<%= render ComponentsGuideWeb.GraphicsView, "_top.html" %>
3+
</header>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<nav class="pt-6 pb-4">
2+
<ul y-y x-x=md class="text-lg font-bold text-shadow" data-links="p-3">
3+
<li><%= link("Intro", to: '/graphics') %>
4+
<li><%= link("Postage Stamp", to: '/graphics/postage-stamp') %>
5+
</ul>
6+
</nav>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div class="mx-auto max-w-4xl text-white">
2+
<h1 y-y x-x=md class="pt-8 row space-x-4 text-4xl text-center font-bold leading-tight text-shadow">
3+
<span class="mr-1 text-5xl">🌟🍭</span>
4+
<span><%= "Graphics" %></span>
5+
</h1>
6+
<%= render ComponentsGuideWeb.GraphicsView, "_nav.html" %>
7+
</div>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<%= render @view_module, "_header.html" %>
2+
3+
<article>
4+
<div class="text-white bg-gray-900">
5+
<div class="content max-w-4xl mx-auto py-8 text-xl" style="--link-decoration: underline;">
6+
<%= render(@view_module, @article <> ".html", conn: @conn) %>
7+
</div>
8+
</div>
9+
</article>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Postage Stamp
2+
3+
<live-render>
4+
ComponentsGuideWeb.Graphics.Live.PostageStamp
5+
</live-render>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
defmodule ComponentsGuideWeb.Graphics.Live.PostageStamp do
2+
use ComponentsGuideWeb, :live_view
3+
4+
alias ComponentsGuideWeb.StylingHelpers
5+
6+
defmodule State do
7+
defstruct primary: "Primary", secondary: "Secondary", width: 400, height: 400, center_y: false
8+
9+
def to_url(%State{primary: primary, secondary: secondary, width: width, height: height, center_y: center_y}) do
10+
# url = URI.parse("https://postage-stamp.collected.workers.dev/1/poster")
11+
url = URI.parse("https://postage-stamp.fly.dev/1/poster")
12+
query = [primary: primary, secondary: secondary, width: width, height: height]
13+
query = if center_y, do: Keyword.put(query, :centerY, ""), else: query
14+
url = put_in(url.query, URI.encode_query(query))
15+
url
16+
end
17+
18+
def change_primary(%State{} = state, primary) do
19+
put_in(state.primary, primary)
20+
end
21+
22+
def change_secondary(%State{} = state, secondary) do
23+
put_in(state.secondary, secondary)
24+
end
25+
26+
def change_center_y(%State{} = state, center_y) when is_boolean(center_y) do
27+
put_in(state.center_y, center_y)
28+
end
29+
end
30+
31+
def mount(_params, _session, socket) do
32+
{:ok, assign(socket, state: %State{})}
33+
end
34+
35+
def render(assigns) do
36+
url = assigns.state |> State.to_url()
37+
38+
~L"""
39+
<form phx-change=change>
40+
41+
<div class="flex flex-col space-y-4">
42+
43+
<label class="flex flex-col">
44+
<span class="font-bold text-yellow-400 mr-2">Primary</span>
45+
<input name=primary type=text value="<%= @state.primary %>" class="text-black text-yellow-900 bg-yellow-100 px-2">
46+
</label>
47+
48+
<label class="flex flex-col">
49+
<span class="font-bold text-purple-400 mr-2">Secondary</span>
50+
<input name=secondary type=text value="<%= @state.secondary %>" class="text-black text-purple-900 bg-purple-100 px-2">
51+
</label>
52+
53+
<output>
54+
<img src="<%= url |> URI.to_string() %>" width="<%= @state.width %>" height="<%= @state.height %>">
55+
<a class="inline-block bg-blue-500 rounded-full" href="<%= url |> URI.to_string() %>">Link to image</a>
56+
</output>
57+
58+
</div>
59+
60+
</form>
61+
"""
62+
end
63+
64+
def handle_event(
65+
"change",
66+
changes = %{
67+
"primary" => primary,
68+
"secondary" => secondary
69+
},
70+
socket
71+
) do
72+
IO.inspect(changes)
73+
74+
state =
75+
socket.assigns.state
76+
|> State.change_primary(primary)
77+
|> State.change_secondary(secondary)
78+
|> State.change_center_y(Map.has_key?(changes, "centerY"))
79+
80+
{
81+
:noreply,
82+
socket |> assign(:state, state)
83+
}
84+
end
85+
end

0 commit comments

Comments
 (0)