From fb2a3f88ffc123e2bae18090e705e41a7f3683c9 Mon Sep 17 00:00:00 2001 From: Afonso Martins Date: Thu, 3 Oct 2024 23:38:53 +0100 Subject: [PATCH 01/10] feat: validate and add socials.ex --- lib/atomic/uploader.ex | 17 ++++++++++++++--- lib/atomic/uploaders/profile_picture.ex | 2 +- lib/atomic_web/components/socials.ex | 0 3 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 lib/atomic_web/components/socials.ex diff --git a/lib/atomic/uploader.ex b/lib/atomic/uploader.ex index 8bb59c9fe..69274bba0 100644 --- a/lib/atomic/uploader.ex +++ b/lib/atomic/uploader.ex @@ -9,18 +9,29 @@ defmodule Atomic.Uploader do use Waffle.Definition use Waffle.Ecto.Definition - def validate({file, _}) do + def validate(file, _) do file_extension = file.file_name |> Path.extname() |> String.downcase() case Enum.member?(extension_whitelist(), file_extension) do - true -> :ok - false -> {:error, "invalid file extension"} + true -> + if file.size <= max_size() do + :ok + else + {:error, "file size exceeds maximum allowed size"} + end + + false -> + {:error, "invalid file extension"} end end def extension_whitelist do Keyword.get(unquote(opts), :extensions, []) end + + def max_size do + Keyword.get(unquote(opts), :max_size, 500 ) + end end end end diff --git a/lib/atomic/uploaders/profile_picture.ex b/lib/atomic/uploaders/profile_picture.ex index 06a93519b..7a2cbdf85 100644 --- a/lib/atomic/uploaders/profile_picture.ex +++ b/lib/atomic/uploaders/profile_picture.ex @@ -2,7 +2,7 @@ defmodule Atomic.Uploaders.ProfilePicture do @moduledoc """ Uploader for profile pictures. """ - use Atomic.Uploader, extensions: ~w(.jpg .jpeg .png .gif) + use Atomic.Uploader, extensions: ~w(.jpg .jpeg .png .gif), max_size: 500 alias Atomic.Accounts.User @versions [:original] diff --git a/lib/atomic_web/components/socials.ex b/lib/atomic_web/components/socials.ex new file mode 100644 index 000000000..e69de29bb From 6aa9b720a1ad4d13fe038d9847f50fe5029ea62e Mon Sep 17 00:00:00 2001 From: Afonso Martins Date: Fri, 4 Oct 2024 01:08:35 +0100 Subject: [PATCH 02/10] feat: socials component working --- lib/atomic/uploader.ex | 2 +- lib/atomic_web/components/sidebar.ex | 2 +- lib/atomic_web/components/socials.ex | 31 ++++++++++++++++ lib/atomic_web/live/partner_live/show.ex | 1 + .../live/partner_live/show.html.heex | 36 +++++++------------ 5 files changed, 47 insertions(+), 25 deletions(-) diff --git a/lib/atomic/uploader.ex b/lib/atomic/uploader.ex index 69274bba0..b029f4429 100644 --- a/lib/atomic/uploader.ex +++ b/lib/atomic/uploader.ex @@ -30,7 +30,7 @@ defmodule Atomic.Uploader do end def max_size do - Keyword.get(unquote(opts), :max_size, 500 ) + Keyword.get(unquote(opts), :max_size, 500) end end end diff --git a/lib/atomic_web/components/sidebar.ex b/lib/atomic_web/components/sidebar.ex index 4062d54b5..1dc89f527 100644 --- a/lib/atomic_web/components/sidebar.ex +++ b/lib/atomic_web/components/sidebar.ex @@ -190,7 +190,7 @@ defmodule AtomicWeb.Components.Sidebar do defp user_image(user) do if user.profile_picture do - Uploaders.ProfilePicture.url({user, user.profile_picture}, :original) + Uploaders.ProfilePicture.url({user.profile_picture, user}, :original) else nil end diff --git a/lib/atomic_web/components/socials.ex b/lib/atomic_web/components/socials.ex index e69de29bb..d1f0779ad 100644 --- a/lib/atomic_web/components/socials.ex +++ b/lib/atomic_web/components/socials.ex @@ -0,0 +1,31 @@ +defmodule AtomicWeb.Components.Socials do + @moduledoc false + use Phoenix.Component + + def socials(assigns) do + social_links = [ + {:tiktok, "tiktok.svg", "https://tiktok.com/"}, + {:instagram, "instagram.svg", "https://instagram.com/"}, + {:facebook, "facebook.svg", "https://facebook.com/"}, + {:x, "x.svg", "https://x.com/"} + ] + + ~H""" + <%= if @entity.socials do %> +
+ <%= for {social, icon, url_base} <- social_links do %> + <% social_value = Map.get(@entity.socials, social) %> + <%= if social_value do %> +
+ icon} class="h-5 w-5" alt={social |> Atom.to_string() |> String.capitalize()} /> + <.link class="text-blue-500" target="_blank" href={url_base <> social_value}> + <%= social |> Atom.to_string() |> String.capitalize() %> + +
+ <% end %> + <% end %> +
+ <% end %> + """ + end +end diff --git a/lib/atomic_web/live/partner_live/show.ex b/lib/atomic_web/live/partner_live/show.ex index c2718ddd3..375cc426a 100644 --- a/lib/atomic_web/live/partner_live/show.ex +++ b/lib/atomic_web/live/partner_live/show.ex @@ -2,6 +2,7 @@ defmodule AtomicWeb.PartnerLive.Show do use AtomicWeb, :live_view import AtomicWeb.Components.Avatar + import AtomicWeb.Components.Socials alias Atomic.Accounts alias Atomic.Organizations diff --git a/lib/atomic_web/live/partner_live/show.html.heex b/lib/atomic_web/live/partner_live/show.html.heex index 0756af2d2..ce1795bad 100644 --- a/lib/atomic_web/live/partner_live/show.html.heex +++ b/lib/atomic_web/live/partner_live/show.html.heex @@ -21,37 +21,27 @@ <%= if @partner.location do %> +
- <.icon name="hero-map-pin" class="size-5 text-zinc-400" /> - <.link class="text-blue-500" href={"https://www.google.com/maps/search/?api=1&query=#{@partner.location.name}"}><%= @partner.location.name %> + <.icon name="hero-map-pin" class="h-5 w-5 text-zinc-400" /> + <.link class="text-blue-500" href={"https://www.google.com/maps/search/?api=1&query=#{@partner.location.name}"}> + <%= @partner.location.name %> +
<% end %> + <%= if @partner.socials do %> -
+ +
+ <%= if @partner.socials.website do %> -
- <.icon name="hero-globe-alt" class="size-5 text-zinc-400" /> +
+ <.icon name="hero-globe-alt" class="h-5 w-5 text-zinc-400" /> <.link class="text-blue-500" href={@partner.socials.website}>Website
<% end %> - <%= if @partner.socials.instagram do %> -
- Instagram - <.link class="text-blue-500" href={"https://instagram.com/" <> @partner.socials.instagram}>Instagram -
- <% end %> - <%= if @partner.socials.facebook do %> -
- Facebook - <.link class="text-blue-500" href={"https://facebook.com/" <> @partner.socials.facebook}>Facebook -
- <% end %> - <%= if @partner.socials.x do %> -
- X - <.link class="text-blue-500" href={"https://x.com/" <> @partner.socials.x}>X -
- <% end %> + + <.socials entity={@partner} />
<% end %>
From d973f50dfbd75dc3527a8c6b8134f17f9144e682 Mon Sep 17 00:00:00 2001 From: Afonso Martins Date: Fri, 4 Oct 2024 01:22:20 +0100 Subject: [PATCH 03/10] fix: pass tests --- lib/atomic_web/components/socials.ex | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/atomic_web/components/socials.ex b/lib/atomic_web/components/socials.ex index d1f0779ad..f026a074e 100644 --- a/lib/atomic_web/components/socials.ex +++ b/lib/atomic_web/components/socials.ex @@ -3,17 +3,15 @@ defmodule AtomicWeb.Components.Socials do use Phoenix.Component def socials(assigns) do - social_links = [ - {:tiktok, "tiktok.svg", "https://tiktok.com/"}, - {:instagram, "instagram.svg", "https://instagram.com/"}, - {:facebook, "facebook.svg", "https://facebook.com/"}, - {:x, "x.svg", "https://x.com/"} - ] - ~H""" <%= if @entity.socials do %>
- <%= for {social, icon, url_base} <- social_links do %> + <%= for {social, icon, url_base} <- [ + {:tiktok, "tiktok.svg", "https://tiktok.com/"}, + {:instagram, "instagram.svg", "https://instagram.com/"}, + {:facebook, "facebook.svg", "https://facebook.com/"}, + {:x, "x.svg", "https://x.com/"} + ] do %> <% social_value = Map.get(@entity.socials, social) %> <%= if social_value do %>
From 254e5ec4465eadbc55bb1ad202540a3495b94f85 Mon Sep 17 00:00:00 2001 From: Afonso Martins Date: Fri, 4 Oct 2024 23:14:39 +0100 Subject: [PATCH 04/10] fix: uploader.ex restored --- lib/atomic/uploader.ex | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/lib/atomic/uploader.ex b/lib/atomic/uploader.ex index b029f4429..8bb59c9fe 100644 --- a/lib/atomic/uploader.ex +++ b/lib/atomic/uploader.ex @@ -9,29 +9,18 @@ defmodule Atomic.Uploader do use Waffle.Definition use Waffle.Ecto.Definition - def validate(file, _) do + def validate({file, _}) do file_extension = file.file_name |> Path.extname() |> String.downcase() case Enum.member?(extension_whitelist(), file_extension) do - true -> - if file.size <= max_size() do - :ok - else - {:error, "file size exceeds maximum allowed size"} - end - - false -> - {:error, "invalid file extension"} + true -> :ok + false -> {:error, "invalid file extension"} end end def extension_whitelist do Keyword.get(unquote(opts), :extensions, []) end - - def max_size do - Keyword.get(unquote(opts), :max_size, 500) - end end end end From e8a47ff9eb5f3a7851abcbdcb054f49178e12daf Mon Sep 17 00:00:00 2001 From: Afonso Martins Date: Wed, 9 Oct 2024 12:39:02 +0100 Subject: [PATCH 05/10] feat: remove max_size --- lib/atomic/uploaders/profile_picture.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/atomic/uploaders/profile_picture.ex b/lib/atomic/uploaders/profile_picture.ex index 7a2cbdf85..06a93519b 100644 --- a/lib/atomic/uploaders/profile_picture.ex +++ b/lib/atomic/uploaders/profile_picture.ex @@ -2,7 +2,7 @@ defmodule Atomic.Uploaders.ProfilePicture do @moduledoc """ Uploader for profile pictures. """ - use Atomic.Uploader, extensions: ~w(.jpg .jpeg .png .gif), max_size: 500 + use Atomic.Uploader, extensions: ~w(.jpg .jpeg .png .gif) alias Atomic.Accounts.User @versions [:original] From 46c0b6e5f005858f7819e22d62ebdfed7e3f5764 Mon Sep 17 00:00:00 2001 From: Afonso Martins Date: Wed, 9 Oct 2024 18:29:12 +0100 Subject: [PATCH 06/10] feat: some of the requested changes --- lib/atomic_web/components/socials.ex | 42 ++++++++++--------- .../live/partner_live/form_component.ex | 3 +- lib/atomic_web/live/partner_live/show.ex | 3 +- .../live/partner_live/show.html.heex | 10 +++-- 4 files changed, 32 insertions(+), 26 deletions(-) diff --git a/lib/atomic_web/components/socials.ex b/lib/atomic_web/components/socials.ex index f026a074e..85bef9867 100644 --- a/lib/atomic_web/components/socials.ex +++ b/lib/atomic_web/components/socials.ex @@ -2,28 +2,32 @@ defmodule AtomicWeb.Components.Socials do @moduledoc false use Phoenix.Component + attr :socials, :map, required: true + def socials(assigns) do ~H""" - <%= if @entity.socials do %> -
- <%= for {social, icon, url_base} <- [ - {:tiktok, "tiktok.svg", "https://tiktok.com/"}, - {:instagram, "instagram.svg", "https://instagram.com/"}, - {:facebook, "facebook.svg", "https://facebook.com/"}, - {:x, "x.svg", "https://x.com/"} - ] do %> - <% social_value = Map.get(@entity.socials, social) %> - <%= if social_value do %> -
- icon} class="h-5 w-5" alt={social |> Atom.to_string() |> String.capitalize()} /> - <.link class="text-blue-500" target="_blank" href={url_base <> social_value}> - <%= social |> Atom.to_string() |> String.capitalize() %> - -
- <% end %> +
+ <%= for {social, icon, url_base} <- get_socials() do %> + <% social_value = Map.get(@entity, social) %> + <%= if social_value do %> +
+ icon} class="h-5 w-5" alt={social |> Atom.to_string() |> String.capitalize()} /> + <.link class="text-blue-500" target="_blank" href={url_base <> social_value}> + <%= social |> Atom.to_string() |> String.capitalize() %> + +
<% end %> -
- <% end %> + <% end %> +
""" end + + def get_socials do + [ + {:tiktok, "tiktok.svg", "https://tiktok.com/"}, + {:instagram, "instagram.svg", "https://instagram.com/"}, + {:facebook, "facebook.svg", "https://facebook.com/"}, + {:x, "x.svg", "https://x.com/"} + ] + end end diff --git a/lib/atomic_web/live/partner_live/form_component.ex b/lib/atomic_web/live/partner_live/form_component.ex index bd6ec775f..f8fdfe07c 100644 --- a/lib/atomic_web/live/partner_live/form_component.ex +++ b/lib/atomic_web/live/partner_live/form_component.ex @@ -20,11 +20,12 @@ defmodule AtomicWeb.PartnerLive.FormComponent do <.field field={location_form[:name]} label="Address" type="text" placeholder="Address" help_text={gettext("Address of the partner")} required />

<%= gettext("Socials") %>

-
+
<.inputs_for :let={socials_form} field={f[:socials]}> <.field field={socials_form[:instagram]} type="text" class="w-full" /> <.field field={socials_form[:facebook]} type="text" class="w-full" /> <.field field={socials_form[:x]} type="text" class="w-full" /> + <.field field={socials_form[:tiktok]} type="text" class="w-full" /> <.field field={socials_form[:website]} type="text" class="w-full" />
diff --git a/lib/atomic_web/live/partner_live/show.ex b/lib/atomic_web/live/partner_live/show.ex index 375cc426a..c67b4409e 100644 --- a/lib/atomic_web/live/partner_live/show.ex +++ b/lib/atomic_web/live/partner_live/show.ex @@ -1,8 +1,7 @@ defmodule AtomicWeb.PartnerLive.Show do use AtomicWeb, :live_view - import AtomicWeb.Components.Avatar - import AtomicWeb.Components.Socials + import AtomicWeb.Components.{Avatar, Socials} alias Atomic.Accounts alias Atomic.Organizations diff --git a/lib/atomic_web/live/partner_live/show.html.heex b/lib/atomic_web/live/partner_live/show.html.heex index ce1795bad..8e6642e72 100644 --- a/lib/atomic_web/live/partner_live/show.html.heex +++ b/lib/atomic_web/live/partner_live/show.html.heex @@ -21,7 +21,7 @@
<%= if @partner.location do %> - +
<.icon name="hero-map-pin" class="h-5 w-5 text-zinc-400" /> <.link class="text-blue-500" href={"https://www.google.com/maps/search/?api=1&query=#{@partner.location.name}"}> @@ -31,7 +31,7 @@ <% end %> <%= if @partner.socials do %> - +
<%= if @partner.socials.website do %> @@ -40,8 +40,10 @@ <.link class="text-blue-500" href={@partner.socials.website}>Website
<% end %> - - <.socials entity={@partner} /> + + <%= if @partner.socials do %> + <.socials entity={@partner.socials} /> + <% end %>
<% end %>
From 610e62b6a99ac2e925341721f7cced2a66706085 Mon Sep 17 00:00:00 2001 From: Afonso Martins Date: Wed, 9 Oct 2024 18:32:59 +0100 Subject: [PATCH 07/10] feat: entity required --- lib/atomic_web/components/socials.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/atomic_web/components/socials.ex b/lib/atomic_web/components/socials.ex index 85bef9867..4bf3dc0e3 100644 --- a/lib/atomic_web/components/socials.ex +++ b/lib/atomic_web/components/socials.ex @@ -3,6 +3,7 @@ defmodule AtomicWeb.Components.Socials do use Phoenix.Component attr :socials, :map, required: true + attr :entity, :map, required: true def socials(assigns) do ~H""" From 0a36db78be81959fe9e807c1859b49a0b06cc261 Mon Sep 17 00:00:00 2001 From: Afonso Martins Date: Fri, 11 Oct 2024 22:44:04 +0100 Subject: [PATCH 08/10] feat: requested change --- lib/atomic_web/components/socials.ex | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/atomic_web/components/socials.ex b/lib/atomic_web/components/socials.ex index 4bf3dc0e3..6fa8cf761 100644 --- a/lib/atomic_web/components/socials.ex +++ b/lib/atomic_web/components/socials.ex @@ -2,14 +2,14 @@ defmodule AtomicWeb.Components.Socials do @moduledoc false use Phoenix.Component - attr :socials, :map, required: true attr :entity, :map, required: true def socials(assigns) do + assigns = assign(assigns, :socials_with_values, get_social_values(assigns.entity)) + ~H"""
- <%= for {social, icon, url_base} <- get_socials() do %> - <% social_value = Map.get(@entity, social) %> + <%= for {social, icon, url_base, social_value} <- @socials_with_values do %> <%= if social_value do %>
icon} class="h-5 w-5" alt={social |> Atom.to_string() |> String.capitalize()} /> @@ -23,6 +23,14 @@ defmodule AtomicWeb.Components.Socials do """ end + defp get_social_values(entity) do + get_socials() + |> Enum.map(fn {social, icon, url_base} -> + social_value = Map.get(entity, social) + {social, icon, url_base, social_value} + end) + end + def get_socials do [ {:tiktok, "tiktok.svg", "https://tiktok.com/"}, From 5065e3041057815ac58476f2bb4e65c0c3003b89 Mon Sep 17 00:00:00 2001 From: Afonso Martins Date: Fri, 11 Oct 2024 23:17:09 +0100 Subject: [PATCH 09/10] feat: add function to helpers.ex --- lib/atomic_web/components/helpers.ex | 8 +++++++- lib/atomic_web/components/socials.ex | 5 +++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/atomic_web/components/helpers.ex b/lib/atomic_web/components/helpers.ex index beee3c9b2..1970169ca 100644 --- a/lib/atomic_web/components/helpers.ex +++ b/lib/atomic_web/components/helpers.ex @@ -38,7 +38,7 @@ defmodule AtomicWeb.Components.Helpers do ...> %Flop.Filter{field: :age, op: :>, value: 25} ...> ] ...> } - ...> + ...> ...> to_query(f) [ filters: %{ @@ -101,4 +101,10 @@ defmodule AtomicWeb.Components.Helpers do |> maybe_put(:order_by, order_by) |> maybe_put(:order_directions, order_directions) end + + def atom_to_string_capitalize(atom) when is_atom(atom) do + atom + |> Atom.to_string() + |> String.capitalize() + end end diff --git a/lib/atomic_web/components/socials.ex b/lib/atomic_web/components/socials.ex index 6fa8cf761..acc3df0af 100644 --- a/lib/atomic_web/components/socials.ex +++ b/lib/atomic_web/components/socials.ex @@ -1,6 +1,7 @@ defmodule AtomicWeb.Components.Socials do @moduledoc false use Phoenix.Component + alias AtomicWeb.Components.Helpers attr :entity, :map, required: true @@ -12,9 +13,9 @@ defmodule AtomicWeb.Components.Socials do <%= for {social, icon, url_base, social_value} <- @socials_with_values do %> <%= if social_value do %>
- icon} class="h-5 w-5" alt={social |> Atom.to_string() |> String.capitalize()} /> + icon} class="h-5 w-5" alt={Helpers.atom_to_string_capitalize(social)} /> <.link class="text-blue-500" target="_blank" href={url_base <> social_value}> - <%= social |> Atom.to_string() |> String.capitalize() %> + <%= Helpers.atom_to_string_capitalize(social) %>
<% end %> From f85352ad8b389ff906f9bab21e7ffc24063001c6 Mon Sep 17 00:00:00 2001 From: Afonso Martins Date: Fri, 11 Oct 2024 23:17:39 +0100 Subject: [PATCH 10/10] code format --- lib/atomic_web/components/helpers.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/atomic_web/components/helpers.ex b/lib/atomic_web/components/helpers.ex index 1970169ca..b5c63d5f1 100644 --- a/lib/atomic_web/components/helpers.ex +++ b/lib/atomic_web/components/helpers.ex @@ -38,7 +38,7 @@ defmodule AtomicWeb.Components.Helpers do ...> %Flop.Filter{field: :age, op: :>, value: 25} ...> ] ...> } - ...> + ...> ...> to_query(f) [ filters: %{