WIP
This commit is contained in:
parent
0468b926c7
commit
ca5546c35e
4 changed files with 38 additions and 10 deletions
|
@ -5,8 +5,8 @@ defmodule ChoreTracker.Chores do
|
|||
|
||||
import Ecto.Query, warn: false
|
||||
alias ChoreTracker.Repo
|
||||
|
||||
alias ChoreTracker.Chores.Chore
|
||||
alias ChoreTracker.Accounts.User
|
||||
|
||||
@doc """
|
||||
Returns the list of chores.
|
||||
|
@ -145,9 +145,10 @@ defmodule ChoreTracker.Chores do
|
|||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def create_chore_log(attrs \\ %{}) do
|
||||
|
||||
def log_chore_execution(%User{} = user, %Chore{} = chore) do
|
||||
%ChoreLog{}
|
||||
|> ChoreLog.changeset(attrs)
|
||||
|> ChoreLog.changeset(%{user_id: user.id, chore_id: chore.id})
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
|
|
|
@ -17,6 +17,6 @@ defmodule ChoreTracker.Chores.Chore do
|
|||
def changeset(chore, attrs) do
|
||||
chore
|
||||
|> cast(attrs, [:name, :description, :emoji, :period, :period_unit, :starts_at])
|
||||
|> validate_required([:name, :description, :emoji, :period, :period_unit, :starts_at])
|
||||
|> validate_required([:name, :emoji, :period, :period_unit, :starts_at])
|
||||
end
|
||||
end
|
||||
|
|
|
@ -12,10 +12,9 @@
|
|||
rows={@streams.chores}
|
||||
row_click={fn {_id, chore} -> JS.navigate(~p"/manage/chores/#{chore}") end}
|
||||
>
|
||||
<:col :let={{_id, chore}}><%= chore.emoji %></:col>
|
||||
<:col :let={{_id, chore}} label="Chore"><%= chore.name %></:col>
|
||||
<:col :let={{_id, chore}} label="Period"><%= chore.period %></:col>
|
||||
<:col :let={{_id, chore}} label="Period unit"><%= chore.period_unit %></:col>
|
||||
<:col :let={{_id, chore}}><span class="text-2xl"><%= chore.emoji %></span></:col>
|
||||
<:col :let={{_id, chore}} label="Chore"><strong><%= chore.name %></strong></:col>
|
||||
<:col :let={{_id, chore}} label="Every"><%= chore.period %> <%= chore.period_unit %></:col>
|
||||
<:col :let={{_id, chore}} label="Starts at"><%= chore.starts_at %></:col>
|
||||
<:action :let={{_id, chore}}>
|
||||
<div class="sr-only">
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
defmodule ChoreTrackerWeb.OverviewLive do
|
||||
use ChoreTrackerWeb, :live_view
|
||||
alias ChoreTracker.Chores
|
||||
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
|
@ -8,13 +9,40 @@ defmodule ChoreTrackerWeb.OverviewLive do
|
|||
</.header>
|
||||
|
||||
<.table id="chores" rows={@chores}>
|
||||
<:col :let={chore}><span class="text-2xl"><%= chore.emoji %></span></:col>
|
||||
<:col :let={chore} label="Chore"><%= chore.name %></:col>
|
||||
<:col :let={chore} label="Last executed">...</:col>
|
||||
<:action :let={chore}>
|
||||
<.link
|
||||
phx-click={JS.push("log_execution", value: %{id: chore.id})}
|
||||
data-confirm="Are you sure?"
|
||||
>
|
||||
Log execution
|
||||
</.link>
|
||||
</:action>
|
||||
</.table>
|
||||
"""
|
||||
end
|
||||
|
||||
def mount(_params, _session, socket) do
|
||||
chores = ChoreTracker.Chores.list_chores()
|
||||
{:ok, assign(socket, chores: chores)}
|
||||
{:ok, socket |> load_chores()}
|
||||
end
|
||||
|
||||
def handle_event("log_execution", %{"id" => chore_id}, socket) do
|
||||
%{current_user: user} = socket.assigns
|
||||
chore = Chores.get_chore!(chore_id)
|
||||
|
||||
case Chores.log_chore_execution(user, chore) do
|
||||
{:ok, _chore_log} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Chore execution logged.")
|
||||
|> load_chores()}
|
||||
end
|
||||
end
|
||||
|
||||
def load_chores(socket) do
|
||||
chores = Chores.list_chores()
|
||||
assign(socket, chores: chores)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue