Skip to content

Commit

Permalink
feat: add views info on content index
Browse files Browse the repository at this point in the history
  • Loading branch information
abehidek committed Mar 10, 2024
1 parent cdf3f3f commit dc5d9b1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
25 changes: 25 additions & 0 deletions lib/hidek_xyz/content/live_views.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,30 @@ defmodule HidekXyz.Content.LiveViews do
GenServer.start_link(__MODULE__, %__MODULE__{}, name: __MODULE__)
end

@impl true
def handle_call({:get_only, slug}, _from, %__MODULE__{} = state) do
case Map.get(state.content_views, slug) do
%ContentViews{} = content_views ->
{:reply, {:ok, content_views}, state}

nil ->
db_content_view =
case Repo.get_by(View, slug: slug) do
nil -> Repo.insert!(%View{count: 0, slug: slug})
%View{} = view -> view
end

new_content_view = %ContentViews{
count: db_content_view.count
}

new_state =
Map.put(state, :content_views, state.content_views |> Map.put(slug, new_content_view))

{:reply, {:ok, new_content_view}, new_state}
end
end

@impl true
def handle_call({:get, slug}, _from, %__MODULE__{} = state) do
case Map.get(state.content_views, slug) do
Expand Down Expand Up @@ -81,6 +105,7 @@ defmodule HidekXyz.Content.LiveViews do
end

def get(slug) when is_binary(slug), do: GenServer.call(__MODULE__, {:get, slug})
def get_only(slug) when is_binary(slug), do: GenServer.call(__MODULE__, {:get_only, slug})

@impl true
def handle_info(:sync_db, %__MODULE__{} = state) do
Expand Down
6 changes: 6 additions & 0 deletions lib/hidek_xyz_web/live/content/index_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ defmodule HidekXyzWeb.Content.IndexLive do
<h2 class="text-3xl font-bold mb-2"><%= article.frontmatter.title %></h2>
<h3 class="text-xl mb-1"><%= article.frontmatter.description %></h3>
<p class="text-lg"><%= article.frontmatter.publish_date %></p>
<%= live_render(@socket, HidekXyzWeb.ContentViewsLive,
sticky: true,
id: "content_views_live_#{article.slug}",
session: %{"id" => article.slug, "inc" => false}
) %>
</.link>
<%!-- <div class="absolute w-full top-0 flex flex-wrap gap-2 p-1">
<div class="flex flex-wrap bg-black bg-opacity-10 rounded-xl p-3 gap-3">
Expand Down
2 changes: 1 addition & 1 deletion lib/hidek_xyz_web/live/content/show_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ defmodule HidekXyzWeb.Content.ShowLive do
<%= live_render(@socket, HidekXyzWeb.ContentViewsLive,
sticky: true,
id: "content_views_live",
session: %{"id" => @article.slug}
session: %{"id" => @article.slug, "inc" => true}
) %>
</div>
</div>
Expand Down
7 changes: 5 additions & 2 deletions lib/hidek_xyz_web/live/content_views_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ defmodule HidekXyzWeb.ContentViewsLive do
alias HidekXyzWeb.{Endpoint}

@impl true
def mount(_params, %{"id" => id} = _session, socket) do
def mount(_params, %{"id" => id, "inc" => inc} = _session, socket) do
topic = "content_views_" <> id

socket =
if connected?(socket) do
{:ok, %HidekXyz.Content.LiveViews.ContentViews{} = content_views} =
HidekXyz.Content.LiveViews.get(id)
case inc do
true -> HidekXyz.Content.LiveViews.get(id)
false -> HidekXyz.Content.LiveViews.get_only(id)
end

Endpoint.subscribe(topic)

Expand Down

0 comments on commit dc5d9b1

Please sign in to comment.