-
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.
add live views for activity and journal entry (#23)
- Loading branch information
Showing
26 changed files
with
834 additions
and
19 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
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
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
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,88 @@ | ||
defmodule JourniPlanWeb.ActivityLive.FormComponent do | ||
use JourniPlanWeb, :live_component | ||
|
||
alias JourniPlan.Itineraries | ||
|
||
@impl true | ||
def render(assigns) do | ||
~H""" | ||
<div> | ||
<.header> | ||
<%= @title %> | ||
<:subtitle>Use this form to manage activity records in your database.</:subtitle> | ||
</.header> | ||
<.simple_form | ||
for={@form} | ||
id="activity-form" | ||
phx-target={@myself} | ||
phx-change="validate" | ||
phx-submit="save" | ||
> | ||
<.input field={@form[:name]} type="text" label="Name" /> | ||
<.input field={@form[:description]} type="text" label="Description" /> | ||
<.input field={@form[:start_time]} type="datetime-local" label="Start time" /> | ||
<.input field={@form[:end_time]} type="datetime-local" label="End time" /> | ||
<:actions> | ||
<.button phx-disable-with="Saving...">Save Activity</.button> | ||
</:actions> | ||
</.simple_form> | ||
</div> | ||
""" | ||
end | ||
|
||
@impl true | ||
def update(%{activity: activity, action: action} = assigns, socket) do | ||
{:ok, | ||
socket | ||
|> assign(assigns) | ||
|> assign(:form, to_form(Itineraries.change_activity(activity, action), as: "activity"))} | ||
|
||
end | ||
|
||
@impl true | ||
def handle_event("validate", %{"activity" => activity_params}, socket) do | ||
changeset = Itineraries.change_activity(socket.assigns.activity, socket.assigns.action, activity_params) | ||
{:noreply, assign(socket, form: to_form(changeset, action: :validate, as: "activity"))} | ||
end | ||
|
||
def handle_event("save", %{"activity" => activity_params}, socket) do | ||
save_activity(socket, socket.assigns.action, activity_params) | ||
end | ||
|
||
defp save_activity(socket, :edit, activity_params) do | ||
case Itineraries.update_activity(socket.assigns.activity, activity_params) do | ||
{:ok, activity} -> | ||
notify_parent({:saved, activity}) | ||
|
||
{:noreply, | ||
socket | ||
|> put_flash(:info, "Activity updated successfully") | ||
|> push_patch(to: socket.assigns.patch)} | ||
|
||
{:error, %Ecto.Changeset{} = changeset} -> | ||
{:noreply, assign(socket, form: to_form(changeset))} | ||
end | ||
end | ||
|
||
defp save_activity(socket, :new, activity_params) do | ||
user_id = socket.assigns.current_user.id | ||
activity_params = Map.put(activity_params, "user_id", user_id) | ||
activity_params = Map.put(activity_params, "itinerary_uuid", socket.assigns.itinerary_id) | ||
|
||
case Itineraries.create_activity(activity_params) do | ||
{:ok, activity} -> | ||
notify_parent({:saved, activity}) | ||
|
||
{:noreply, | ||
socket | ||
|> put_flash(:info, "Activity created successfully") | ||
|> push_patch(to: socket.assigns.patch)} | ||
|
||
{:error, %Ecto.Changeset{} = changeset} -> | ||
{:noreply, assign(socket, form: to_form(changeset))} | ||
end | ||
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,56 @@ | ||
defmodule JourniPlanWeb.ActivityLive.Index do | ||
use JourniPlanWeb, :live_view | ||
|
||
alias JourniPlan.Itineraries | ||
alias JourniPlan.Itineraries.Projections.Activity | ||
|
||
@impl true | ||
def mount(params, _session, socket) do | ||
current_user = socket.assigns.current_user | ||
|
||
{ | ||
:ok, | ||
socket | ||
|> assign(:current_user, current_user) | ||
|> assign(:itinerary_id, params["id"]) | ||
|> stream_configure(:activities, dom_id: & &1.uuid) | ||
|> stream(:activities, Itineraries.list_user_activities(current_user.id)) | ||
} | ||
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 Activity") | ||
|> assign(:activity, Itineraries.get_activity!(id)) | ||
end | ||
|
||
defp apply_action(socket, :new, _params) do | ||
socket | ||
|> assign(:page_title, "New Activity") | ||
|> assign(:activity, %Activity{}) | ||
end | ||
|
||
defp apply_action(socket, :index, _params) do | ||
socket | ||
|> assign(:page_title, "Listing Activities") | ||
|> assign(:activity, nil) | ||
end | ||
|
||
@impl true | ||
def handle_info({JourniPlanWeb.ActivityLive.FormComponent, {:saved, activity}}, socket) do | ||
{:noreply, stream_insert(socket, :activities, activity)} | ||
end | ||
|
||
@impl true | ||
def handle_event("delete", %{"id" => id}, socket) do | ||
activity = Itineraries.get_activity!(id) | ||
{:ok, _} = Itineraries.delete_activity(activity) | ||
|
||
{:noreply, stream_delete(socket, :activities, activity)} | ||
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,46 @@ | ||
<.header> | ||
Listing Activities | ||
<:actions> | ||
<.link patch={~p"/activities/new"}> | ||
<.button>New Activity</.button> | ||
</.link> | ||
</:actions> | ||
</.header> | ||
|
||
<.table | ||
id="activities" | ||
rows={@streams.activities} | ||
row_click={fn {_id, activity} -> JS.navigate(~p"/activities/#{activity}") end} | ||
> | ||
<:col :let={{_id, activity}} label="Name"><%= activity.name %></:col> | ||
<:col :let={{_id, activity}} label="Description"><%= activity.description %></:col> | ||
<:col :let={{_id, activity}} label="Start time"><%= activity.start_time %></:col> | ||
<:col :let={{_id, activity}} label="End time"><%= activity.end_time %></:col> | ||
<:action :let={{_id, activity}}> | ||
<div class="sr-only"> | ||
<.link navigate={~p"/activities/#{activity}"}>Show</.link> | ||
</div> | ||
<.link patch={~p"/activities/#{activity}/edit"}>Edit</.link> | ||
</:action> | ||
<:action :let={{id, activity}}> | ||
<.link | ||
phx-click={JS.push("delete", value: %{id: activity.uuid}) |> hide("##{id}")} | ||
data-confirm="Are you sure?" | ||
> | ||
Delete | ||
</.link> | ||
</:action> | ||
</.table> | ||
|
||
<.modal :if={@live_action in [:new, :edit]} id="activity-modal" show on_cancel={JS.patch(~p"/activities")}> | ||
<.live_component | ||
module={JourniPlanWeb.ActivityLive.FormComponent} | ||
id={@activity.uuid || :new} | ||
title={@page_title} | ||
action={@live_action} | ||
activity={@activity} | ||
current_user={@current_user} | ||
itinerary_id={@itinerary_id} | ||
patch={~p"/activities"} | ||
/> | ||
</.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 JourniPlanWeb.ActivityLive.Show do | ||
use JourniPlanWeb, :live_view | ||
|
||
alias JourniPlan.Itineraries | ||
|
||
@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(:activity, Itineraries.get_activity!(id))} | ||
end | ||
|
||
defp page_title(:show), do: "Show Activity" | ||
defp page_title(:edit), do: "Edit Activity" | ||
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,29 @@ | ||
<.header> | ||
Activity <%= @activity.uuid %> | ||
<:subtitle>This is a activity record from your database.</:subtitle> | ||
<:actions> | ||
<.link patch={~p"/activities/#{@activity}/show/edit"} phx-click={JS.push_focus()}> | ||
<.button>Edit activity</.button> | ||
</.link> | ||
</:actions> | ||
</.header> | ||
|
||
<.list> | ||
<:item title="Name"><%= @activity.name %></:item> | ||
<:item title="Description"><%= @activity.description %></:item> | ||
<:item title="Start time"><%= @activity.start_time %></:item> | ||
<:item title="End time"><%= @activity.end_time %></:item> | ||
</.list> | ||
|
||
<.back navigate={~p"/activities"}>Back to activities</.back> | ||
|
||
<.modal :if={@live_action == :edit} id="activity-modal" show on_cancel={JS.patch(~p"/activities/#{@activity}")}> | ||
<.live_component | ||
module={JourniPlanWeb.ActivityLive.FormComponent} | ||
id={@activity.uuid} | ||
title={@page_title} | ||
action={@live_action} | ||
activity={@activity} | ||
patch={~p"/activities/#{@activity}"} | ||
/> | ||
</.modal> |
Oops, something went wrong.