Skip to content

Commit 4d85626

Browse files
committed
Improve URL page with query vars, more code examples
1 parent 0718853 commit 4d85626

File tree

3 files changed

+126
-36
lines changed

3 files changed

+126
-36
lines changed

Diff for: apps/components_guide_web/lib/components_guide_web/template_engines/markdown_engine.ex

+22-12
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,35 @@ defmodule ComponentsGuideWeb.TemplateEngines.MarkdownEngine do
77

88
def compile(path, name) do
99
IO.puts("compile #{path} #{name}")
10-
html = path
11-
|> File.read!()
12-
|> Earmark.as_html!(%Earmark.Options{code_class_prefix: "language-", smartypants: false})
10+
11+
html =
12+
path
13+
|> File.read!()
14+
|> Earmark.as_html!(%Earmark.Options{code_class_prefix: "language-", smartypants: false})
1315

1416
# regex = ~r{<live-([\w-]+)>(.+)</live-([\w-]+)>}
1517
regex = ~r{<live-([\w-]+)>([^<]+)</live-([^>]+)>}
1618
# regex = ~r{<live-([\w-]+)>}
1719

1820
# live? = Regex.match(regex, html)
1921

20-
html = Regex.replace(regex, html, fn whole, name, content ->
21-
case name do
22-
"render" ->
23-
module = content |> String.trim #|> String.to_existing_atom
24-
"<div><%= live_render(@conn, #{module}, session: %{}) %></div>"
25-
_ ->
26-
"!" <> name <> "!" <> content
27-
end
28-
end)
22+
html =
23+
Regex.replace(regex, html, fn whole, name, content ->
24+
case name do
25+
"render" ->
26+
# |> String.to_existing_atom
27+
module = content |> String.trim()
28+
"<div><%= live_render(@conn, #{module}, session: %{}) %></div>"
29+
30+
"component" ->
31+
# |> String.to_existing_atom
32+
module = content |> String.trim()
33+
"<div><%= live_component(@conn, #{module}) %></div>"
34+
35+
_ ->
36+
"!" <> name <> "!" <> content
37+
end
38+
end)
2939

3040
# html = Regex.replace(regex, html, fn whole, name, content -> "!" <> name <> "!" <> content end)
3141

Diff for: apps/components_guide_web/lib/components_guide_web/templates/web_standards/url.html.md

+14-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
ComponentsGuideWeb.WebStandards.Live.URL
77
</live-render>
88

9-
## Relative URLs
10-
119
## JavaScript’s `URL`
1210

