11defmodule 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