1
1
defmodule ComponentsGuideWeb.WebStandards.Live.URL do
2
2
use ComponentsGuideWeb , :live_view
3
+
3
4
alias ComponentsGuideWeb.StylingHelpers
4
5
5
6
defmodule State do
@@ -9,9 +10,17 @@ defmodule ComponentsGuideWeb.WebStandards.Live.URL do
9
10
URI . parse ( raw_url )
10
11
end
11
12
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
13
18
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 ) )
15
24
end
16
25
17
26
def change ( % State { } = state , :scheme , new_scheme ) when is_binary ( new_scheme ) do
@@ -33,6 +42,24 @@ defmodule ComponentsGuideWeb.WebStandards.Live.URL do
33
42
IO . inspect ( url )
34
43
put_in ( state . raw_url , url |> URI . to_string ( ) )
35
44
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
36
63
end
37
64
38
65
def mount ( _params , _session , socket ) do
@@ -44,23 +71,19 @@ defmodule ComponentsGuideWeb.WebStandards.Live.URL do
44
71
<form phx-change=change>
45
72
46
73
<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>
52
75
</pre>
53
76
54
77
<div class="flex flex-col space-y-4">
55
78
56
79
<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">
59
82
</label>
60
83
61
84
<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">
64
87
</label>
65
88
66
89
<label>
@@ -70,38 +93,84 @@ defmodule ComponentsGuideWeb.WebStandards.Live.URL do
70
93
71
94
<label>
72
95
<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 %>
77
106
</div>
107
+
78
108
</label>
79
109
80
110
</div>
81
111
82
112
</form>
83
-
113
+
84
114
<pre class="language-js" phx-hook=PreCode><code>const url = new URL(
85
115
'<%= @state |> State.to_url() |> URI.to_string() %>'
86
116
);
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 %>
91
126
</code></pre>
92
127
"""
93
128
end
94
129
95
130
def handle_event (
96
131
"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
+ } ,
98
139
socket
99
140
) do
141
+ IO . inspect ( changes )
142
+
143
+ query_pairs = Enum . zip ( query_keys , query_values )
144
+ IO . inspect ( query_pairs )
145
+
100
146
state =
101
147
socket . assigns . state
102
148
|> State . change ( :scheme , new_scheme )
103
149
|> State . change ( :host , new_host )
104
150
|> 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 ( )
105
174
106
175
{
107
176
:noreply ,
0 commit comments