Skip to content

Commit

Permalink
Add Ratings index page
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkonidas committed Aug 10, 2023
1 parent a4bebf1 commit b2b4f71
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/plexus/query_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ defmodule Plexus.QueryHelpers do
{:preload, associations}, query ->
from q in query, preload: ^associations

{:google_lib, google_lib}, query ->
from q in query, where: q.google_lib == ^google_lib

_, query ->
query
end)
Expand Down
7 changes: 7 additions & 0 deletions lib/plexus/ratings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ defmodule Plexus.Ratings do
|> broadcast(:app_rating_updated)
end

@spec delete_rating(Rating.t()) :: {:ok, Rating.t()} | {:error, Ecto.Changeset.t()}
def delete_rating(%Rating{} = rating) do
rating
|> Repo.delete()
|> broadcast(:rating_deleted)
end

defp broadcast({:error, _reason} = error, _event), do: error

defp broadcast({:ok, rating}, event) do
Expand Down
20 changes: 18 additions & 2 deletions lib/plexus_web/live/admin/app_live/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,24 @@
</div>
</:item>

<:item title="Native"><.score app={@app} google_lib={:none} /></:item>
<:item title="MicroG"><.score app={@app} google_lib={:micro_g} /></:item>
<:item title="Native">
<.score app={@app} google_lib={:none} />
<.link
navigate={~p"/admin/apps/#{@app}/ratings/none"}
class="block text-sm font-semibold leading-6 text-zinc-900 hover:text-zinc-700"
>
View ratings
</.link>
</:item>
<:item title="MicroG">
<.score app={@app} google_lib={:micro_g} />
<.link
navigate={~p"/admin/apps/#{@app}/ratings/micro_g"}
class="block text-sm font-semibold leading-6 text-zinc-900 hover:text-zinc-700 self-end"
>
View ratings
</.link>
</:item>
</.list>

<.back navigate={~p"/admin/apps"}>Back to apps</.back>
Expand Down
76 changes: 76 additions & 0 deletions lib/plexus_web/live/admin/rating_live/index.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
defmodule PlexusWeb.Admin.RatingLive.Index do
use PlexusWeb, :live_view

alias Plexus.Apps
alias Plexus.Ratings

@impl Phoenix.LiveView
def mount(%{"package" => package, "google_lib" => google_lib}, _session, socket) do
if connected?(socket), do: Apps.subscribe(package)

page =
Ratings.list_ratings(package,
google_lib: google_lib,
page_size: 9999,
order_by: [desc: :app_build_number, desc: :updated_at]
)

{:ok,
socket
|> assign(:google_lib, google_lib)
|> assign(:app, Apps.get_app!(package, scores: true))
|> stream(:ratings, page.entries)}
end

@impl Phoenix.LiveView
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end

defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "#{socket.assigns.app.name} Ratings")
end

@impl Phoenix.LiveView
def handle_info({:app_updated, app}, socket) do
{:noreply,
socket
|> put_flash(:info, "'#{app.name}' Updated")
|> assign(:app, Apps.get_app!(app.package, scores: true))}
end

def handle_info({:app_deleted, _app}, socket) do
{:noreply, push_navigate(socket, to: ~p"/admin/apps")}
end

def handle_info({:rating_deleted, rating}, socket) do
{:noreply,
socket
|> put_flash(:info, "Rating Deleted")
|> assign(:app, Apps.get_app!(socket.assigns.app.package, scores: true))
|> stream_delete(:ratings, rating)}
end

def handle_info({:app_rating_updated, rating}, socket) do
if rating.google_lib == String.to_existing_atom(socket.assigns.google_lib) do
{:noreply,
socket
|> put_flash(:info, "Rating Added")
|> assign(:app, Apps.get_app!(rating.app_package, scores: true))
|> stream_insert(:ratings, rating)}
else
{:noreply, socket}
end
end

@impl Phoenix.LiveView
def handle_event("delete", %{"id" => id}, socket) do
{:ok, rating} =
id
|> Ratings.get_rating!()
|> Ratings.delete_rating()

{:noreply, stream_delete(socket, :ratings, rating)}
end
end
39 changes: 39 additions & 0 deletions lib/plexus_web/live/admin/rating_live/index.html.heex
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<.header>
<%= @app.name %> <%= if @google_lib == "none", do: "Native", else: "MicroG" %> Ratings
<:subtitle>
<.badge score={Enum.find(@app.scores, &(to_string(&1.google_lib) == @google_lib))} />
</:subtitle>
</.header>

<.table id="ratings" rows={@streams.ratings}>
<:col :let={{_id, rating}} label="Score">
<p class="text-sm text-gray-700"><%= rating.score %></p>
</:col>
<:col :let={{_id, rating}} label="App Version">
<p class="text-sm text-gray-700">
<%= rating.app_version %> (<%= rating.app_build_number %>)
</p>
</:col>
<:col :let={{_id, rating}} label="ROM">
<p class="text-sm text-gray-700"><%= rating.rom_name %> (<%= rating.rom_build %>)</p>
</:col>
<:col :let={{_id, rating}} label="Android Version">
<p class="text-sm text-gray-700"><%= rating.android_version %></p>
</:col>
<:col :let={{_id, rating}} label="Installation Source">
<p class="text-sm text-gray-700"><%= rating.installation_source %></p>
</:col>
<:col :let={{_id, rating}} label="Notes">
<p class="text-sm text-gray-700 whitespace-pre-line"><%= rating.notes %></p>
</:col>
<:action :let={{id, rating}}>
<.link
phx-click={JS.push("delete", value: %{id: rating.id}) |> hide("##{id}")}
data-confirm="Are you sure?"
>
Delete
</.link>
</:action>
</.table>

<.back navigate={~p"/admin/apps/#{@app}"}>Back to app</.back>
2 changes: 2 additions & 0 deletions lib/plexus_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ defmodule PlexusWeb.Router do

live "/apps/:package", AppLive.Show, :show
live "/apps/:package/show/edit", AppLive.Show, :edit

live "/apps/:package/ratings/:google_lib", RatingLive.Index, :index
end

scope "/api/v1", PlexusWeb.API.V1 do
Expand Down

0 comments on commit b2b4f71

Please sign in to comment.