Skip to content

Commit

Permalink
lowercase "All" to "all" for #356
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Aug 13, 2023
1 parent 5288630 commit 56d92d5
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 66 deletions.
8 changes: 4 additions & 4 deletions assets/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ input[type=radio].has-error:not(.phx-no-feedback) {


/* For the drag and drop feature */
.cursor-grab{
.cursor-grab {
cursor: grab;
}

.cursor-grabbing{
.cursor-grabbing {
cursor: grabbing;
}

.bg-yellow-300{
background-color: rgb(253 224 71);
.bg-teal-300 {
background-color: #5eead4;
}
9 changes: 5 additions & 4 deletions lib/app/list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ defmodule App.List do
|> Repo.all()
end

@default_lists ~w(All Meals Recipes Shopping Todo)
# Default Lists? discuss: github.com/dwyl/mvp/issues/401
@default_lists ~w(all goals fitness meals recipes reading shopping today Todo)
@doc """
`create_default_lists/1` create the default "All" list
for the `person_id` if it does not already exist.
Expand All @@ -96,7 +97,7 @@ defmodule App.List do
list_names = Enum.reduce(lists, [], fn l, acc -> [l.text | acc] end)
# Quick check for length of lists:
if length(list_names) < length(@default_lists) do
create_list_if_not_present(list_names, person_id)
create_list_if_not_exists(list_names, person_id)
# Re-fetch the list of lists for the person_id
get_person_lists(person_id)
else
Expand All @@ -106,10 +107,10 @@ defmodule App.List do
end

@doc """
`create_list_if_not_present/1` create the default "All" list
`create_list_if_not_exists/1` create the default "All" list
for the `person_id` if it does not already exist.
"""
def create_list_if_not_present(list_names, person_id) do
def create_list_if_not_exists(list_names, person_id) do
Enum.each(@default_lists, fn name ->
# Create the list if it does not already exists
unless Enum.member?(list_names, name) do
Expand Down
21 changes: 9 additions & 12 deletions lib/app/list_item.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ defmodule App.ListItem do
end

@doc """
`add_item_to_all_list/1` adds the `item` the "All" `list` for `person_id`.
`add_item_to_all_list/1` adds the `item` the "all" `list` for `person_id`.
"""
def add_item_to_all_list(item) do
all_list = App.List.get_list_by_text!(item.person_id, "All")
all_list = App.List.get_list_by_text!(item.person_id, "all")

%ListItem{
item: item,
Expand Down Expand Up @@ -118,10 +118,10 @@ defmodule App.ListItem do
# IO.inspect("move_item/3 -> id_from: #{id_from} | id_to: #{id_to} | list_id: #{list_id} | #{Useful.typeof(list_id)}")
item_from = App.Item.get_item!(id_from)
# dbg(item_from)
# For now we only have the "All" list, but soon we will have "PARA" 😉
# For now we only have the "all" list, but soon we will have "PARA" 😉
list =
if list_id == 0 do
App.List.get_list_by_text!(item_from.person_id, "All")
App.List.get_list_by_text!(item_from.person_id, "all")
else
App.List.get_list!(list_id)
end
Expand All @@ -141,18 +141,18 @@ defmodule App.ListItem do

@doc """
`get_items_on_all_list/1` retrieves a List of item.ids e.g: [8, 6, 4, 2]
that are on the "All" list for a given `person_id`.
that are on the "all" list for a given `person_id`.
This is relevant as we transition to having Lists.
But once all data is migrated we can delete this function and it's sister below.
"""
def get_items_on_all_list(person_id) do
# Retrieve list of `items` for the `person_id` on the "All" list:
# Retrieve list of `items` for the `person_id` on the "all" list:
sql = """
SELECT li.item_id
FROM list_items li
JOIN lists l ON li.list_id = l.id
WHERE li.person_id = $1
AND l.text = 'All'
AND l.text = 'all'
GROUP by li.item_id, li.list_id, l.text
"""

Expand All @@ -161,25 +161,22 @@ defmodule App.ListItem do
end

@doc """
`add_items_to_all_list/1` adds all `items` to the "All" `list`.
`add_items_to_all_list/1` adds all `items` to the "all" `list`.
This is a temporary function to migrate existing data (`items`)
to the new schema. Once we are certain that everyone using the MVP
has their data migrated, we can delete this function.
"""
def add_items_to_all_list(person_id) do
all_list = App.List.get_list_by_text!(person_id, "All")
all_list = App.List.get_list_by_text!(person_id, "all")
all_items = App.Item.all_items_for_person(person_id)
item_ids_in_all_list = get_items_on_all_list(person_id)

all_items
|> Enum.with_index()
|> Enum.each(fn {item, index} ->
unless Enum.member?(item_ids_in_all_list, item.id) do
# IO.inspect(item.id)
add_list_item(item, all_list, person_id, (index + 1) / 1)
end

# IO.puts("-------> #{index} => #{id} | #{person_id}")
end)
end
end
4 changes: 2 additions & 2 deletions lib/app_web/live/app_live.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@
x-on:dragend="selected = false; $dispatch('remove-highlight', {id: $el.id}); selectedItem = null; $dispatch('update-indexes', {fromItemId: $el.dataset.id})"
x-bind:class="selected ?? 'cursor-grabbing'"
x-on:dragover.throttle="$dispatch('dragoverItem', {selectedItemId: selectedItem.id, currentItem: $el})"
data-highlight={JS.add_class("bg-yellow-300")}
data-remove-highlight={JS.remove_class("bg-yellow-300")}
data-highlight={JS.add_class("bg-teal-300")}
data-remove-highlight={JS.remove_class("bg-teal-300")}
>
<div class="flex flex-row">
<svg
Expand Down
59 changes: 33 additions & 26 deletions test/app/list_items_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,45 @@ defmodule App.ListItemsTest do
assert list_item_removed.position == 999_999.999
end

test "add_items_to_all_list/1 to seed the All list" do
person_id = 0
all_list = App.List.get_list_by_text!(person_id, "All")
count_before = App.ListItem.next_position_on_list(all_list.id)
assert count_before == 1
@valid_attrs %{text: "Buy Bananas", person_id: 0, status: 2}
test "get_list_item_position/2 retrieves the position of an item in a list" do
{:ok, %{model: item, version: _version}} =
Item.create_item(@valid_attrs)
{:ok, li1} = App.ListItem.add_item_to_all_list(item)
assert li1.position == 1.0

# Note: this conversion from Int to Binary is to simulate the
# binary that the LiveView sends to this function ...
item_id = Integer.to_string(item.id)
pos = App.ListItem.get_list_item_position(item_id, li1.list_id)

assert li1.position == pos
end

test "App.ListItem.next_position_on_list/1 retrieve next position" do
person_id = 7
App.List.create_list(%{text: "all", person_id: person_id})
all_list = App.List.get_list_by_text!(person_id, "all")
pos_before = App.ListItem.next_position_on_list(all_list.id)
assert pos_before == 1.0

item_ids = App.ListItem.get_items_on_all_list(person_id)
assert length(item_ids) == 0
App.ListItem.add_items_to_all_list(person_id)

# Add an new item for the person:
{:ok, %{model: item}} = %{text: "hai", person_id: person_id, status: 2}
|> Item.create_item()

App.ListItem.add_item_to_all_list(item)

updated_item_ids = App.ListItem.get_items_on_all_list(person_id)
# dbg(updated_item_ids)
assert length(updated_item_ids) ==
length(App.Item.all_items_for_person(person_id))

count_after = App.ListItem.next_position_on_list(all_list.id)
assert count_before + length(updated_item_ids) == count_after
pos_after = App.ListItem.next_position_on_list(all_list.id)
assert pos_before + length(updated_item_ids) == pos_after
end

@valid_attrs %{text: "Buy Bananas", person_id: 0, status: 2}

test "move_item/3 repositions an item in the list" do
{:ok, %{model: item1, version: _version}} = Item.create_item(@valid_attrs)
{:ok, %{model: item2, version: _version}} = Item.create_item(@valid_attrs)
Expand All @@ -91,25 +111,12 @@ defmodule App.ListItemsTest do

assert li1.position < li2.position

# Get the "All" list for this person:
list = App.List.get_list_by_text!(@valid_attrs.person_id, "All")
# Get the "all" list for this person:
list = App.List.get_list_by_text!(@valid_attrs.person_id, "all")

# Move item2 to be above item1:
{:ok, li3} = App.ListItem.move_item(item2.id, item1.id, list.id)
assert li3.position == 0.999999
end

test "get_list_item_position/2 retrieves the postion of an item in a list" do
{:ok, %{model: item, version: _version}} = Item.create_item(@valid_attrs)
{:ok, li1} = App.ListItem.add_item_to_all_list(item)
assert li1.position == 1.0

# Note: this conversion from Int to Binary is to simulate the
# binary that the LiveView sends to this function ...
item_id = Integer.to_string(item.id)
pos = App.ListItem.get_list_item_position(item_id, li1.list_id)

assert li1.position == pos
end
end
end
50 changes: 35 additions & 15 deletions test/app/list_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,6 @@ defmodule App.ListTest do
assert List.get_list!(list.id).text == list.text
end

# test "get_item/2 returns the item with given id with tags" do
# {:ok, %{model: item, version: _version}} = Item.create_item(@valid_attrs)

# tags = Map.get(Item.get_item(item.id, true), :tags)

# assert Item.get_item(item.id, true).text == item.text
# assert not is_nil(tags)
# end

test "create_list/1 with valid data creates a list" do
assert {:ok, %{model: list, version: _version}} =
List.create_list(@valid_attrs)
Expand All @@ -34,7 +25,7 @@ defmodule App.ListTest do
assert {:error, %Ecto.Changeset{}} = List.create_list(@invalid_attrs)
end

test "update_item/2 with valid data updates the item" do
test "update_list/2 with valid data updates the list" do
{:ok, %{model: list, version: _version}} = List.create_list(@valid_attrs)

assert {:ok, %{model: list, version: _version}} =
Expand All @@ -43,14 +34,43 @@ defmodule App.ListTest do
assert list.text == "some updated text"
end

test "create_default_lists/1 creates the default lists" do
assert List.create_default_lists(2) |> length() > 2
test "get_person_lists/1 returns the lists for the person_id" do
person_id = 3
lists_before = App.List.get_person_lists(person_id)
assert length(lists_before) == 0

# Create a couple of lists
{:ok, %{model: all_list}} =
%{text: "all", person_id: person_id, status: 2}
|> App.List.create_list()

{:ok, %{model: recipe_list}} =
%{text: "Recipes", person_id: person_id, status: 2}
|> App.List.create_list()

# Retrieve the lists for the person_id:
lists_after = App.List.get_person_lists(person_id)
assert length(lists_after) == 2
assert Enum.member?(lists_after, all_list)
assert Enum.member?(lists_after, recipe_list)
end

test "get_list_by_text!/2 returns the list for the person_id by text" do
assert App.List.create_default_lists(0) |> length() > 3
list = App.List.get_list_by_text!(0, "All")
assert list.text == "All"
person_id = 4
{:ok, %{model: all_list}} = %{text: "all", person_id: person_id, status: 2}
|> App.List.create_list()
list = App.List.get_list_by_text!(person_id, "all")
assert list.text == "all"
assert list.id == all_list.id
end

test "create_default_lists/1 creates the default lists" do
# Should have no lists:
person_id = 5
lists = App.List.get_person_lists(person_id)
assert length(lists) == 0
# Create the default lists for this person_id:
assert List.create_default_lists(person_id) |> length() == 9
end
end
end
6 changes: 3 additions & 3 deletions test/app_web/live/app_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -828,15 +828,15 @@ defmodule AppWeb.AppLiveTest do
status: 2
})

# Get "All" list for this person_id
list = App.List.get_list_by_text!(person_id, "All")
# Get "all" list for this person_id
list = App.List.get_list_by_text!(person_id, "all")

# Render LiveView
{:ok, view, _html} = live(conn, "/")

# Highlight broadcast should have occurred
assert render_hook(view, "highlight", %{"id" => item.id})
|> String.split("bg-yellow-300")
|> String.split("bg-teal-300")
|> Enum.drop(1)
|> length() > 0

Expand Down

0 comments on commit 56d92d5

Please sign in to comment.