diff --git a/assets/js/app.js b/assets/js/app.js index ae2adb5d..403e93a5 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -21,7 +21,7 @@ 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, @@ -29,7 +29,7 @@ let Hooks = { Confetti: Confetti, Sorting: Sorting, Timer: Timer, - SpotlightBanner: SpotlightBanner + Banner: Banner }; let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content") diff --git a/assets/js/hooks/spotlight_banner.js b/assets/js/hooks/banner.js similarity index 92% rename from assets/js/hooks/spotlight_banner.js rename to assets/js/hooks/banner.js index 7dbc71d9..c905051c 100644 --- a/assets/js/hooks/spotlight_banner.js +++ b/assets/js/hooks/banner.js @@ -1,4 +1,4 @@ -export const SpotlightBanner = { +export const Banner = { mounted() { const banner = this.el; diff --git a/assets/js/hooks/index.js b/assets/js/hooks/index.js index 8caacf77..ff49f2b9 100644 --- a/assets/js/hooks/index.js +++ b/assets/js/hooks/index.js @@ -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"; diff --git a/lib/safira/companies.ex b/lib/safira/companies.ex index 7ec18720..df41012e 100644 --- a/lib/safira/companies.ex +++ b/lib/safira/companies.ex @@ -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 diff --git a/lib/safira/companies/tier.ex b/lib/safira/companies/tier.ex index 6c301b76..a38301d5 100644 --- a/lib/safira/companies/tier.ex +++ b/lib/safira/companies/tier.ex @@ -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 @@ -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 diff --git a/lib/safira/spotlights.ex b/lib/safira/spotlights.ex index 76780f84..2d1d59a5 100644 --- a/lib/safira/spotlights.ex +++ b/lib/safira/spotlights.ex @@ -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 @@ -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 @@ -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 diff --git a/lib/safira/spotlights/spotlight.ex b/lib/safira/spotlights/spotlight.ex index ccc2e28d..a7d60ae6 100644 --- a/lib/safira/spotlights/spotlight.ex +++ b/lib/safira/spotlights/spotlight.ex @@ -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 diff --git a/lib/safira_web/components/banner.ex b/lib/safira_web/components/banner.ex index ecd49b10..dbf3f1bf 100644 --- a/lib/safira_web/components/banner.ex +++ b/lib/safira_web/components/banner.ex @@ -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"""
- <%= gettext("%{company_name} is on spotlight!", company_name: @company_name) %> +

<%= @text %>

00:00:00
diff --git a/lib/safira_web/components/layouts.ex b/lib/safira_web/components/layouts.ex index 7168a2b6..b1526f5b 100644 --- a/lib/safira_web/components/layouts.ex +++ b/lib/safira_web/components/layouts.ex @@ -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 diff --git a/lib/safira_web/components/layouts/app.html.heex b/lib/safira_web/components/layouts/app.html.heex index 0618e126..350b7c1e 100644 --- a/lib/safira_web/components/layouts/app.html.heex +++ b/lib/safira_web/components/layouts/app.html.heex @@ -19,11 +19,10 @@ <% end %>
<.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} + />