Skip to content

Commit

Permalink
Merge pull request #38 from actnforchildren/previous-emotions
Browse files Browse the repository at this point in the history
Previous emotions
  • Loading branch information
Danwhy authored Oct 18, 2018
2 parents 349c8e8 + 097bae3 commit 3a1ed50
Show file tree
Hide file tree
Showing 17 changed files with 160 additions and 41 deletions.
4 changes: 4 additions & 0 deletions assets/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@
.mt-day-week-month {
margin-top: 3rem;
}

.afc-b--red {
border-color: #DF001C;
}
2 changes: 1 addition & 1 deletion lib/afc/angry.ex → lib/afc/emotion/angry.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Afc.Angry do
defmodule Afc.Emotion.Angry do
use Ecto.Schema
import Ecto.Changeset

Expand Down
53 changes: 53 additions & 0 deletions lib/afc/emotion/emotion.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
defmodule Afc.Emotion do
alias Afc.Repo
alias Afc.Emotion.{Angry, EmotionLog, Happy}
import Ecto.Query
use Timex

def todays_emotion_log(user) do
start_of_today = Timex.today() |> Timex.to_naive_datetime()

query =
from e in EmotionLog,
where: e.user_id == ^user.id
and e.inserted_at > ^start_of_today

Repo.one(query)
end

def get_emotion_module_name(emotion) do
emotion_str =
case is_atom(emotion) do
true ->
Atom.to_string(emotion)
false ->
emotion
end
|> String.capitalize()

"Elixir.Afc.Emotion." <> emotion_str
|> String.to_atom()
end

def get_emotion_map(emotion_str) do
emotion_atom = String.to_atom(emotion_str)
emotions_map() |> Map.get(emotion_atom)
end

def emotion_list do
emotions_map()
|> Map.keys()
end

defp emotions_map do
%{
happy: Map.new([module: Happy, emoji: "😆"]),
angry: Map.new([module: Angry, emoji: "😡"]),
excited: Map.new([module: Angry, emoji: "🤩"]),
sad: Map.new([module: Angry, emoji: "😭"]),
worried: Map.new([module: Angry, emoji: "😬"]),
dont_know: Map.new([module: Angry, emoji: "😐"]),
else: Map.new([module: Angry, emoji: "😶"])
}
end
end
2 changes: 1 addition & 1 deletion lib/afc/emotion_log.ex → lib/afc/emotion/emotion_log.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Afc.EmotionLog do
defmodule Afc.Emotion.EmotionLog do
use Ecto.Schema
import Ecto.Changeset
alias Afc.{EctoEnum.EmotionEnum, User}
Expand Down
6 changes: 3 additions & 3 deletions lib/afc/happy.ex → lib/afc/emotion/happy.ex
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
defmodule Afc.Happy do
defmodule Afc.Emotion.Happy do
use Ecto.Schema
import Ecto.Changeset


schema "happy" do
field :reason_text, :string
field :reason, :string

timestamps()
end

@doc false
def changeset(happy, attrs) do
happy
|> cast(attrs, [:reason_text])
|> cast(attrs, [:reason])
end
end
44 changes: 21 additions & 23 deletions lib/afc_web/controllers/emotion_controller.ex
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
defmodule AfcWeb.EmotionController do
use AfcWeb, :controller
alias Afc.{Angry, EmotionLog, Happy, Repo}
alias Afc.Emotion
alias Afc.Emotion.{EmotionLog}
alias Afc.Repo
alias Ecto.Changeset

def show(conn, %{"id" => "captured"}) do
render conn, "captured.html"
end

def show(conn, %{"id" => emotion}) do
{module, changeset} = get_page_module_and_changeset(emotion)

render conn, "form.html", changeset: changeset, module: module
def show(conn, %{"id" => emotion_str}) do
emotion_map = Emotion.get_emotion_map(emotion_str)
module_struct = emotion_str |> Emotion.get_emotion_module_name() |> struct()
changeset = emotion_map.module.changeset(module_struct, %{})
render conn, "form.html", changeset: changeset, module: emotion_map.module
end

def create(conn, params) do
submitted_emotion =
~w(angry happy)
|> Enum.filter(&(Map.has_key?(params, &1)))
|> hd()

form_info = Map.get(params, submitted_emotion)

{module, changeset} =
get_page_module_and_changeset(submitted_emotion, form_info)
emotion_str = get_submitted_emotion(params)
form_info = Map.get(params, emotion_str)
emotion_map = Emotion.get_emotion_map(emotion_str)
module_struct = emotion_str |> Emotion.get_emotion_module_name() |> struct()
changeset = emotion_map.module.changeset(module_struct, form_info)

case Repo.insert(changeset) do
{:ok, captured_emotion} ->
emotion_params =
%{emotion: submitted_emotion, emotion_id: captured_emotion.id}
%{emotion: emotion_str, emotion_id: captured_emotion.id}

