diff --git a/pento/lib/pento_web/live/wrong_live.ex b/pento/lib/pento_web/live/wrong_live.ex new file mode 100644 index 0000000..b86319b --- /dev/null +++ b/pento/lib/pento_web/live/wrong_live.ex @@ -0,0 +1,42 @@ +defmodule PentoWeb.WrongLive do + use PentoWeb, :live_view + + def mount(_params, _session, socket) do + {:ok, assign(socket, score: 0, message: "Make a guess:", time: time())} + end + + def render(assigns) do + ~H""" +

Your score: <%= @score %>

+

+ <%= @message %> + It's <%= @time %> +

+
+

+ <%= for n <- 1..10 do %> + <.link + class="bg-blue-500 hover:bg-blue-700 + text-white font-bold py-2 px-4 border border-blue-700 rounded m-1" + phx-click="guess" + phx-value-number={n} + > + <%= n %> + + <% end %> +

+ """ + end + + defp time() do + DateTime.utc_now |> to_string + end + + def handle_event("guess", %{"number" => guess}, socket) do + message = "Your guess: #{guess}. Wrong. Guess again." + + score = socket.assigns.score - 1 + + {:noreply, assign(socket, message: message, score: score, time: time())} + end +end diff --git a/pento/lib/pento_web/router.ex b/pento/lib/pento_web/router.ex index 0e44583..4e66c94 100644 --- a/pento/lib/pento_web/router.ex +++ b/pento/lib/pento_web/router.ex @@ -18,6 +18,7 @@ defmodule PentoWeb.Router do pipe_through :browser get "/", PageController, :home + live "/guess", WrongLive end # Other scopes may use custom stacks.