Skip to content

Commit

Permalink
Implement suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioRodrigues10 committed Jul 21, 2023
1 parent 735069f commit eec29b2
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 159 deletions.
29 changes: 29 additions & 0 deletions lib/atomic/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,35 @@ defmodule Atomic.Accounts do
end
end

@doc """
Return the first and last name of a name.
## Examples
iex> extract_first_last_name("John Doe")
"John Doe"
iex> extract_first_last_name("John")
"John"
iex> extract_first_last_name(nil)
""
"""
def extract_first_last_name(name) do
names =
name
|> String.split(" ")
|> Enum.filter(&String.match?(String.slice(&1, 0, 1), ~r/^\p{L}$/u))
|> Enum.map(&String.capitalize/1)

case length(names) do
0 -> ""
1 -> hd(names)
_ -> List.first(names) <> " " <> List.last(names)
end
end

@doc """
Returns an `%Ecto.Changeset{}` for tracking user changes.
Expand Down
2 changes: 1 addition & 1 deletion lib/atomic_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ defmodule AtomicWeb do

import AtomicWeb.ErrorHelpers
import AtomicWeb.Gettext
import AtomicWeb.UtilsView
import AtomicWeb.ViewUtils
alias AtomicWeb.Router.Helpers, as: Routes

alias Icons.{Heroicons, Ionicons}
Expand Down
1 change: 1 addition & 0 deletions lib/atomic_web/live/activity_live/index.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule AtomicWeb.ActivityLive.Index do
use AtomicWeb, :live_view

alias Atomic.Accounts
alias Atomic.Activities
alias Atomic.Activities.Activity

Expand Down
6 changes: 3 additions & 3 deletions lib/atomic_web/live/activity_live/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<p class="mt-2 flex items-center text-sm text-zinc-500 lg:mt-0">
<Heroicons.Solid.calendar class="flex-shrink-0 mr-1.5 w-5 h-5 text-zinc-400" />
<%= if hd(activity.activity_sessions) do %>
<%= display_date(hd(activity.activity_sessions).start) %>
<%= AtomicWeb.ViewUtils.display_date(hd(activity.activity_sessions).start) %>
<% end %>
</p>
<%= if not is_nil(hd(activity.activity_sessions).location) do %>
Expand All @@ -71,11 +71,11 @@
<div class="mt-2 flex items-center">
<span class="inline-flex justify-center items-center mr-1.5 w-6 h-6 bg-zinc-500 rounded-full">
<span class="text-xs font-medium leading-none text-white">
<%= extract_initials(speaker.name) %>
<%= Accounts.extract_initials(speaker.name) %>
</span>
</span>
<p class="text-sm text-zinc-500">
<%= extract_first_last_name(speaker.name) %>
<%= Accounts.extract_first_last_name(speaker.name) %>
</p>
</div>
<% end %>
Expand Down
1 change: 1 addition & 0 deletions lib/atomic_web/live/activity_live/show.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule AtomicWeb.ActivityLive.Show do
use AtomicWeb, :live_view

alias Atomic.Accounts
alias Atomic.Activities
alias Atomic.Organizations

Expand Down
6 changes: 3 additions & 3 deletions lib/atomic_web/live/activity_live/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
<div class="text-zinc-900x inset-y-0 my-auto flex w-full gap-y-4 text-sm">
<Heroicons.Solid.calendar class="h-5 w-5 text-zinc-400" />
<p class="block pl-1.5 max-w-6xl">
<%= display_date(session.start) %>
<%= AtomicWeb.ViewUtils.display_date(session.start) %>
</p>
</div>
</div>
Expand Down Expand Up @@ -125,11 +125,11 @@
<div class="mt-1 flex items-center">
<span class="mr-2 inline-flex h-9 w-9 items-center justify-center rounded-full bg-zinc-500">
<span class="text-md font-medium leading-none text-white">
<%= extract_initials(speaker.name) %>
<%= Accounts.extract_initials(speaker.name) %>
</span>
</span>
<%= live_redirect to: Routes.speaker_show_path(@socket, :show, speaker), class: "text-md text-blue-500" do %>
<%= extract_first_last_name(speaker.name) %>
<%= Accounts.extract_first_last_name(speaker.name) %>
<% end %>
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions lib/atomic_web/templates/layout/live.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<span class="flex min-w-0 items-center justify-between space-x-3">
<span class="inline-flex justify-center items-center w-10 h-10 bg-zinc-300 rounded-lg">
<span class="text-lg font-medium leading-none text-white">
<%= extract_initials("CeSIUM") %>
<%= Atomic.Accounts.extract_initials("CeSIUM") %>
</span>
</span>
<span class="flex min-w-0 flex-1 flex-col">
Expand Down Expand Up @@ -60,7 +60,7 @@
<span class="flex min-w-0 items-center justify-between space-x-3">
<span class="inline-flex justify-center items-center mr-1.5 w-10 h-10 bg-zinc-500 rounded-lg">
<span class="text-lg font-medium leading-none text-white">
<%= extract_initials("NECC") %>
<%= Atomic.Accounts.extract_initials("NECC") %>
</span>
</span>
<span class="flex min-w-0 flex-1 flex-col">
Expand Down Expand Up @@ -178,7 +178,7 @@
<% end %>
<%= live_redirect to: Routes.user_settings_path(@socket, :edit), class: "bg-zinc-200 flex items-center gap-x-4 px-6 py-3 text-sm font-semibold leading-6 text-gray-900" do %>
<span class="bg-zinc-400 rounded-lg w-10 h-10 flex items-center justify-center text-lg font-medium leading-none text-white">
<%= extract_initials(@current_user.email) %>
<%= Atomic.Accounts.extract_initials(@current_user.email) %>
</span>
<span aria-hidden="true"><%= @current_user.email %></span>
<% end %>
Expand Down
153 changes: 116 additions & 37 deletions lib/atomic_web/views/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,145 @@ defmodule AtomicWeb.ViewUtils do
"""
use Phoenix.HTML