%EmotionLog{}
|> EmotionLog.changeset(emotion_params)
Expand All @@ -37,16 +36,15 @@ defmodule AfcWeb.EmotionController do
redirect(conn, to: emotion_path(conn, :show, "captured"))

{:error, changeset} ->
render conn, "form.html", changeset: changeset, module: module
assigns = [changeset: changeset, module: emotion_map.module]
render conn, "form.html", assigns
end
end

defp get_page_module_and_changeset(page, params \\ %{}) do
case page do
"happy" ->
{Happy, Happy.changeset(%Happy{}, params)}
"angry" ->
{Angry, Angry.changeset(%Angry{}, params)}
end
defp get_submitted_emotion(params) do
Emotion.emotion_list
|> Enum.map(&Atom.to_string/1)
|> Enum.filter(&(Map.has_key?(params, &1)))
|> hd()
end
end
11 changes: 10 additions & 1 deletion lib/afc_web/controllers/page_controller.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
defmodule AfcWeb.PageController do
use AfcWeb, :controller
alias Afc.{Emotion, Repo}

def index(conn, _params) do
render conn, "index.html"
case Emotion.todays_emotion_log(conn.assigns.current_user) do
nil ->
render conn, "index.html"
emotion_log ->
module_name = Emotion.get_emotion_module_name(emotion_log.emotion)
emotion = Repo.get(module_name, emotion_log.emotion_id)

render conn, "single.html", emotion_log: emotion_log, emotion: emotion
end
end
end
2 changes: 1 addition & 1 deletion lib/afc_web/templates/component/emoji_helper.html.eex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="tc w4">
<%= link to: emotion_path(@conn, :show, @page), class: "pointer link black" do %>
<%= emoji_p_tag(@emoji) %>
<%= emoji_p_tag(@emotion) %>
<p class="-mt2"><%= @emotion %></p>
<% end %>
</div>
6 changes: 6 additions & 0 deletions lib/afc_web/templates/component/trusted_share_btn.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="tc mt5 mb3 f4">
<button type="button" class="b ba afc-b--red ph3 pv2 br2 bg-white">Share with Trusted Adult</button>
<!-- <%= form_for @conn, "/", fn f -> %>
<%= submit("Share with Trusted Adult", class: "b ba afc-b--red ph3 pv2 br2 bg-white") %>
<% end %> -->
</div>
14 changes: 7 additions & 7 deletions lib/afc_web/templates/page/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
<div class="center w-90">

<div class="flex justify-between pt3 ph1 mb3">
<%= component "emoji_helper", [emoji: "😆", emotion: "Happy", page: "happy", conn: @conn] %>
<%= component "emoji_helper", [emoji: "🤩", emotion: "Excited", page: "happy", conn: @conn] %>
<%= component "emoji_helper", [emotion: "Happy", page: "happy", conn: @conn] %>
<%= component "emoji_helper", [emotion: "Excited", page: "happy", conn: @conn] %>
</div>

<div class="flex justify-between pt3 ph1 mb3">
<%= component "emoji_helper", [emoji: "😡", emotion: "Angry", page: "angry", conn: @conn] %>
<%= component "emoji_helper", [emoji: "😭", emotion: "Sad", page: "happy", conn: @conn] %>
<%= component "emoji_helper", [emotion: "Angry", page: "angry", conn: @conn] %>
<%= component "emoji_helper", [emotion: "Sad", page: "happy", conn: @conn] %>
</div>

<div class="flex justify-between pt3 ph1 mb3">
<%= component "emoji_helper", [emoji: "😬", emotion: "Worried", page: "happy", conn: @conn] %>
<%= component "emoji_helper", [emoji: "😐", emotion: "I don't know", page: "happy", conn: @conn] %>
<%= component "emoji_helper", [emotion: "Worried", page: "happy", conn: @conn] %>
<%= component "emoji_helper", [emotion: "I don't know", page: "happy", conn: @conn] %>
</div>

<div class="flex justify-between pt3 ph1 mb3">
<%= component "emoji_helper", [emoji: "😶", emotion: "Something else", page: "happy", conn: @conn] %>
<%= component "emoji_helper", [emotion: "Something else", page: "happy", conn: @conn] %>
</div>

</div>
Expand Down
22 changes: 22 additions & 0 deletions lib/afc_web/templates/page/single.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<%= component "day_week_month_bar", [] %>

<div class="w-90 bg-white center br2 pa3 mb5 mt-day-week-month">
<p class="pa3 ba br2 dib mb4">10th Oct 2019 (placeholder)</p>
<p class="tc f3 mb3">Today's Log</p>
<div class="center w-100 tc f4">
<%= emoji_p_tag(@emotion_log.emotion) %>

