defmodule TasksComponent do
use PlannerWeb, :live_component
alias Planner.Tasks
alias Planner.Tasks.Task
def update(%{:changeset => changeset, :id => _id}, socket) do
{:ok, assign(socket, :changeset, changeset)}
end
def update(assigns, socket) do
socket =
socket
|> assign(assigns)
|> assign(:changeset, Tasks.change_task(%Task{}))
{:ok, socket}
end
def render(assigns) do
~L"""
<%= f = form_for(@changeset, "#", [phx_submit: "new-task"]) %>
<%= text_input(f,
:value,
placeholder: "add new task",
class: "input", autocomplete: "off"
)%>
<%= error_tag(f, :value) %>
<%= if(!is_nil(@active_plan)) do %>
<% end %>
<%= for task <- @tasks do %>
<%= live_component(@socket,
TaskComponent,
id: "task:#{task.id}",
task: task,
live_action: @live_action,
is_active: @active_task == task.id,
route_show_task: @route_show_task,
route_edit_task: @route_edit_task,
route_index_tasks: @route_index_tasks
)%>
<% end %>
"""
end
end
defmodule TaskComponent do
use Phoenix.LiveComponent
import PlannerWeb.Util
def render(assigns) do
~L"""
<%= live_patch("",
to: @route_index_tasks.(@socket),
class: "delete is-pulled-right"
) %>
<%= if(not is_nil(@task.due_at) or is_nil(@task.filed_at)) do %>
<%= if(not is_nil(@task.due_at)) do %>
due: <%= @task.due_at %>
<% end %>
<%= if(is_nil(@task.filed_at)) do %>
unfiled
<% end %>
<% end %>
<%= md_to_html(@task.value) %>
updated: <%= @task.updated_at %>
created: <%= @task.inserted_at %>
"""
end
end
defmodule TaskEditComponent do
use PlannerWeb, :live_component
alias Planner.Tasks
def update(%{:changeset => changeset, :id => _id}, socket) do
{:ok, assign(socket, :changeset, changeset)}
end
def update(assigns, socket) do
socket =
socket
|> assign(assigns)
|> assign(:changeset, Tasks.change_task(assigns.task))
{:ok, socket}
end
def render(assigns) do
~L"""
<%= f = form_for(@changeset, "#", [phx_submit: "save-task"]) %>
<%= hidden_input(f, :id) %>
<%= textarea(f,
:value,
required: true,
class: "textarea",
placeholder: "task",
autocomplete: "off"
) %>
<%= error_tag(f, :value) %>
<%= label(f, :due_at, class: "label") do %>
due (YYYY-MM-DD HH:MM:SS)
<% end %>
<%= text_input(f,
:due_at,
class: "input",
placeholder: "YYYY-MM-DD HH:MM:SS",
autocomplete: "off"
) %>
<%= error_tag(f, :due_at) %>
<%= submit("save", class: "button is-dark is-small") %>
"""
end
end