Programming Phoenix LiveView: B3.0 (page 27)

I tried to do the exercise for the basic LiveView “game”.

One part is:
Show a restart message and button when the user wins. Hint: you might want to check out the live_patch/28 function to help you build that button

Several things were not clear to me:

  • Should the success message and the Restart-Button be displayed in a new LiveView (CorrectLiveVive) or should I use the existing LiveView (WrongLiveView)?

I decided to use the existing LiveView.
As I had to conditionally render the view for the success-case and the wrong-case,
I added another state (answer_guessed Boolean) to the socket.

This allowed me to use this state to conditionally include the “live_patch”-method inside the render-function for the success case.

I then realized, I had to implement the “handle_params”-method correctly.

See: pento/wrong_live.ex at master · Chrichton/pento · GitHub

I am asking: Is there an easier way to solve this exercise?

Thank you for your reply, Heiko

Hi @Chrichton! Your approach looks pretty clean to me. There was one small error to fix though, your “guess” route definition in router.exs was missing the / before the word “guess”.

You’re right that if you use live_patch/2 you’ll need to implement a handle_params function, and a later chapter will go into all the details there :slight_smile: