Skip to content

Commit

Permalink
feat: finish functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ruilopesm committed Mar 20, 2024
1 parent 1205dbe commit 799c0c9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 103 deletions.
53 changes: 24 additions & 29 deletions lib/atomic_web/components/field.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ defmodule AtomicWeb.Components.Field do
doc:
"The message to be displayed when there are no options available for inputs in a group (checkboxes or radio button)."

attr :rows, :string, default: "4", doc: "The number of rows for textarea inputs."
attr :rows, :integer, default: 4, doc: "The number of rows for textarea inputs."

attr :selected, :any, default: nil, doc: "The selected value for select inputs."

Expand All @@ -97,13 +97,11 @@ defmodule AtomicWeb.Components.Field do
def field(%{field: %HTML.FormField{} = field} = assigns) do
assigns
|> assign(field: nil, id: assigns.id || field.id)
# |> assign(:errors, Enum.map(field.errors, &translate_error(&1)))
|> assign(:errors, Enum.map(field.errors, &translate_error(&1)))
|> assign_new(:name, fn ->
if assigns.multiple && assigns.type not in ["checkbox-group", "radio-group"] do
field.name <> "[]"
else
field.name
end
if assigns.multiple && assigns.type not in ["checkbox-group", "radio-group"],
do: field.name <> "[]",
else: field.name
end)
|> assign_new(:value, fn -> field.value end)
|> assign_new(:label, fn -> humanize(field.field) end)
Expand Down Expand Up @@ -142,8 +140,8 @@ defmodule AtomicWeb.Components.Field do
<%= HTML.Form.options_for_select(@options, @selected || @value) %>
</select>
<%!-- <.field_error :for{msg <- @errors}><%= msg %><./field_error> --%>
<%!-- <.field_help_text help_text={@help_text} /> --%>
<.field_error :for={msg <- @errors}><%= msg %></.field_error>
<.field_help_text help_text={@help_text} />
</.field_wrapper>
"""
end
Expand All @@ -159,8 +157,8 @@ defmodule AtomicWeb.Components.Field do
<%= Phoenix.HTML.Form.normalize_value("textarea", @value) %>
</textarea>
<%!-- <.field_error :for{msg <- @errors}><%= msg %><./field_error> --%>
<%!-- <.field_help_text help_text={@help_text} /> --%>
<.field_error :for={msg <- @errors}><%= msg %></.field_error>
<.field_help_text help_text={@help_text} />
</.field_wrapper>
"""
end
Expand All @@ -182,8 +180,8 @@ defmodule AtomicWeb.Components.Field do
<div><%= @label %></div>
</label>
<%!-- <.field_error :for{msg <- @errors}><%= msg %><./field_error> --%>
<%!-- <.field_help_text help_text={@help_text} /> --%>
<.field_error :for={msg <- @errors}><%= msg %></.field_error>
<.field_help_text help_text={@help_text} />
</.field_wrapper>
"""
end
Expand Down Expand Up @@ -231,8 +229,8 @@ defmodule AtomicWeb.Components.Field do
<% end %>
</div>
<%!-- <.field_error :for={msg <- @errors}><%= msg %></.field_error> --%>
<%!-- <.field_help_text help_text={@help_text} /> --%>
<.field_error :for={msg <- @errors}><%= msg %></.field_error>
<.field_help_text help_text={@help_text} />
</.field_wrapper>
"""
end
Expand Down Expand Up @@ -269,8 +267,8 @@ defmodule AtomicWeb.Components.Field do
<% end %>
</div>
<%!-- <.field_error :for={msg <- @errors}><%= msg %></.field_error> --%>
<%!-- <.field_help_text help_text={@help_text} /> --%>
<.field_error :for={msg <- @errors}><%= msg %></.field_error>
<.field_help_text help_text={@help_text} />
</.field_wrapper>
"""
end
Expand All @@ -290,7 +288,9 @@ defmodule AtomicWeb.Components.Field do
<.field_label required={@required} for={@id} class={@label_class}>
<%= @label %>
</.field_label>
<input type={@type} name={@name} id={@id} value={Phoenix.HTML.Form.normalize_value(@type, @value)} class={@class} required={@required} {@rest} />
<.field_error :for={msg <- @errors}><%= msg %></.field_error>
<.field_help_text help_text={@help_text} />
</.field_wrapper>
Expand Down Expand Up @@ -355,19 +355,14 @@ defmodule AtomicWeb.Components.Field do
defp get_class_for_type("range"), do: "atomic-range-input"
defp get_class_for_type(_), do: "atomic-text-input"

# TODO: translate_error/1 function

@doc """
Converts an attribute/form field into its humanize version.
iex> humanize(:username)
"Username"
iex> humanize(:created_at)
"Created at"
iex> humanize("user_id")
"User"
defp translate_error({msg, opts}) do
if count = opts[:count] do
Gettext.dngettext(AtomicWeb.Gettext, "errors", msg, msg, count, opts)
else
Gettext.dgettext(AtomicWeb.Gettext, "errors", msg, opts)
end
end

