Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: checkboxes fix #188

Merged
merged 3 commits into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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