1311
```js
@@ -35,6 +33,19 @@ const current = new URL(request.url);
3533
## Swift’s `URL`
3634

3735
```swift
38-
let root = URL(string: "/")
36+
let url = URL(string: "https://www.example.org/")
37+
38+
```
39+
40+
## Elixir’s `URI`
41+
42+
```elixir
43+
url = URI.parse("https://www.example.org/")
44+
45+
```
46+
47+
## Go’s `net/url`
3948

49+
```go
50+
u, err := url.Parse("https://www.example.org/")
4051
```

Diff for: apps/components_guide_web/lib/components_guide_web/templates/web_standards/url_live.ex

+90-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule ComponentsGuideWeb.WebStandards.Live.URL do
22
use ComponentsGuideWeb, :live_view
3+
34
alias ComponentsGuideWeb.StylingHelpers
45

56
defmodule State do
@@ -9,9 +10,17 @@ defmodule ComponentsGuideWeb.WebStandards.Live.URL do
910
URI.parse(raw_url)
1011
end
1112

12-
def get_field(%State{} = state, field) when field in [:host] do
13+
def to_url_string(%State{raw_url: raw_url}) do
14+
URI.parse(raw_url) |> URI.to_string()
15+
end
16+
17+
def get_query_vars(%State{} = state) do
1318
url = state |> to_url()
14-
url[field]
19+
url.query |> URI.query_decoder()
20+
end
21+
22+
defp change_url(%State{} = state, url) do
23+
put_in(state.raw_url, URI.to_string(url))
1524
end
1625

1726
def change(%State{} = state, :scheme, new_scheme) when is_binary(new_scheme) do
@@ -33,6 +42,24 @@ defmodule ComponentsGuideWeb.WebStandards.Live.URL do
3342
IO.inspect(url)
3443
put_in(state.raw_url, url |> URI.to_string())
3544
end
45+
46+
def clear_query(%State{} = state) do
47+
url = to_url(state)
48+
url = put_in(url.query, "")
49+
put_in(state.raw_url, url |> URI.to_string())
50+
end
51+
52+
def add_new_query(%State{} = state) do
53+
url = to_url(state)
54+
url = put_in(url.query, url.query <> "&a=b")
55+
put_in(state.raw_url, url |> URI.to_string())
56+
end
57+
58+
def change_query_vars(%State{} = state, query_vars) do
59+
url = to_url(state)
60+
url = put_in(url.query, URI.encode_query(query_vars))
61+
put_in(state.raw_url, url |> URI.to_string())
62+
end
3663
end
3764

3865
def mount(_params, _session, socket) do
@@ -44,23 +71,19 @@ defmodule ComponentsGuideWeb.WebStandards.Live.URL do
4471
<form phx-change=change>
4572
4673
<pre class="p-4 my-2" style="color: #d6deeb;">
47-
<code><%= @state |> State.to_url() |> URI.to_string() %></code>
48-
</pre>
49-
50-
<pre class="p-4 my-2" style="color: #d6deeb;">
51-
<code><span class="text-green-400"><%= State.to_url(@state).scheme %></span>://<span class="text-yellow-400"><%= State.to_url(@state).host %></span><span class="text-orange-400"><%= State.to_url(@state).path %></span><span class="text-indigo-400"/songs</span>?first=20&sortBy=releaseDate</code>
74+
<code><span class="text-green-400"><%= State.to_url(@state).scheme %></span>://<span class="text-yellow-400"><%= State.to_url(@state).host %></span><span class="text-orange-400"><%= State.to_url(@state).path %></span><span class="text-indigo-400">?<%= State.to_url(@state).query %></span></code>
5275
</pre>
5376
5477
<div class="flex flex-col space-y-4">
5578
5679
<label>
57-
<span class="font-bold text-green-400 mr-2">Scheme</span>
58-
<input name=scheme type=text value="https" class="text-black text-green-900 bg-green-100 px-2">
80+
<span class="font-bold text-yellow-400 mr-2">Scheme</span>
81+
<input name=scheme type=text value="https" class="text-black text-yellow-900 bg-yellow-100 px-2">
5982
</label>
6083
6184
<label>
62-
<span class="font-bold text-yellow-400 mr-2">Host</span>
63-
<input name=host type=text value="example.org" class="text-black text-yellow-900 bg-yellow-100 px-2">
85+
<span class="font-bold text-green-400 mr-2">Host</span>
86+
<input name=host type=text value="example.org" class="text-black text-green-900 bg-green-100 px-2">
6487
</label>
6588
6689
<label>
@@ -70,38 +93,84 @@ defmodule ComponentsGuideWeb.WebStandards.Live.URL do
7093
7194
<label>
7295
<span class="font-bold text-orange-400 mr-2">Query</span>
73-
<button type=button class="px-2 bg-white text-black rounded">Add query</button>
74-
<div>
75-
<input name=queryKey type=text value="" class="text-black text-orange-900 bg-orange-100 px-2">
76-
<input name=queryValue type=text value="/songs" class="text-black text-orange-900 bg-orange-100 px-2">
96+
<button type=button phx-click=add-query class="px-2 bg-white text-black rounded">Add query</button>
97+
<button type=button phx-click=clear-query class="px-2 bg-white text-black rounded">Clear query</button>
98+
99+
<div class="space-y-2 mt-2">
100+
<%= for {key, value} <- State.get_query_vars(@state) do %>
101+
<div>
102+
<input name=query-keys[] type=text value="<%= key %>" class="text-black text-indigo-900 bg-indigo-100 px-2">
103+
<input name=query-values[] type=text value="<%= value %>" class="text-black text-indigo-900 bg-indigo-100 px-2">
104+
</div>
105+
<% end %>
77106
</div>
107+
78108
</label>
79109
80110
</div>
81111
82112
</form>
83-
113+
84114
<pre class="language-js" phx-hook=PreCode><code>const url = new URL(
85115
'<%= @state |> State.to_url() |> URI.to_string() %>'
86116
);
87-
url.protocol; // <%= State.to_url(@state).scheme %>:
88-
url.host; // <%= State.to_url(@state).host %>:
89-
url.path; // <%= State.to_url(@state).path %>:
90-
url.search; // <%= State.to_url(@state).query %>:
117+
url.protocol; // '<%= State.to_url(@state).scheme %>:'
118+
url.host; // '<%= State.to_url(@state).host %>'
119+
url.path; // '<%= State.to_url(@state).path %>'
120+
121+
url.search; // '<%= State.to_url(@state).query %>'
122+
const query = new URLSearchParams(url.search);
123+
<%= for {key, value} <- State.get_query_vars(@state) do
124+
"query.get('#{key}'); // '#{value}'\n"
125+
end %>
91126
</code></pre>
92127
"""
93128
end
94129

95130
def handle_event(
96131
"change",
97-
%{"scheme" => new_scheme, "host" => new_host, "path" => new_path},
132+
changes = %{
133+
"scheme" => new_scheme,
134+
"host" => new_host,
135+
"path" => new_path,
136+
"query-keys" => query_keys,
137+
"query-values" => query_values
138+
},
98139
socket
99140
) do
141+
IO.inspect(changes)
142+
143+
query_pairs = Enum.zip(query_keys, query_values)
144+
IO.inspect(query_pairs)
145+
100146
state =
101147
socket.assigns.state
102148
|> State.change(:scheme, new_scheme)
103149
|> State.change(:host, new_host)
104150
|> State.change(:path, new_path)
151+
|> State.change_query_vars(query_pairs)
152+
153+
{
154+
:noreply,
155+
socket |> assign(:state, state)
156+
}
157+
end
158+
159+
def handle_event("clear-query", _, socket) do
160+
state =
161+
socket.assigns.state
162+
|> State.clear_query()
163+
164+
{
165+
:noreply,
166+
socket |> assign(:state, state)
167+
}
168+
end
169+
170+
def handle_event("add-query", _, socket) do
171+
state =
172+
socket.assigns.state
173+
|> State.add_new_query()
105174

106175
{
107176
:noreply,

0 commit comments

Comments
 (0)