""" && false
defp humanize(atom) when is_atom(atom), do: humanize(Atom.to_string(atom))

defp humanize(bin) when is_binary(bin) do
Expand Down
68 changes: 0 additions & 68 deletions lib/atomic_web/components/forms.ex

This file was deleted.

13 changes: 7 additions & 6 deletions lib/atomic_web/live/activity_live/form_component.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div class="relative py-5 border-b border-gray-200 sm:py-6">
<div class="flex flex-col gap-y-3 lg:self-end">
<div class="w-full">
<.field type="text" field={@form[:title]} placeholder="Choose a title" phx-debounce="blur" />
<.field type="text" field={@form[:title]} placeholder="Choose a title" required />
</div>
</div>
</div>
Expand All @@ -16,21 +16,21 @@
<div class="flex flex-col">
<div class="flex pt-6 flex-col md:flex-row justify-center md:justify-start">
<div class="flex flex-col gap-y-1">
<.field type="datetime-local" field={@form[:start]} label="Starting date" />
<.field type="datetime-local" field={@form[:start]} label="Starting date" required />
</div>

<div class="flex flex-col md:ml-8 gap-y-1 mt-4 sm:mt-0">
<.field type="datetime-local" field={@form[:finish]} label="Ending date" />
<.field type="datetime-local" field={@form[:finish]} label="Ending date" required />
</div>
</div>

<div class="flex mt-4 flex-col md:flex-row justify-center md:justify-start">
<div class="flex flex-col gap-y-1">
<.field type="number" field={@form[:minimum_entries]} label="Minimum entries" placeholder="Choose minimum entries" />
<.field type="number" field={@form[:minimum_entries]} label="Minimum entries" placeholder="Choose minimum entries" required />
</div>

<div class="flex flex-col md:ml-8 gap-y-1 mt-4 sm:mt-0">
<.field type="number" field={@form[:maximum_entries]} label="Maximum entries" placeholder="Choose maximum entries" />
<.field type="number" field={@form[:maximum_entries]} label="Maximum entries" placeholder="Choose maximum entries" required />
</div>
</div>

Expand All @@ -51,13 +51,14 @@
</div>

<div class="flex-grow mt-4">
<.field type="textarea" field={@form[:description]} label="Description" placeholder="Choose description" rows={15} />
<.field type="textarea" field={@form[:description]} label="Description" placeholder="Choose description" rows={15} required />
</div>
</div>
</div>

<div class="flex-grow mt-4">
<div class="flex justify-center lg:justify-end">
<!-- TODO: Implement a button with spinner when saving -->
<.button type="submit" phx-disable-with="Saving...">Save</.button>
</div>
</div>
Expand Down

0 comments on commit 799c0c9

Please sign in to comment.