Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Creates the Person module and refactors a little #415

Merged
merged 2 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/app/person.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defmodule App.Person do
@doc """
Gets the Person Id from the assigns on connection or socket.
"""
def get_person_id(assigns), do: assigns[:person][:id] || 0
end
6 changes: 3 additions & 3 deletions lib/app_web/controllers/tag_controller.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule AppWeb.TagController do
use AppWeb, :controller
alias App.Tag
alias App.{Person, Tag}
plug :permission_tag when action in [:edit, :update, :delete]

def index(conn, _params) do
Expand All @@ -16,7 +16,7 @@ defmodule AppWeb.TagController do
end

def create(conn, %{"tag" => tag_params}) do
person_id = conn.assigns[:person][:id] || 0
person_id = Person.get_person_id(conn.assigns)
tag_params = Map.put(tag_params, "person_id", person_id)

case Tag.create_tag(tag_params) do
Expand Down Expand Up @@ -63,7 +63,7 @@ defmodule AppWeb.TagController do
defp permission_tag(conn, _opts) do
tag = Tag.get_tag!(conn.params["id"])

person_id = conn.assigns[:person][:id] || 0
person_id = Person.get_person_id(conn.assigns)

if tag.person_id == person_id do
conn
Expand Down
22 changes: 10 additions & 12 deletions lib/app_web/live/app_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@ defmodule AppWeb.AppLive do
require Logger
use AppWeb, :live_view
use Timex
alias App.{Item, Tag, Timer}
alias App.{Item, Person, Tag, Timer}
# run authentication on mount
on_mount(AppWeb.AuthController)
alias Phoenix.Socket.Broadcast

@topic "live"
@stats_topic "stats"

defp get_person_id(assigns), do: assigns[:person][:id] || 0

@impl true
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 = get_person_id(socket.assigns)
person_id = Person.get_person_id(socket.assigns)
items = Item.items_with_timers(person_id)
tags = Tag.list_person_tags(person_id)
selected_tags = []
Expand All @@ -43,7 +41,7 @@ defmodule AppWeb.AppLive do

@impl true
def handle_event("validate", %{"text" => text}, socket) do
person_id = get_person_id(socket.assigns)
person_id = Person.get_person_id(socket.assigns)
draft = Item.get_draft_item(person_id)
Item.update_draft(draft, %{text: text})
# only save draft if person id != 0 (ie not guest)
Expand All @@ -52,7 +50,7 @@ defmodule AppWeb.AppLive do

@impl true
def handle_event("create", %{"text" => text}, socket) do
person_id = get_person_id(socket.assigns)
person_id = Person.get_person_id(socket.assigns)

Item.create_item_with_tags(%{
text: text,
Expand All @@ -77,7 +75,7 @@ defmodule AppWeb.AppLive do

@impl true
def handle_event("toggle", data, socket) do
person_id = get_person_id(socket.assigns)
person_id = Person.get_person_id(socket.assigns)

# Toggle the status of the item between 3 (:active) and 4 (:done)
status = if Map.has_key?(data, "value"), do: 4, else: 3
Expand All @@ -93,7 +91,7 @@ defmodule AppWeb.AppLive do

@impl true
def handle_event("toggle_tag", value, socket) do
person_id = get_person_id(socket.assigns)
person_id = Person.get_person_id(socket.assigns)
selected_tags = socket.assigns.selected_tags
tag = Tag.get_tag!(value["tag_id"])
tags = Tag.list_person_tags(person_id)
Expand All @@ -105,7 +103,7 @@ defmodule AppWeb.AppLive do

@impl true
def handle_event("filter-tags", %{"key" => _key, "value" => value}, socket) do
person_id = get_person_id(socket.assigns)
person_id = Person.get_person_id(socket.assigns)

tags =
Tag.list_person_tags(person_id)
Expand Down Expand Up @@ -154,7 +152,7 @@ defmodule AppWeb.AppLive do
@impl true
def handle_event("start", data, socket) do
item = Item.get_item!(Map.get(data, "id"))
person_id = get_person_id(socket.assigns)
person_id = Person.get_person_id(socket.assigns)

{:ok, _timer} =
Timer.start(%{
Expand Down Expand Up @@ -199,7 +197,7 @@ defmodule AppWeb.AppLive do
%{"id" => item_id, "text" => text},
socket
) do
person_id = get_person_id(socket.assigns)
person_id = Person.get_person_id(socket.assigns)
current_item = Item.get_item!(item_id)

Item.update_item(current_item, %{
Expand Down Expand Up @@ -252,7 +250,7 @@ defmodule AppWeb.AppLive do

@impl true
def handle_info(%Broadcast{event: "update", payload: payload}, socket) do
person_id = get_person_id(socket.assigns)
person_id = Person.get_person_id(socket.assigns)
items = Item.items_with_timers(person_id)
isEditingItem = socket.assigns.editing

Expand Down
6 changes: 2 additions & 4 deletions lib/app_web/live/stats_live.ex
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
defmodule AppWeb.StatsLive do
require Logger
use AppWeb, :live_view
alias App.{Stats, DateTimeHelper}
alias App.{Stats, DateTimeHelper, Person}
alias Phoenix.Socket.Broadcast

# run authentication on mount
on_mount(AppWeb.AuthController)

@stats_topic "stats"

defp get_person_id(assigns), do: assigns[:person][:id] || 0

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

person_id = get_person_id(socket.assigns)
person_id = Person.get_person_id(socket.assigns)
metrics = Stats.person_with_item_and_timer_count()

{:ok,
Expand Down
29 changes: 29 additions & 0 deletions test/app/person_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule App.PersonTest do
use ExUnit.Case
alias App.Person

@doc """
Test for `get_person_id/1`
"""
describe "get_person_id/1" do
test "returns id when person is present in assigns" do
assigns = %{person: %{id: 1}}
assert Person.get_person_id(assigns) == 1
end

test "returns 0 when person is not present in assigns" do
assigns = %{}
assert Person.get_person_id(assigns) == 0
end

test "returns 0 when id is not present in person" do
assigns = %{person: %{}}
assert Person.get_person_id(assigns) == 0
end

test "returns 0 when id is nil in person" do
assigns = %{person: %{id: nil}}
assert Person.get_person_id(assigns) == 0
end
end
end