Skip to content

Commit

Permalink
fix: checkboxes fix
Browse files Browse the repository at this point in the history
  • Loading branch information
theycallmehero committed Nov 25, 2021
1 parent 0b50e6c commit 31522f3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 37 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 == "true")
)}
</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
35 changes: 4 additions & 31 deletions lib/moon_web/pages/components/checkbox_page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defmodule MoonWeb.Pages.Components.CheckboxPage do
user_changeset =
User.changeset(%User{}, %{
agrees_to_terms_of_service: false,
agrees_to_marketing_emails: true
agrees_to_marketing_emails: false
})

{:ok,
Expand Down Expand Up @@ -87,45 +87,18 @@ 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
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_marketing_emails" => agrees_to_marketing_emails,
"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,
agrees_to_marketing_emails: agrees_to_marketing_emails
})

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

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

{:noreply, assign(socket, user_changeset: user_changeset)}
end
Expand Down
4 changes: 2 additions & 2 deletions lib/moon_web/pages/tutorials/add_data_using_form/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ defmodule MoonWeb.Pages.Tutorials.AddDataUsingForm.User do
field(:email, :string, default: "joe@doe.com")
field(:gender, :string, default: "male")
field(:document_filename)
field(:agrees_to_terms_of_service, :boolean, default: true)
field(:agrees_to_marketing_emails, :boolean, default: true)
field(:agrees_to_terms_of_service, :boolean, default: false)
field(:agrees_to_marketing_emails, :boolean, default: false)
end

def changeset(user = %User{}, params \\ %{}) do
Expand Down

0 comments on commit 31522f3

Please sign in to comment.