|
| 1 | +defmodule ComponentsGuideWeb.WebStandards.Live.URL do |
| 2 | + use ComponentsGuideWeb, :live_view |
| 3 | + alias ComponentsGuideWeb.StylingHelpers |
| 4 | + |
| 5 | + defmodule State do |
| 6 | + defstruct raw_url: "https://example.org/songs?first=20&sortBy=releaseDate" |
| 7 | + |
| 8 | + def to_url(%State{raw_url: raw_url}) do |
| 9 | + URI.parse(raw_url) |
| 10 | + end |
| 11 | + |
| 12 | + def get_field(%State{} = state, field) when field in [:host] do |
| 13 | + url = state |> to_url() |
| 14 | + url[field] |
| 15 | + end |
| 16 | + |
| 17 | + def change(%State{} = state, :scheme, new_scheme) when is_binary(new_scheme) do |
| 18 | + url = to_url(state) |
| 19 | + url = put_in(url.scheme, new_scheme) |
| 20 | + put_in(state.raw_url, url |> URI.to_string()) |
| 21 | + end |
| 22 | + |
| 23 | + def change(%State{} = state, :host, new_host) when is_binary(new_host) do |
| 24 | + url = to_url(state) |
| 25 | + url = put_in(url.host, new_host) |
| 26 | + IO.inspect(url) |
| 27 | + put_in(state.raw_url, url |> URI.to_string()) |
| 28 | + end |
| 29 | + |
| 30 | + def change(%State{} = state, :path, new_path) when is_binary(new_path) do |
| 31 | + url = to_url(state) |
| 32 | + url = put_in(url.path, new_path) |
| 33 | + IO.inspect(url) |
| 34 | + put_in(state.raw_url, url |> URI.to_string()) |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + def mount(_params, _session, socket) do |
| 39 | + {:ok, assign(socket, state: %State{})} |
| 40 | + end |
| 41 | + |
| 42 | + def render(assigns) do |
| 43 | + ~L""" |
| 44 | + <form phx-change=change> |
| 45 | +
|
| 46 | + <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> |
| 52 | + </pre> |
| 53 | +
|
| 54 | + <div class="flex flex-col space-y-4"> |
| 55 | +
|
| 56 | + <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"> |
| 59 | + </label> |
| 60 | +
|
| 61 | + <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"> |
| 64 | + </label> |
| 65 | +
|
| 66 | + <label> |
| 67 | + <span class="font-bold text-orange-400 mr-2">Path</span> |
| 68 | + <input name=path type=text value="/songs" class="text-black text-orange-900 bg-orange-100 px-2"> |
| 69 | + </label> |
| 70 | +
|
| 71 | + <label> |
| 72 | + <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"> |
| 77 | + </div> |
| 78 | + </label> |
| 79 | +
|
| 80 | + </div> |
| 81 | +
|
| 82 | + </form> |
| 83 | + |
| 84 | + <pre class="language-js" phx-hook=PreCode><code>const url = new URL( |
| 85 | + '<%= @state |> State.to_url() |> URI.to_string() %>' |
| 86 | + ); |
| 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 %>: |
| 91 | + </code></pre> |
| 92 | + """ |
| 93 | + end |
| 94 | + |
| 95 | + def handle_event( |
| 96 | + "change", |
| 97 | + %{"scheme" => new_scheme, "host" => new_host, "path" => new_path}, |
| 98 | + socket |
| 99 | + ) do |
| 100 | + state = |
| 101 | + socket.assigns.state |
| 102 | + |> State.change(:scheme, new_scheme) |
| 103 | + |> State.change(:host, new_host) |
| 104 | + |> State.change(:path, new_path) |
| 105 | + |
| 106 | + { |
| 107 | + :noreply, |
| 108 | + socket |> assign(:state, state) |
| 109 | + } |
| 110 | + end |
| 111 | +end |
0 commit comments