Skip to content

Commit

Permalink
Merge pull request #188 from coingaming/fix/checkboxes-fix-new
Browse files Browse the repository at this point in the history
fix: checkboxes fix
  • Loading branch information
theycallmehero authored Nov 26, 2021
2 parents 0b50e6c + 866abc0 commit 103d482
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 51 deletions.
8 changes: 7 additions & 1 deletion lib/moon/components/checkbox.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ defmodule Moon.Components.Checkbox do
field={@field}
click={@click}
class="z-10 absolute left-0 opacity-0"
checked_value="true"
unchecked_value="false"
opts={disabled: @disabled}
/>
<InputContext assigns={assigns} :let={form: form, field: field}>
{fake_checkbox(
assigns,
selected: Phoenix.HTML.Form.input_value(form, field) || @checked
selected: is_true(Phoenix.HTML.Form.input_value(form, field)) || is_true(@checked)
)}
</InputContext>
Expand All @@ -34,6 +36,10 @@ defmodule Moon.Components.Checkbox do
"""
end

def is_true(val) do
Enum.member?([true, "true"], val)
end

def fake_checkbox(assigns, selected: selected) do
~F"""
<div class={
Expand Down
6 changes: 3 additions & 3 deletions lib/moon/components/link.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ defmodule Moon.Components.Link do
~F"""
<a
class={
"inline-block p-0 bg-transparent border-none no-underline
#{@class}",
"inline-block p-0 bg-transparent border-none no-underline",
"text-piccolo-100 hover:text-piccolo-80 focus:text-piccolo-80 active:text-piccolo-80":
!@disabled and !@optional and !@secondary,
"cursor-pointer": !@disabled,
"cursor-not-allowed opacity-25": @disabled,
"text-trunks-100": @secondary,
"text-trunks-100 hover:text-piccolo-80 focus:text-bulma-100 active:text-bulma-100": @optional
"text-trunks-100 hover:text-piccolo-80 focus:text-bulma-100 active:text-bulma-100": @optional,
"#{@class}": true
}
data-phx-link="patch"
data-phx-link-state="push"
Expand Down
67 changes: 20 additions & 47 deletions lib/moon_web/pages/components/checkbox_page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,6 @@ defmodule MoonWeb.Pages.Components.CheckboxPage do
}
]

def mount(params, _session, socket) do
user_changeset =
User.changeset(%User{}, %{
agrees_to_terms_of_service: false,
agrees_to_marketing_emails: true
})

{:ok,
assign(socket,
user_changeset: user_changeset,
theme_name: params["theme_name"] || "sportsbet-dark",
active_page: __MODULE__
)}
end

def handle_params(_params, uri, socket) do
{:noreply, assign(socket, uri: uri)}
end

def render(assigns) do
~F"""
<Page theme_name={@theme_name} active_page={@active_page} breadcrumbs={@breadcrumbs}>
Expand Down Expand Up @@ -86,46 +67,38 @@ defmodule MoonWeb.Pages.Components.CheckboxPage do
"""
end

def get_agrees_to_terms_of_service(user_changeset) do
if user_changeset.changes[:agrees_to_terms_of_service] == false do
false
else
user_changeset.data.agrees_to_terms_of_service
end
end

def handle_event(
"register_form_update",
%{
"user" => %{
"agrees_to_marketing_emails" => agrees_to_marketing_emails,
"agrees_to_terms_of_service" => agrees_to_terms_of_service
}
},
socket
) do
def mount(params, _session, socket) do
user_changeset =
User.changeset(%User{}, %{
agrees_to_terms_of_service: agrees_to_terms_of_service,
agrees_to_marketing_emails: agrees_to_marketing_emails
agrees_to_terms_of_service: false,
agrees_to_marketing_emails: true
})

{:noreply, assign(socket, user_changeset: user_changeset)}
{:ok,
assign(socket,
user_changeset: user_changeset,
theme_name: params["theme_name"] || "sportsbet-dark",
active_page: __MODULE__
)}
end

def handle_params(_params, uri, socket) do
{:noreply, assign(socket, uri: uri)}
end

def get_agrees_to_terms_of_service(user_changeset) do
Moon.Components.Checkbox.is_true(user_changeset.changes[:agrees_to_terms_of_service]) ||
Moon.Components.Checkbox.is_true(user_changeset.data.agrees_to_terms_of_service)
end

def handle_event(
"register_form_update",
%{
"user" => %{
"agrees_to_terms_of_service" => agrees_to_terms_of_service
}
"user" => params
},
socket
) do
user_changeset =
User.changeset(%User{}, %{
agrees_to_terms_of_service: agrees_to_terms_of_service
})
user_changeset = User.changeset(%User{}, params)

{:noreply, assign(socket, user_changeset: user_changeset)}
end
Expand Down

0 comments on commit 103d482

Please sign in to comment.