<div class="center tl flex mv3">
<p class="w-40 pl3 pl6-l">I felt:</p>
<p class="w-60"><%= format_emotion(@emotion_log.emotion) %></p>
</div>

<div class="center tl flex">
<p class="w-40 pl3 pl6-l">Because:</p>
<p class="w-60"><%= @emotion.reason %></p>
</div>

</div>

<%= component "trusted_share_btn", [conn: @conn] %>
</div>
19 changes: 18 additions & 1 deletion lib/afc_web/views/component_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,24 @@ defmodule AfcWeb.ComponentHelpers do
ComponentView.render "#{template}.html", assigns
end

def emoji_p_tag(emoji) do
def emoji_p_tag(emotion) do
emoji = string_to_emoji(emotion)
content_tag(:p, emoji, [class: "emoji-font-size"])
end

defp string_to_emoji(emotion) do
emotion = if is_atom(emotion), do: Atom.to_string(emotion), else: emotion

emotion
|> String.downcase()
|> case do
"happy" -> "😆"
"excited" -> "🤩"
"angry" -> "😡"
"sad" -> "😭"
"worried" -> "😬"
"i don't know" -> "😐"
"something else" -> "😶"
end
end
end
6 changes: 6 additions & 0 deletions lib/afc_web/views/page_view.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
defmodule AfcWeb.PageView do
use AfcWeb, :view

def format_emotion(emotion) do
emotion
|> Atom.to_string()
|> String.capitalize()
end
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ defmodule Afc.Mixfile do
{:excoveralls, "~> 0.10", only: :test},
{:credo, "~> 0.10.0", only: [:dev, :test], runtime: false},
{:autoform, git: "https:/dwyl/autoform.git", tag: "0.1"},
{:ecto_enum, "~> 1.0"}
{:ecto_enum, "~> 1.0"},
{:timex, "~> 3.1"}
]
end

Expand Down
3 changes: 3 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"autoform": {:git, "https:/dwyl/autoform.git", "eaa1d76e858d1ed5b042270c821e14932ba470e3", [tag: "0.1"]},
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"},
"certifi": {:hex, :certifi, "2.4.2", "75424ff0f3baaccfd34b1214184b6ef616d89e420b258bb0a5ea7d7bc628f7f0", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"},
"combine": {:hex, :combine, "0.10.0", "eff8224eeb56498a2af13011d142c5e7997a80c8f5b97c499f84c841032e429f", [:mix], [], "hexpm"},
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm"},
"cowboy": {:hex, :cowboy, "1.1.2", "61ac29ea970389a88eca5a65601460162d370a70018afe6f949a29dca91f3bb0", [:rebar3], [{:cowlib, "~> 1.0.2", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.3.2", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm"},
"cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [:make], [], "hexpm"},
Expand Down Expand Up @@ -32,5 +33,7 @@
"pre_commit": {:hex, :pre_commit, "0.3.4", "e2850f80be8090d50ad8019ef2426039307ff5dfbe70c736ad0d4d401facf304", [:mix], [], "hexpm"},
"ranch": {:hex, :ranch, "1.3.2", "e4965a144dc9fbe70e5c077c65e73c57165416a901bd02ea899cfd95aa890986", [:rebar3], [], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.4", "f0eafff810d2041e93f915ef59899c923f4568f4585904d010387ed74988e77b", [:make, :mix, :rebar3], [], "hexpm"},
"timex": {:hex, :timex, "3.4.1", "e63fc1a37453035e534c3febfe9b6b9e18583ec7b37fd9c390efdef97397d70b", [:mix], [{:combine, "~> 0.10", [hex: :combine, repo: "hexpm", optional: false]}, {:gettext, "~> 0.10", [hex: :gettext, repo: "hexpm", optional: false]}, {:tzdata, "~> 0.1.8 or ~> 0.5", [hex: :tzdata, repo: "hexpm", optional: false]}], "hexpm"},
"tzdata": {:hex, :tzdata, "0.5.19", "7962a3997bf06303b7d1772988ede22260f3dae1bf897408ebdac2b4435f4e6a", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm"},
}
2 changes: 1 addition & 1 deletion priv/repo/migrations/20181014104921_create_happy.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Afc.Repo.Migrations.CreateHappy do

def change do
create table(:happy) do
add :reason_text, :string
add :reason, :text

timestamps()
end
Expand Down
2 changes: 1 addition & 1 deletion priv/repo/migrations/20181014105259_create_angry.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule Afc.Repo.Migrations.CreateAngry do
add :classwork, :boolean, default: false, null: false
add :homework, :boolean, default: false, null: false
add :else, :boolean, default: false, null: false
add :reason, :string
add :reason, :text

timestamps()
end
Expand Down

0 comments on commit 3a1ed50

Please sign in to comment.