Skip to content

Commit

Permalink
move song administration to song page, and add music permisson group
Browse files Browse the repository at this point in the history
  • Loading branch information
adriansalamon committed Sep 12, 2024
1 parent ac0a6bc commit 9d5ae5a
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 162 deletions.
7 changes: 7 additions & 0 deletions lib/haj/policy/policy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,11 @@ defmodule Haj.Policy do
allow role: :admin
end
end

object :songs do
action :edit do
allow role: :admin
allow group_member: :music
end
end
end
2 changes: 1 addition & 1 deletion lib/haj/spex/group.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Haj.Spex.Group do
use Ecto.Schema
import Ecto.Changeset

@group_permissions [:chefsgruppen, :grafiq]
@group_permissions [:chefsgruppen, :grafiq, :music]

schema "groups" do
field :name, :string
Expand Down
6 changes: 0 additions & 6 deletions lib/haj_web/components/layouts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,6 @@ defmodule HajWeb.Layouts do
title="Ansvar"
active={@active_tab == {:setting, :responsibilities}}
/>
<:sub_link
navigate={~p"/settings/songs"}
title="Sånger"
active={@active_tab == {:setting, :song}}
/>
</.nav_link_group>
</div>
"""
Expand Down
1 change: 0 additions & 1 deletion lib/haj_web/live/nav_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ defmodule HajWeb.Nav do
tab SettingsLive.User.Index, {:setting, :users}
tab SettingsLive.Merch.Index, {:setting, :merch}
tab SettingsLive.Responsibility.Index, {:setting, :responsibilities}
tab SettingsLive.Song.Index, {:setting, :song}

defp set_active_tab(params, _url, socket) do
active_tab =
Expand Down
3 changes: 0 additions & 3 deletions lib/haj_web/live/settings_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ defmodule HajWeb.SettingsLive.Index do
<.setting_card name="Ansvar" navigate={~p"/settings/responsibilities"}>
Redigera ansvar
</.setting_card>
<.setting_card name="Sånger" navigate={~p"/settings/songs"}>
Redigera sånger
</.setting_card>
</div>
</div>
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule HajWeb.SettingsLive.Song.FormComponent do
defmodule HajWeb.SongLive.Edit.FormComponent do
use HajWeb, :live_component

alias Haj.Archive
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
defmodule HajWeb.SettingsLive.Song.Index do
defmodule HajWeb.SongLive.Edit.Index do
use HajWeb, :live_view

alias Haj.Archive
alias Haj.Archive.Song
alias Haj.Repo

on_mount {HajWeb.UserAuth, {:authorize, :songs_edit}}

@impl true
def mount(_params, _session, socket) do
{:ok, stream(socket, :songs, Archive.list_songs() |> Repo.preload(:show))}
Expand Down Expand Up @@ -34,7 +36,7 @@ defmodule HajWeb.SettingsLive.Song.Index do
end

@impl true
def handle_info({HajWeb.SettingsLive.Song.FormComponent, {:saved, song}}, socket) do
def handle_info({HajWeb.SongLive.Edit.FormComponent, {:saved, song}}, socket) do
{:noreply, stream_insert(socket, :songs, song |> Repo.preload(:show))}
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<.header>
Listing Songs
<:actions>
<.link patch={~p"/settings/songs/new"}>
<.link patch={~p"/songs/edit/new"}>
<.button>New Song</.button>
</.link>
</:actions>
Expand All @@ -10,17 +10,17 @@
<.table
id="songs"
rows={@streams.songs}
row_click={fn {_id, song} -> JS.navigate(~p"/settings/songs/#{song}") end}
row_click={fn {_id, song} -> JS.navigate(~p"/songs/edit/#{song}") end}
>
<:col :let={{_id, song}} label="Name"><%= song.name %></:col>
<:col :let={{_id, song}} label="Original name"><%= song.original_name %></:col>
<:col :let={{_id, song}} label="Number"><%= song.number %></:col>
<:col :let={{_id, song}} label="Spex"><%= song.show.year.year %></:col>
<:action :let={{_id, song}}>
<div class="sr-only">
<.link navigate={~p"/settings/songs/#{song}"}>Show</.link>
<.link navigate={~p"/songs/edit/#{song}"}>Show</.link>
</div>
<.link patch={~p"/settings/songs/#{song}/edit"}>Edit</.link>
<.link patch={~p"/songs/edit/#{song}/edit"}>Edit</.link>
</:action>
<:action :let={{id, song}}>
<.link
Expand All @@ -36,14 +36,14 @@
:if={@live_action in [:new, :edit]}
id="song-modal"
show
on_cancel={JS.navigate(~p"/settings/songs")}
on_cancel={JS.navigate(~p"/songs/edit")}
>
<.live_component
module={HajWeb.SettingsLive.Song.FormComponent}
module={HajWeb.SongLive.Edit.FormComponent}
id={@song.id || :new}
title={@page_title}
action={@live_action}
song={@song}
patch={~p"/settings/songs"}
patch={~p"/songs/edit"}
/>
</.modal>
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
defmodule HajWeb.SettingsLive.Song.Show do
defmodule HajWeb.SongLive.Edit.Show do
use HajWeb, :live_view

alias Haj.Archive
on_mount {HajWeb.UserAuth, {:authorize, :songs_edit}}

@impl true
def mount(_params, _session, socket) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Song <%= @song.id %>
<:subtitle>This is a song record from your database.</:subtitle>
<:actions>
<.link patch={~p"/settings/songs/#{@song}/show/edit"} phx-click={JS.push_focus()}>
<.link patch={~p"/songs/edit/#{@song}/show/edit"} phx-click={JS.push_focus()}>
<.button>Edit song</.button>
</.link>
</:actions>
Expand All @@ -22,20 +22,20 @@
</div>
</div>

<.back navigate={~p"/settings/songs"}>Back to songs</.back>
<.back navigate={~p"/songs/edit"}>Back to songs</.back>

<.modal
:if={@live_action == :edit}
id="song-modal"
show
on_cancel={JS.patch(~p"/settings/songs/#{@song}")}
on_cancel={JS.patch(~p"/songs/edit/#{@song}")}
>
<.live_component
module={HajWeb.SettingsLive.Song.FormComponent}
module={HajWeb.SongLive.Edit.FormComponent}
id={@song.id}
title={@page_title}
action={@live_action}
song={@song}
patch={~p"/settings/songs/#{@song}"}
patch={~p"/songs/edit/#{@song}"}
/>
</.modal>
11 changes: 10 additions & 1 deletion lib/haj_web/live/song_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule HajWeb.SongLive.Index do

alias Haj.Archive
alias Haj.Spex
alias Haj.Policy

def mount(_params, _session, socket) do
show = Spex.current_spex()
Expand All @@ -14,8 +15,16 @@ defmodule HajWeb.SongLive.Index do
[key: "#{year.year}: #{title}", value: id]
end)

authorized_admin = Policy.authorize?(:songs_edit, socket.assigns.current_user)

{:ok,
assign(socket, page_title: "Sånger", songs: songs, show: show, show_options: show_options)}
assign(socket,
page_title: "Sånger",
songs: songs,
show: show,
show_options: show_options,
authorized_admin: authorized_admin
)}
end

def handle_event("select_show", %{"show" => show_id}, socket) do
Expand Down
20 changes: 16 additions & 4 deletions lib/haj_web/live/song_live/index.html.heex
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
<div class="mr-auto flex w-full flex-col items-baseline justify-between pt-4">
<span class="text-2xl font-bold">Sångarkiv</span>
<span class="text-sm text-gray-600">
Här finns alla sånger som har gjorts i spexet genom åren. Kika och lyssna in!
</span>
<div class="flex w-full flex-row items-center justify-between">
<div class="flex flex-col">
<span class="text-2xl font-bold">Sångarkiv</span>
<span class="text-sm text-gray-600">
Här finns alla sånger som har gjorts i spexet genom åren. Kika och lyssna in!
</span>
</div>

<.link
:if={@authorized_admin}
navigate={~p"/songs/edit"}
class="text-md bg-burgandy-500 block rounded-md px-3 py-1 text-sm text-white hover:bg-burgandy-400"
>
Administrera
</.link>
</div>
<.form id="show-form" as={:show} for={%{}} phx-change="select_show" class="w-full pt-2">
<.input name="show" label="Välj spex" type="select" value={@show.id} options={@show_options} />
</.form>
Expand Down
14 changes: 7 additions & 7 deletions lib/haj_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ defmodule HajWeb.Router do
live "/applications/:id", ApplicationsLive.Show, :show
live "/applications/:id/confirm", ApplicationsLive.Show, :approve

## Song administration
live "/songs/edit", SongLive.Edit.Index, :index
live "/songs/edit/new", SongLive.Edit.Index, :new
live "/songs/edit/:id/edit", SongLive.Edit.Index, :edit
live "/songs/edit/:id", SongLive.Edit.Show, :show
live "/songs/edit/:id/show/edit", SongLive.Edit.Show, :edit

## Songs
live "/songs", SongLive.Index, :index
live "/songs/:id", SongLive.Show, :show
Expand Down Expand Up @@ -154,13 +161,6 @@ defmodule HajWeb.Router do
:new_responsible

live "/responsibilities/:id/show/edit", SettingsLive.Responsibility.Show, :edit

live "/songs", SettingsLive.Song.Index, :index
live "/songs/new", SettingsLive.Song.Index, :new
live "/songs/:id/edit", SettingsLive.Song.Index, :edit

live "/songs/:id", SettingsLive.Song.Show, :show
live "/songs/:id/show/edit", SettingsLive.Song.Show, :edit
end
end
end
Expand Down
123 changes: 0 additions & 123 deletions test/haj_web/live/song_live_test.exs

This file was deleted.

0 comments on commit 9d5ae5a

Please sign in to comment.