-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: created
Cycle
schema in Schools
context
adjusted files generated by `mix phx.gen.live`, supporting DB constraints
- Loading branch information
Showing
13 changed files
with
637 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule Lanttern.Schools.Cycle do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
schema "school_cycles" do | ||
field :name, :string | ||
field :start_at, :date | ||
field :end_at, :date | ||
belongs_to :school, Lanttern.Schools.School | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(cycle, attrs) do | ||
cycle | ||
|> cast(attrs, [:name, :start_at, :end_at, :school_id]) | ||
|> validate_required([:name, :start_at, :end_at, :school_id]) | ||
|> check_constraint( | ||
:end_at, | ||
name: :cycle_end_date_is_greater_than_start_date, | ||
message: "End date should be greater than start date" | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
lib/lanttern_web/live/admin/cycle_live/form_component.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
defmodule LantternWeb.Admin.CycleLive.FormComponent do | ||
use LantternWeb, :live_component | ||
|
||
alias Lanttern.Schools | ||
import LantternWeb.SchoolsHelpers | ||
|
||
@impl true | ||
def render(assigns) do | ||
~H""" | ||
<div> | ||
<.header> | ||
<%= @title %> | ||
<:subtitle>Use this form to manage cycle records in your database.</:subtitle> | ||
</.header> | ||
<.simple_form | ||
for={@form} | ||
id="cycle-form" | ||
phx-target={@myself} | ||
phx-change="validate" | ||
phx-submit="save" | ||
> | ||
<.input | ||
field={@form[:school_id]} | ||
type="select" | ||
label="School" | ||
prompt="Select school" | ||
options={@school_options} | ||
/> | ||
<.input field={@form[:name]} type="text" label="Name" /> | ||
<.input field={@form[:start_at]} type="date" label="Start at" /> | ||
<.input field={@form[:end_at]} type="date" label="End at" /> | ||
<:actions> | ||
<.button phx-disable-with="Saving...">Save Cycle</.button> | ||
</:actions> | ||
</.simple_form> | ||
</div> | ||
""" | ||
end | ||
|
||
@impl true | ||
def update(%{cycle: cycle} = assigns, socket) do | ||
changeset = Schools.change_cycle(cycle) | ||
|
||
{:ok, | ||
socket | ||
|> assign(assigns) | ||
|> assign(:school_options, generate_school_options()) | ||
|> assign_form(changeset)} | ||
end | ||
|
||
@impl true | ||
def handle_event("validate", %{"cycle" => cycle_params}, socket) do | ||
changeset = | ||
socket.assigns.cycle | ||
|> Schools.change_cycle(cycle_params) | ||
|> Map.put(:action, :validate) | ||
|
||
{:noreply, assign_form(socket, changeset)} | ||
end | ||
|
||
def handle_event("save", %{"cycle" => cycle_params}, socket) do | ||
save_cycle(socket, socket.assigns.action, cycle_params) | ||
end | ||
|
||
defp save_cycle(socket, :edit, cycle_params) do | ||
case Schools.update_cycle(socket.assigns.cycle, cycle_params) do | ||
{:ok, cycle} -> | ||
notify_parent({:saved, cycle}) | ||
|
||
{:noreply, | ||
socket | ||
|> put_flash(:info, "Cycle updated successfully") | ||
|> push_patch(to: socket.assigns.patch)} | ||
|
||
{:error, %Ecto.Changeset{} = changeset} -> | ||
{:noreply, assign_form(socket, changeset)} | ||
end | ||
end | ||
|
||
defp save_cycle(socket, :new, cycle_params) do | ||
case Schools.create_cycle(cycle_params) do | ||
{:ok, cycle} -> | ||
notify_parent({:saved, cycle}) | ||
|
||
{:noreply, | ||
socket | ||
|> put_flash(:info, "Cycle created successfully") | ||
|> push_patch(to: socket.assigns.patch)} | ||
|
||
{:error, %Ecto.Changeset{} = changeset} -> | ||
{:noreply, assign_form(socket, changeset)} | ||
end | ||
end | ||
|
||
defp assign_form(socket, %Ecto.Changeset{} = changeset) do | ||
assign(socket, :form, to_form(changeset)) | ||
end | ||
|
||
defp notify_parent(msg), do: send(self(), {__MODULE__, msg}) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
defmodule LantternWeb.Admin.CycleLive.Index do | ||
use LantternWeb, {:live_view, layout: :admin} | ||
|
||
alias Lanttern.Schools | ||
alias Lanttern.Schools.Cycle | ||
|
||
@impl true | ||
def mount(_params, _session, socket) do | ||
{:ok, stream(socket, :school_cycles, Schools.list_school_cycles())} | ||
end | ||
|
||
@impl true | ||
def handle_params(params, _url, socket) do | ||
{:noreply, apply_action(socket, socket.assigns.live_action, params)} | ||
end | ||
|
||
defp apply_action(socket, :edit, %{"id" => id}) do | ||
socket | ||
|> assign(:page_title, "Edit Cycle") | ||
|> assign(:cycle, Schools.get_cycle!(id)) | ||
end | ||
|
||
defp apply_action(socket, :new, _params) do | ||
socket | ||
|> assign(:page_title, "New Cycle") | ||
|> assign(:cycle, %Cycle{}) | ||
end | ||
|
||
defp apply_action(socket, :index, _params) do | ||
socket | ||
|> assign(:page_title, "Listing School cycles") | ||
|> assign(:cycle, nil) | ||
end | ||
|
||
@impl true | ||
def handle_info({LantternWeb.Admin.CycleLive.FormComponent, {:saved, cycle}}, socket) do | ||
{:noreply, stream_insert(socket, :school_cycles, cycle)} | ||
end | ||
|
||
@impl true | ||
def handle_event("delete", %{"id" => id}, socket) do | ||
cycle = Schools.get_cycle!(id) | ||
{:ok, _} = Schools.delete_cycle(cycle) | ||
|
||
{:noreply, stream_delete(socket, :school_cycles, cycle)} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<.header> | ||
Listing School cycles | ||
<:actions> | ||
<.link patch={~p"/admin/school_cycles/new"}> | ||
<.button>New Cycle</.button> | ||
</.link> | ||
</:actions> | ||
</.header> | ||
|
||
<.table | ||
id="school_cycles" | ||
rows={@streams.school_cycles} | ||
row_click={fn {_id, cycle} -> JS.navigate(~p"/admin/school_cycles/#{cycle}") end} | ||
> | ||
<:col :let={{_id, cycle}} label="Name"><%= cycle.name %></:col> | ||
<:col :let={{_id, cycle}} label="Start at"><%= cycle.start_at %></:col> | ||
<:col :let={{_id, cycle}} label="End at"><%= cycle.end_at %></:col> | ||
<:action :let={{_id, cycle}}> | ||
<div class="sr-only"> | ||
<.link navigate={~p"/admin/school_cycles/#{cycle}"}>Show</.link> | ||
</div> | ||
<.link patch={~p"/admin/school_cycles/#{cycle}/edit"}>Edit</.link> | ||
</:action> | ||
<:action :let={{id, cycle}}> | ||
<.link | ||
phx-click={JS.push("delete", value: %{id: cycle.id}) |> hide("##{id}")} | ||
data-confirm="Are you sure?" | ||
> | ||
Delete | ||
</.link> | ||
</:action> | ||
</.table> | ||
|
||
<.modal | ||
:if={@live_action in [:new, :edit]} | ||
id="cycle-modal" | ||
show | ||
on_cancel={JS.patch(~p"/admin/school_cycles")} | ||
> | ||
<.live_component | ||
module={LantternWeb.Admin.CycleLive.FormComponent} | ||
id={@cycle.id || :new} | ||
title={@page_title} | ||
action={@live_action} | ||
cycle={@cycle} | ||
patch={~p"/admin/school_cycles"} | ||
/> | ||
</.modal> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
defmodule LantternWeb.Admin.CycleLive.Show do | ||
use LantternWeb, {:live_view, layout: :admin} | ||
|
||
alias Lanttern.Schools | ||
|
||
@impl true | ||
def mount(_params, _session, socket) do | ||
{:ok, socket} | ||
end | ||
|
||
@impl true | ||
def handle_params(%{"id" => id}, _, socket) do | ||
{:noreply, | ||
socket | ||
|> assign(:page_title, page_title(socket.assigns.live_action)) | ||
|> assign(:cycle, Schools.get_cycle!(id))} | ||
end | ||
|
||
defp page_title(:show), do: "Show Cycle" | ||
defp page_title(:edit), do: "Edit Cycle" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<.header> | ||
Cycle <%= @cycle.id %> | ||
<:subtitle>This is a cycle record from your database.</:subtitle> | ||
<:actions> | ||
<.link patch={~p"/admin/school_cycles/#{@cycle}/show/edit"} phx-click={JS.push_focus()}> | ||
<.button>Edit cycle</.button> | ||
</.link> | ||
</:actions> | ||
</.header> | ||
|
||
<.list> | ||
<:item title="Name"><%= @cycle.name %></:item> | ||
<:item title="Start at"><%= @cycle.start_at %></:item> | ||
<:item title="End at"><%= @cycle.end_at %></:item> | ||
</.list> | ||
|
||
<.back navigate={~p"/admin/school_cycles"}>Back to school_cycles</.back> | ||
|
||
<.modal | ||
:if={@live_action == :edit} | ||
id="cycle-modal" | ||
show | ||
on_cancel={JS.patch(~p"/admin/school_cycles/#{@cycle}")} | ||
> | ||
<.live_component | ||
module={LantternWeb.Admin.CycleLive.FormComponent} | ||
id={@cycle.id} | ||
title={@page_title} | ||
action={@live_action} | ||
cycle={@cycle} | ||
patch={~p"/admin/school_cycles/#{@cycle}"} | ||
/> | ||
</.modal> |
Oops, something went wrong.