Skip to content

Commit

Permalink
feat: requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AfonsoMartins26 committed Jan 10, 2025
1 parent 7cd2d8d commit 8e87950
Show file tree
Hide file tree
Showing 19 changed files with 54 additions and 90 deletions.
4 changes: 2 additions & 2 deletions assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import "phoenix_html"
import {Socket} from "phoenix"
import {LiveSocket} from "phoenix_live_view"
import topbar from "../vendor/topbar"
import { QrScanner, Wheel, Confetti, Sorting , Timer, SpotlightBanner} from "./hooks";
import { QrScanner, Wheel, Confetti, Sorting , Timer, Banner} from "./hooks";

let Hooks = {
QrScanner: QrScanner,
Wheel: Wheel,
Confetti: Confetti,
Sorting: Sorting,
Timer: Timer,
SpotlightBanner: SpotlightBanner
Banner: Banner
};

let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const SpotlightBanner = {
export const Banner = {
mounted() {
const banner = this.el;

Expand Down
2 changes: 1 addition & 1 deletion assets/js/hooks/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { QrScanner } from "./qr_reading.js";
export { Wheel } from "./wheel.js";
export { Confetti } from "./confetti.js";
export { SpotlightBanner } from "./spotlight_banner.js";
export { Banner } from "./banner.js";
export { Sorting } from "./sorting.js";
export { Timer } from "./time.js";
export { Countdown } from "./countdown.js";
6 changes: 3 additions & 3 deletions lib/safira/companies.ex
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,14 @@ defmodule Safira.Companies do
(Repo.aggregate(from(t in Tier), :max, :priority) || -1) + 1
end

def update_tier_multiplier(%Tier{} = tier, multiplier, max_spotlights) do
def update_tier_spotlight_configuration(%Tier{} = tier, multiplier, max_spotlights) do
tier
|> Tier.changeset_multiplier(%{multiplier: multiplier, max_spotlights: max_spotlights})
|> Tier.changeset_spotlight_configuration(%{spotlight_multiplier: multiplier, max_spotlights: max_spotlights})
|> Repo.update()
end

def change_tier_multiplier(%Tier{} = tier, attrs \\ %{}) do
Tier.changeset_multiplier(tier, attrs)
Tier.changeset_spotlight_configuration(tier, attrs)
end

def get_company_spotlights_count(company_id) do
Expand Down
8 changes: 4 additions & 4 deletions lib/safira/companies/tier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule Safira.Companies.Tier do
schema "tiers" do
field :name, :string
field :priority, :integer
field :multiplier, :float, default: 0.0
field :spotlight_multiplier, :float, default: 0.0
field :max_spotlights, :integer, default: 1

has_many :companies, Safira.Companies.Company, foreign_key: :tier_id
Expand All @@ -26,9 +26,9 @@ defmodule Safira.Companies.Tier do
|> validate_required(@required_fields)
end

def changeset_multiplier(tier, attrs) do
def changeset_spotlight_configuration(tier, attrs) do
tier
|> cast(attrs, [:multiplier, :max_spotlights])
|> validate_required([:multiplier, :max_spotlights])
|> cast(attrs, [:spotlight_multiplier, :max_spotlights])
|> validate_required([:spotlight_multiplier, :max_spotlights])
end
end
28 changes: 10 additions & 18 deletions lib/safira/spotlights.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,19 @@ defmodule Safira.Spotlights do

@pubsub Safira.PubSub

def create_spotlight(attrs) do
def create_spotlight(company_id) do
now = DateTime.utc_now()
duration = attrs.duration

duration = get_spotlight_duration()
if duration > 0 do
end_time = DateTime.add(now, duration, :minute)

spotlight_attrs = Map.put(attrs, :end, end_time)

%Spotlight{}
|> Spotlight.changeset(spotlight_attrs)
|> Spotlight.changeset(%{company_id: company_id, end: end_time})
|> Repo.insert()
|> case do
{:ok, spotlight} ->
broadcast_new_spotlight(spotlight.id)
{:ok, spotlight}

{:error, changeset} ->
{:error, changeset}
end
Expand All @@ -42,21 +38,17 @@ defmodule Safira.Spotlights do
|> Repo.one()
end

def get_duration do
Constants.get("duration_spotlights")
end

def change_duration_spotlight(time) do
Constants.set("duration_spotlights", time)
def change_spotlight_duration(time) do
Constants.set("spotlight_duration", time)
end

def get_spotlights_duration do
case Constants.get("duration_spotlights") do
def get_spotlight_duration do
case Constants.get("spotlight_duration") do
{:ok, duration} ->
duration

{:error, _} ->
change_duration_spotlight(0)
change_spotlight_duration(0)
0
end
end
Expand Down Expand Up @@ -139,10 +131,10 @@ defmodule Safira.Spotlights do
"""
def change_spotlight(%Spotlight{} = spotlight, attrs \\ %{}) do
Spotlight.changeset(spotlight, attrs)
subscribe_to_spotlight_change()
subscribe_to_spotlight_event()
end

def subscribe_to_spotlight_change() do
def subscribe_to_spotlight_event() do
Phoenix.PubSub.subscribe(@pubsub, "spotlight")
end

Expand Down
9 changes: 4 additions & 5 deletions lib/safira/spotlights/spotlight.ex
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
defmodule Safira.Spotlights.Spotlight do
use Safira.Schema

@required_fields ~w(end company_id)a

schema "spotlights" do
field :end, :utc_datetime

belongs_to :company, Safira.Companies.Company

timestamps(type: :utc_datetime)
end

@doc false
def changeset(spotlight, attrs) do
spotlight
|> cast(attrs, [:end, :company_id])
|> validate_required([:end, :company_id])
|> cast(attrs, @required_fields)
|> validate_required(@required_fields)
|> foreign_key_constraint(:company_id)
end
end
12 changes: 5 additions & 7 deletions lib/safira_web/components/banner.ex
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
defmodule SafiraWeb.Components.Banner do
use SafiraWeb, :component

attr :title, :string, default: ""
attr :text, :string, default: ""
attr :duration, :integer, default: 5000
attr :type, :string, default: "info"
attr :company_name, :string, default: ""


def banner(assigns) do
~H"""
<div
id="spotlight-banner"
phx-hook="SpotlightBanner"
id="banner"
phx-hook="Banner"
class="relative w-full bg-white text-center p-4 text-black font-bold shadow-lg z-10 transition-transform transform"
style="top: 0;"
data-duration={@duration}
>
<%= gettext("%{company_name} is on spotlight!", company_name: @company_name) %>
<p><%= @text %></p>
<div id="timer-countdown" phx-hook="Timer" data-finish-time={DateTime.to_unix(@duration)}>
00:00:00
</div>
Expand Down
7 changes: 2 additions & 5 deletions lib/safira_web/components/layouts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ defmodule SafiraWeb.Layouts do
"""
use SafiraWeb, :html

import SafiraWeb.Components.Sidebar
import SafiraWeb.Landing.Components.Sparkles
import SafiraWeb.Landing.Components.Navbar
import SafiraWeb.Landing.Components.Footer
import SafiraWeb.Components.Banner
import SafiraWeb.Components.{Sidebar,Banner}
import SafiraWeb.Landing.Components.{Footer, Navbar, Sparkles}

embed_templates "layouts/*"
end
9 changes: 4 additions & 5 deletions lib/safira_web/components/layouts/app.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
<% end %>
<div class="flex flex-col flex-1 overflow-hidden">
<.banner
:if={@current_spotlight}
title={gettext("Spotlights")}
company_name={@current_spotlight.company.name}
duration={@current_spotlight.end}
/>
:if={@current_spotlight}
text={gettext("%{company_name} is on spotlight!", company_name: @current_spotlight.company.name)}
duration={@current_spotlight.end}
/>
<div class="bg-primary flex justify-end lg:hidden px-4 sm:px-6 py-2">
<button
class="sidebar-toggle flex items-center justify-center w-16 z-20 dark:text-light text-white"
Expand Down
10 changes: 3 additions & 7 deletions lib/safira_web/live/backoffice/spotlights_live/confirm.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule SafiraWeb.Backoffice.SpotlightLive.Confirm do
<p class="text-center text-2xl mb-4">Are you sure?</p>
<p class="text-center pb-6">
<%= gettext(
"Are you sure you want to start a spotlight for %{company_name} with a duration of %{duration} %{unit}.",
"Are you sure you want to start a spotlight for %{company_name} with a duration of %{duration} %{unit}?",
company_name: @company.name,
duration: @duration,
unit: ngettext("minute", "minutes", @duration)
Expand All @@ -35,13 +35,9 @@ defmodule SafiraWeb.Backoffice.SpotlightLive.Confirm do
def handle_event("confirm-spotlight", _params, socket) do
if socket.assigns.company && socket.assigns.duration &&
can_create_spotlight?(socket.assigns.company.id) do
attrs = %{
company_id: socket.assigns.company.id,
company_name: socket.assigns.company.name,
duration: socket.assigns.duration
}

case Spotlights.create_spotlight(attrs) do

case Spotlights.create_spotlight(socket.assigns.company_id) do
{:ok, _spotlight} ->
{:noreply,
socket
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ defmodule SafiraWeb.Backoffice.SpotlightLive.FormComponent do

@impl true
def mount(socket) do
duration = Spotlights.get_spotlights_duration() || 0
duration = Spotlights.get_spotlight_duration() || 0
form = to_form(%{"duration" => duration}, as: :spotlight_config)

{:ok, socket |> assign(form: form)}
end

@impl true
def handle_event("save", %{"spotlight_config" => %{"duration" => duration}}, socket) do
Spotlights.change_duration_spotlight(String.to_integer(duration))
Spotlights.change_spotlight_duration(String.to_integer(duration))
{:noreply, socket |> push_patch(to: ~p"/dashboard/spotlights/")}
end
end
4 changes: 2 additions & 2 deletions lib/safira_web/live/backoffice/spotlights_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule SafiraWeb.Backoffice.SpotlightLive.Index do
@impl true
def mount(_params, _session, socket) do
if connected?(socket) do
Spotlights.subscribe_to_spotlight_change()
Spotlights.subscribe_to_spotlight_event()
end

{:ok, socket |> assign(:spotlight, Spotlights.get_current_spotlight())}
Expand Down Expand Up @@ -57,7 +57,7 @@ defmodule SafiraWeb.Backoffice.SpotlightLive.Index do
end

defp apply_action(socket, :confirm, %{"id" => company_id}) do
{:ok, duration} = Safira.Spotlights.get_duration()
{:ok, duration} = Safira.Spotlights.get_spotlight_duration()

socket
|> assign(:page_title, "Confirm Spotlight")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

<.modal
:if={@live_action in [:config]}
id="spotlight-config"
id="spotlight-config-modal"
show
on_cancel={JS.patch(~p"/dashboard/spotlights")}
>
Expand All @@ -53,7 +53,7 @@

<.modal
:if={@live_action in [:tiers]}
id="tiers-config"
id="tiers-config-modal"
show
on_cancel={JS.patch(~p"/dashboard/spotlights/config")}
>
Expand All @@ -67,7 +67,7 @@

<.modal
:if={@live_action in [:tiers_edit]}
id="tiers-edit"
id="tiers-edit-modal"
show
on_cancel={JS.patch(~p"/dashboard/spotlights/config/tiers")}
>
Expand Down Expand Up @@ -97,7 +97,7 @@

<.modal
:if={@live_action in [:confirm]}
id="spotlight-confirm"
id="spotlight-confirm-modal"
show
on_cancel={JS.patch(~p"/dashboard/spotlights")}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ defmodule SafiraWeb.Backoffice.SpotlightLive.Tiers.FormComponent do
end

defp save_tier(socket, :tiers_edit, multiplier, max_spotlights) do
case Companies.update_tier_multiplier(socket.assigns.tier, multiplier, max_spotlights) do
case Companies.update_tier(socket.assigns.tier, %{multiplier: multiplier, max_spotlights: max_spotlights}) do
{:ok, _tier} ->
{:noreply,
socket
Expand Down
17 changes: 2 additions & 15 deletions lib/safira_web/live/backoffice/spotlights_live/tiers/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ defmodule SafiraWeb.Backoffice.SpotlightLive.Tiers.Index do
<.page title={@title}>
<ul id="tiers" class="h-96 mt-8 pb-8 flex flex-col space-y-2 overflow-y-auto">
<li
:for={{_, tier} <- @streams.tiers}
id={"tier-" <> tier.id}
:for={{id, tier} <- @streams.tiers}
id={id}
class="even:bg-lightShade/20 dark:even:bg-darkShade/20 py-4 px-4 flex flex-row justify-between"
>
<div class="flex flex-row gap-2 items-center">
Expand All @@ -35,17 +35,4 @@ defmodule SafiraWeb.Backoffice.SpotlightLive.Tiers.Index do
socket
|> stream(:tiers, Companies.list_tiers())}
end

@impl true
def handle_event("update-sorting", %{"ids" => ids}, socket) do
ids
|> Enum.with_index(0)
|> Enum.each(fn {"tier-" <> id, index} ->
id
|> Companies.get_tier!()
|> Companies.update_tier(%{priority: index})
end)

{:noreply, socket}
end
end
2 changes: 1 addition & 1 deletion lib/safira_web/mounts/spotlight.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule SafiraWeb.Spotlight do

def on_mount(:fetch_current_spotlight, _params, _session, socket) do
if connected?(socket) do
Spotlights.subscribe_to_spotlight_change()
Spotlights.subscribe_to_spotlight_event()
end

{:cont,
Expand Down
Loading

0 comments on commit 8e87950

Please sign in to comment.