Skip to content

Commit

Permalink
add item to custom list #425 for #165
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Nov 1, 2023
1 parent 7f7c78b commit a58edec
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 28 deletions.
4 changes: 2 additions & 2 deletions lib/app/list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ defmodule App.List do
`add_item_to_list/3` adds the `item.cid` to the `list.cid` for the given `person_id`.
"""
def add_item_to_list(item_cid, list_cid, person_id) do
list = get_list_by_cid!(list_cid)
prev_seq = get_list_seq(list)
list = get_list_by_cid!(list_cid) |> dbg()
prev_seq = get_list_seq(list) |> dbg()
seq = [item_cid | prev_seq] |> Enum.join(",")
update_list(list, %{seq: seq, person_id: person_id})
end
Expand Down
2 changes: 0 additions & 2 deletions lib/app_web/controllers/auth_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ defmodule AppWeb.AuthController do
import Phoenix.Component, only: [assign_new: 3]

def on_mount(:default, _params, %{"jwt" => jwt} = _session, socket) do
dbg(jwt)

{:cont,
AuthPlug.assign_jwt_to_socket(socket, &Phoenix.Component.assign_new/3, jwt)}
end
Expand Down
2 changes: 1 addition & 1 deletion lib/app_web/controllers/list_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule AppWeb.ListController do
person_id = conn.assigns[:person][:id] || 0
lists = List.get_lists_for_person(person_id)

render(conn, "index.html", lists: lists)
render(conn, "index.html", lists: lists, custom_list: false)
end

def new(conn, _params) do
Expand Down
3 changes: 2 additions & 1 deletion lib/app_web/controllers/tag_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ defmodule AppWeb.TagController do

render(conn, "index.html",
tags: tags,
lists: App.List.get_lists_for_person(person_id)
lists: App.List.get_lists_for_person(person_id),
custom_list: false
)
end

Expand Down
47 changes: 35 additions & 12 deletions lib/app_web/live/app_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,39 @@ defmodule AppWeb.AppLive do
@stats_topic "stats"

defp get_list_cid(assigns), do: assigns[:list_cid]
defp get_list_name(assigns), do: assigns[:list_name]

defp list_cid_from_url_params(params) do
dbg(Map.get(params, "list_cid"))
if Map.has_key?(params, "list_cid"), do: Map.get(params, "list_cid", nil)
end

@impl true
def mount(_params, _session, socket) do
def mount(params, _session, socket) do
# subscribe to the channel
if connected?(socket), do: AppWeb.Endpoint.subscribe(@topic)
AppWeb.Endpoint.subscribe(@stats_topic)

person_id = Person.get_person_id(socket.assigns)

# Create or Get the "all" list for the person_id
all_list = App.List.get_all_list_for_person(person_id)
custom_list = list_cid_from_url_params(params)
list_cid = if custom_list == nil do
# Create or Get the "all" list for the person_id
all_list = App.List.get_all_list_for_person(person_id)

# Temporary function to add All *existing* items to the "All" list:
App.List.add_all_items_to_all_list_for_person_id(person_id)
# Temporary function to add All *existing* items to the "All" list:
App.List.add_all_items_to_all_list_for_person_id(person_id)

# return the "all" list cid
all_list.cid
else
custom_list
end
lists = App.List.get_lists_for_person(person_id)
list = Enum.find(lists, fn list -> list.cid == list_cid end)

# Assigns
items = Item.items_with_timers(person_id)
items = Item.items_with_timers(person_id, list_cid)
tags = Tag.list_person_tags(person_id)
selected_tags = []
draft_item = Item.get_draft_item(person_id)
Expand All @@ -41,8 +57,10 @@ defmodule AppWeb.AppLive do
editing: nil,
filter: "active",
filter_tag: nil,
list_cid: all_list.cid,
lists: App.List.get_lists_for_person(person_id),
custom_list: custom_list,
list_cid: list_cid,
list_name: list.name,
lists: lists,
tags: tags,
selected_tags: selected_tags,
text_value: draft_item.text || "",
Expand All @@ -65,16 +83,20 @@ defmodule AppWeb.AppLive do
def handle_event("create", %{"text" => text}, socket) do
person_id = Person.get_person_id(socket.assigns)

{:ok, %{model: _item}} =
{:ok, %{model: item}} =
Item.create_item_with_tags(%{
text: text,
person_id: person_id,
status: 2,
tags: socket.assigns.selected_tags
})

# Add this newly created `item` to the "All" list:
# App.ListItem.add_item_to_all_list(item)
# Add this newly created `item` to the list current list:
list_cid = get_list_cid(socket.assigns)
list_name = get_list_name(socket.assigns)
if list_name !== "all" do
App.List.add_item_to_list(item.cid, list_cid, person_id)
end

draft = Item.get_draft_item(person_id)
Item.update_draft(draft, %{text: ""})
Expand Down Expand Up @@ -347,7 +369,8 @@ defmodule AppWeb.AppLive do
@impl true
def handle_info(%Broadcast{event: "update", payload: payload}, socket) do
person_id = Person.get_person_id(socket.assigns)
items = Item.items_with_timers(person_id)
list_cid = get_list_cid(socket.assigns)
items = Item.items_with_timers(person_id, list_cid)
is_editing_item = socket.assigns.editing

# If the item is being edited, we update the timer list of the item being edited.
Expand Down
3 changes: 2 additions & 1 deletion lib/app_web/live/stats_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ defmodule AppWeb.StatsLive do
metrics: metrics,
sort_column: :person_id,
sort_order: :asc,
lists: App.List.get_lists_for_person(person_id)
lists: App.List.get_lists_for_person(person_id),
custom_list: false
)}
end

Expand Down
22 changes: 13 additions & 9 deletions lib/app_web/templates/nav/nav.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,20 @@

<div class="flex flex-1 items-center justify-center">
<div class="flex flex-shrink-0 items-center">
<a href="/" class="flex items-center">
<img
src="https://dwyl.com/img/common/dwyl-heart-only-logo.png"
height="32"
width="32"
alt="dwyl logo"
/>
</a>
<%= if @custom_list do %>
<h1 class="text-white text-2xl"> <%= list_name(@list_name) %></h1>
<% else %> <!-- show the @dwyl logo and link to root -->
<a href="/" class="flex items-center">
<img
src="https://dwyl.com/img/common/dwyl-heart-only-logo.png"
height="32"
width="32"
alt="dwyl logo"
/>
</a>
<% end %>
</div>
</div>
</div>

<div class="absolute inset-y-0 right-0 flex items-center">
<!-- Mobile menu button-->
Expand Down

0 comments on commit a58edec

Please sign in to comment.