import AtomicWeb.Gettext

alias Timex.Format.DateTime.Formatters.Relative

require Timex.Translator

def frontend_url do
Application.fetch_env!(:atomic, AtomicWeb.Endpoint)[:frontend_url]
end

@doc """
Display a user's name
@doc ~S"""
Returns a relative datetime string for the given datetime.
## Examples
iex> display_name(%{first_name: "John", last_name: "Doe"})
"John Doe"
iex> relative_datetime(~N[2020-01-01 00:00:00])
"3 years ago"
iex> relative_datetime(~N[2023-01-01 00:00:00] |> Timex.shift(days: 1))
"6 months ago"
"""
def display_name(user) do
"#{user.first_name} #{user.last_name}"
def relative_datetime(nil), do: ""

def relative_datetime(""), do: ""

def relative_datetime(datetime) do
Relative.lformat!(datetime, "{relative}", Gettext.get_locale())
end

@doc """
Display a date in format "HH:MM"
@doc ~S"""
Returns a relative date string for the given date.
## Examples
iex> display_time(~U[2018-01-01 00:00:00Z])
"00:00"
iex> display_time(~U[2018-01-01 12:00:00Z])
"12:00"
iex> display_time(~U[2018-01-01 23:59:00Z])
"23:59"
iex> display_date(~D[2020-01-01])
"01-01-2020"
iex> display_date(~D[2023-01-01])
"01-01-2023"
"""
def display_time(%DateTime{} = datetime) do
hour = two_characters(datetime.hour)
minute = two_characters(datetime.minute)
def display_date(nil), do: ""

def display_date(""), do: ""

def display_date(date) when is_binary(date) do
date
|> Timex.parse!("%FT%H:%M", :strftime)
|> Timex.format!("{0D}-{0M}-{YYYY}")
end

"#{hour}:#{minute}"
def display_date(date) do
Timex.format!(date, "{0D}-{0M}-{YYYY}")
end

@doc """
Display a date in a given locale
@doc ~S"""
Returns a relative time string for the given time.
## Examples
iex> display_date(~N[2021-03-10 02:27:07], "pt")
"Quarta-feira, 10 de Março de 2021"
iex> display_date(~N[2023-02-25 22:25:46], "en")
"Saturday, February 25, 2023"
iex> display_time(~T[00:00:00])
"00:00"
iex> display_time(~T[23:59:59])
"23:59"
"""
def display_date(datetime, locale \\ "pt")
def display_time(nil), do: ""

def display_date(datetime, "pt" = locale) do
Timex.Translator.with_locale locale do
Timex.format!(datetime, "{WDfull}, {D} de {Mfull} de {YYYY}")
end
def display_time(""), do: ""

def display_time(date) when is_binary(date) do
date
|> Timex.parse!("%FT%H:%M", :strftime)
|> Timex.format!("{0D}-{0M}-{YYYY}")
end

def display_date(datetime, "en" = locale) do
Timex.Translator.with_locale locale do
Timex.format!(datetime, "{WDfull}, {Mfull} {D}, {YYYY}")
end
def display_time(date) do
date
|> Timex.format!("{h24}:{m}")
end

defp two_characters(number) do
if number < 10 do
"0#{number}"
@doc ~S"""
Returns a list of first element from tuples where the second element is true
## Examples
iex> class_list([{"col-start-1", true}, {"col-start-2", false}, {"col-start-3", true}])
"col-start-1 col-start-3"
iex> class_list([{"Math", true}, {"Physics", false}, {"Chemistry", false}])
"Math"
"""
def class_list(items) do
items
|> Enum.reject(&(elem(&1, 1) == false))
|> Enum.map_join(" ", &elem(&1, 0))
end

@doc ~S"""
Returns the class name for a given column
## Examples
iex> col_start(1)
"col-start-1"
iex> col_start(2)
"col-start-2"
iex> col_start(0)
"col-start-0"
iex> col_start(8)
"col-start-0"
"""
def col_start(col) do
if col in 1..7 do
"col-start-#{col}"
else
number
"col-start-0"
end
end

@doc ~S"""
Returns an error message for a given error
## Examples
iex> error_to_string(:too_large)
"Too large"
iex> error_to_string(:not_accepted)
"You have selected an unacceptable file type"
iex> error_to_string(:too_many_files)
"You have selected too many files"
"""
def error_to_string(:too_large), do: gettext("Too large")
def error_to_string(:not_accepted), do: gettext("You have selected an unacceptable file type")
def error_to_string(:too_many_files), do: gettext("You have selected too many files")
end
Loading

0 comments on commit eec29b2

Please sign in to comment.