Skip to content

Commit

Permalink
Merge pull request #1185 from animina-dating/use-validator-in-frontend
Browse files Browse the repository at this point in the history
Added Birthday Validator on frontend
  • Loading branch information
wintermeyer authored Dec 10, 2024
2 parents c971897 + af5f372 commit 8ea1be7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 30 deletions.
5 changes: 5 additions & 0 deletions lib/animina/ birthday_validator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ defmodule Animina.BirthdayValidator do
iex> BirthdayValidator.validate_birthday("15.08.2010")
{:error, "Birthday must be more than 18 years ago."}
"""

def validate_birthday("") do
{:error, "Birthday cannot be empty."}
end

def validate_birthday(birthday) do
with {:ok, date_parts} <- parse_birthday(birthday),
{:ok, date} <- validate_date(date_parts),
Expand Down
11 changes: 5 additions & 6 deletions lib/animina_web/components/beta_registration_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ defmodule AniminaWeb.BetaRegistrationComponents do
class="mt-6 space-y-6 group"
phx-change="validate_and_filter_potential_partners"
phx-submit="submit"
phx-debounce="500"
>
<h2 class="mt-3 text-2xl font-semibold dark:text-white">
<%= with_locale(@language, fn -> %>
Expand All @@ -50,7 +51,7 @@ defmodule AniminaWeb.BetaRegistrationComponents do
<div class="w-[100%] md:grid grid-cols-2 gap-8">
<.height_select f={f} language={@language} />
<.birthday_select f={f} language={@language} />
<.birthday_select f={f} language={@language} birthday_error={@birthday_error} />
<.zip_code_select f={f} language={@language} />
</div>
Expand Down Expand Up @@ -242,7 +243,7 @@ defmodule AniminaWeb.BetaRegistrationComponents do
<%= text_input(@f, :birthday,
class:
"block w-full rounded-md border-0 py-1.5 text-gray-900 dark:bg-gray-700 dark:text-white dark:[color-scheme:dark] shadow-sm ring-1 ring-inset placeholder:text-gray-400 focus:ring-2 focus:ring-inset sm:text-sm phx-no-feedback:ring-gray-300 phx-no-feedback:focus:ring-indigo-600 sm:leading-6 " <>
unless(get_field_errors(@f[:birthday], :birthday) == [],
unless(@birthday_error == nil,
do: "ring-red-600 focus:ring-red-600",
else: "ring-gray-300 focus:ring-indigo-600"
),
Expand All @@ -252,10 +253,8 @@ defmodule AniminaWeb.BetaRegistrationComponents do
"phx-debounce": "blur"
) %>
<.error :for={msg <- get_field_errors(@f[:birthday], :birthday)}>
<%= with_locale(@language, fn -> %>
<%= gettext("Date of birth") <> " " <> msg %>
<% end) %>
<.error :if={@birthday_error != nil}>
<%= @birthday_error %>
</.error>
</div>
</div>
Expand Down
19 changes: 16 additions & 3 deletions lib/animina_web/live/beta_register.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule AniminaWeb.BetaRegisterLive do
use AniminaWeb, :live_view
alias Animina.Accounts.User
alias Animina.BirthdayValidator
alias AniminaWeb.PotentialPartner
alias AshPhoenix.Form

Expand All @@ -13,6 +14,7 @@ defmodule AniminaWeb.BetaRegisterLive do
socket
|> assign(language: language)
|> assign(current_user: nil)
|> assign(birthday_error: nil)
|> assign(active_tab: "register")
|> assign(trigger_action: false)
|> assign(current_user_credit_points: 0)
Expand All @@ -30,9 +32,19 @@ defmodule AniminaWeb.BetaRegisterLive do
def handle_event("validate_and_filter_potential_partners", %{"user" => user}, socket) do
potential_partners = PotentialPartner.potential_partners_on_registration(user)

{:noreply,
socket
|> assign(:number_of_potential_partners, Enum.count(potential_partners))}
case BirthdayValidator.validate_birthday(user["birthday"]) do
{:ok, _} ->
{:noreply,
socket
|> assign(:birthday_error, nil)
|> assign(:number_of_potential_partners, Enum.count(potential_partners))}

{:error, reason} ->
{:noreply,
socket
|> assign(:birthday_error, reason)
|> assign(:number_of_potential_partners, Enum.count(potential_partners))}
end
end

defp default_user_params do
Expand All @@ -56,6 +68,7 @@ defmodule AniminaWeb.BetaRegisterLive do
<.initial_form
number_of_potential_partners={@number_of_potential_partners}
language={@language}
birthday_error={@birthday_error}
form={@form}
errors={@errors}
/>
Expand Down
44 changes: 23 additions & 21 deletions lib/animina_web/potential_partner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule AniminaWeb.PotentialPartner do

alias Animina.Accounts
alias Animina.Accounts.User
alias Animina.BirthdayValidator
alias Animina.GeoData.City
# alias Animina.Traits.UserFlags

Expand Down Expand Up @@ -187,27 +188,29 @@ defmodule AniminaWeb.PotentialPartner do
end

defp registration_partner_age_query(query, user) do
if user["birthday"] == "" do
query
else
age = calculate_age(convert_to_date(user["birthday"]))
case convert_to_date(user["birthday"]) do
{:ok, date} ->
age = calculate_age(date)

max_age = conditional_maximum_age(age, user["maximum_partner_age"])
min_age = conditional_minimum_age(age, user["minimum_partner_age"])
max_age = conditional_maximum_age(age, user["maximum_partner_age"])
min_age = conditional_minimum_age(age, user["minimum_partner_age"])

query
|> Ash.Query.filter(
fragment(
"date_part('year', age(current_date, ?))",
birthday
) >= ^min_age
)
|> Ash.Query.filter(
fragment(
"date_part('year', age(current_date, ?))",
birthday
) <= ^max_age
)
query
|> Ash.Query.filter(
fragment(
"date_part('year', age(current_date, ?))",
birthday
) >= ^min_age
)
|> Ash.Query.filter(
fragment(
"date_part('year', age(current_date, ?))",
birthday
) <= ^max_age
)

_ ->
query
end
end

Expand Down Expand Up @@ -358,8 +361,7 @@ defmodule AniminaWeb.PotentialPartner do
end

def convert_to_date(date_string) do
{:ok, date} = Date.from_iso8601(date_string)
date
BirthdayValidator.validate_birthday(date_string)
end

def calculate_age(birthdate) do
Expand Down

0 comments on commit 8ea1be7

Please sign in to comment.