Skip to content

Commit

Permalink
chore: clean up form
Browse files Browse the repository at this point in the history
  • Loading branch information
joaodiaslobo committed Nov 2, 2024
1 parent 086d795 commit 6ed55f1
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 20 deletions.
2 changes: 1 addition & 1 deletion assets/css/components/field.css
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
/* Text */

.safira-text-input {
@apply block w-full rounded-md text-dark border border-lightShade placeholder:text-darkMuted focus:outline-2 focus:border-lightShade ring-0 focus:outline-dark focus:outline-offset-2 dark:outline-darkShade dark:bg-dark dark:text-light dark:placeholder-lightMuted dark:focus:border-darkShade dark:border-darkShade focus:ring-0 dark:focus:outline-light;
@apply block w-full rounded-md text-dark border border-lightShade placeholder:text-darkMuted focus:outline-2 focus:border-lightShade ring-0 focus:outline-dark focus:outline-offset-2 dark:outline-darkShade dark:bg-dark dark:text-light dark:placeholder-lightMuted dark:focus:border-darkShade dark:border-darkShade focus:ring-0 dark:focus:outline-light dark:[color-scheme:dark];
}

/* Code */
Expand Down
16 changes: 14 additions & 2 deletions lib/safira/activities/activity.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@ defmodule Safira.Activities.Activity do
use Safira.Schema

@required_fields ~w(title date time_start time_end)a
@optional_fields ~w(description category_id location has_enrolments)a
@optional_fields ~w(description category_id location has_enrolments max_enrolments)a

@derive {
Flop.Schema,
filterable: [:title], sortable: [:title, :date], default_limit: 11
filterable: [:title],
sortable: [:timestamp],
default_limit: 11,
adapter_opts: [
compound_fields: [
timestamp: [:date, :time_start]
]
],
default_order: %{
order_by: [:timestamp],
order_directions: [:asc]
}
}

schema "activities" do
Expand All @@ -20,6 +31,7 @@ defmodule Safira.Activities.Activity do
field :time_start, :time
field :time_end, :time
field :has_enrolments, :boolean, default: false
field :max_enrolments, :integer, default: 0

belongs_to :category, Safira.Activities.ActivityCategory

Expand Down
53 changes: 39 additions & 14 deletions lib/safira_web/live/backoffice/schedule_live/form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,39 @@ defmodule SafiraWeb.Backoffice.ScheduleLive.FormComponent do
wrapper_class="col-span-1"
/>
</div>
<.field
field={@form[:category_id]}
type="select"
label="Category"
options={categories_options(@categories)}
wrapper_class="w-full"
/>
<div class="w-full flex gap-2">
<.field
field={@form[:category_id]}
type="select"
label="Category"
options={categories_options(@categories)}
wrapper_class="w-full"
/>
<.field
field={@form[:has_enrolments]}
type="checkbox"
label="Requires enrolment"
wrapper_class="w-full"
/>
<div class="w-full grid grid-cols-2">
<div class="w-full flex flex-col">
<.label>
<%= gettext("Enrolments") %>
</.label>
<p class="safira-form-help-text">
<%= gettext(
"Enable enrolments to allow participants to sign up for this activity."
) %>
</p>
<.field
field={@form[:has_enrolments]}
type="switch"
label=""
wrapper_class="w-full pt-3"
/>
</div>
<.field
:if={@enrolments_active}
field={@form[:max_enrolments]}
type="number"
label="Max enrolments"
wrapper_class="w-full"
/>
</div>
</div>
</div>
<:actions>
Expand All @@ -80,6 +99,7 @@ defmodule SafiraWeb.Backoffice.ScheduleLive.FormComponent do
{:ok,
socket
|> assign(assigns)
|> assign(:enrolments_active, activity.has_enrolments)
|> assign_new(:form, fn ->
to_form(Activities.change_activity(activity))
end)}
Expand All @@ -88,7 +108,12 @@ defmodule SafiraWeb.Backoffice.ScheduleLive.FormComponent do
@impl true
def handle_event("validate", %{"activity" => activity_params}, socket) do
changeset = Activities.change_activity(socket.assigns.activity, activity_params)
{:noreply, assign(socket, form: to_form(changeset, action: :validate))}

{:noreply,
assign(socket,
form: to_form(changeset, action: :validate),
enrolments_active: activity_params["has_enrolments"] != "false"
)}
end

def handle_event("save", %{"activity" => activity_params}, socket) do
Expand Down
4 changes: 2 additions & 2 deletions lib/safira_web/live/backoffice/schedule_live/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
<:col :let={{_id, activity}} sortable field={:title} label="Title">
<%= activity.title %>
</:col>
<:col :let={{_id, activity}} sortable field={:date} label="Date">
<:col :let={{_id, activity}} sortable field={:timestamp} label="Date">
<%= Timex.format!(activity.date, "{D}/{M}/{YYYY}") %>
</:col>
<:col :let={{_id, activity}} sortable field={:time} label="Time">
<:col :let={{_id, activity}} field={:time} label="Time">
<%= formatted_activity_times(activity) %>
</:col>
<:col :let={{_id, activity}} field={:category} label="Category">
Expand Down
3 changes: 2 additions & 1 deletion priv/repo/migrations/20241028131718_create_activities.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ defmodule Safira.Repo.Migrations.CreateActivities do
create table(:activities, primary_key: false) do
add :id, :binary_id, primary_key: true
add :title, :string, null: false
add :description, :string
add :description, :text
add :location, :string
add :date, :date, null: false
add :time_start, :time, null: false
add :time_end, :time, null: false
add :has_enrolments, :boolean, default: false, null: false
add :max_enrolments, :integer, default: 0, null: false
add :category_id, references(:activity_categories, on_delete: :nothing, type: :binary_id)

timestamps(type: :utc_datetime)
Expand Down

0 comments on commit 6ed55f1

Please sign in to comment.