Skip to content

Commit

Permalink
add export csv button
Browse files Browse the repository at this point in the history
  • Loading branch information
adriansalamon committed Oct 30, 2023
1 parent 85d184a commit 9a36cfb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
5 changes: 5 additions & 0 deletions lib/haj/policy/policy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ defmodule Haj.Policy do
allow :is_chef
allow role: :admin
end

action :export do
allow :is_chef
allow role: :admin
end
end

object :responsibility_comment do
Expand Down
16 changes: 6 additions & 10 deletions lib/haj_web/controllers/group_controller.ex
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
defmodule HajWeb.GroupController do
use HajWeb, :controller
alias Haj.Policy

def csv(conn, %{"show_group_id" => show_group_id}) do
show_group = Haj.Spex.get_show_group!(show_group_id)
users = Enum.map(show_group.group_memberships, fn %{user: user} -> user end)

if is_admin?(conn, show_group) do
if Policy.authorize?(:show_group_export, conn.assigns.current_user, show_group) do
csv_data = to_csv(users)

conn
|> put_resp_content_type("text/csv")
|> put_resp_header("content-disposition", "attachment; filename=\"medlemmar.csv\"")
|> put_resp_header(
"content-disposition",
"attachment; filename=\"haj-medlemmar-#{show_group.group.name}.csv\""
)
|> put_root_layout(false)
|> send_resp(200, csv_data)
else
Expand Down Expand Up @@ -68,12 +72,4 @@ defmodule HajWeb.GroupController do

CSV.encode([titles | users]) |> Enum.to_list()
end

defp is_admin?(conn, show_group) do
conn.assigns.current_user.role == :admin ||
show_group.group_memberships
|> Enum.any?(fn %{user_id: id, role: role} ->
role == :chef && id == conn.assigns.current_user.id
end)
end
end
15 changes: 12 additions & 3 deletions lib/haj_web/live/group_live/admin.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
<p class="py-2 text-sm font-bold text-zinc-800">
Välj vilken typ av medlem (chef/gruppis), sök på användare och lägg sedan till!
</p>
<div class="flex flex-row items-stretch gap-2">
<div class="flex flex-col items-stretch gap-2 sm:flex-row">
<.form :let={f} for={%{}} as={:role_form} phx-change="update_role">
<%= select(f, :role, @roles, class: "h-full rounded-md border-zinc-400", value: @role) %>
<%= select(f, :role, @roles, class: "h-full w-full rounded-md border-zinc-300", value: @role) %>
</.form>

<.form
Expand All @@ -33,8 +33,17 @@
autocomplete={:off}
class="flex-grow"
>
<%= text_input(f, :q, value: @query, class: "w-full rounded-md border-zinc-400") %>
<%= text_input(f, :q, value: @query, class: "w-full rounded-md border-zinc-300") %>
</.form>

<.link
href={~p"/group/#{@show_group}/csv"}
method="post"
class="ml-auto flex flex-row items-center gap-2 rounded-lg border px-3 py-2 hover:bg-gray-50"
>
<.icon name={:arrow_down_on_square_stack} mini class="h-5 w-5" />
<span class="text-sm">Exportera</span>
</.link>
</div>

<ol id="matches" class="mt-2 flex flex-col overflow-hidden rounded-md bg-gray-50">
Expand Down
2 changes: 1 addition & 1 deletion lib/haj_web/live/group_live/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
Administrera
</.link>
</div>
<div :if={@group.application_description} class="">
<div>
<h4 class="text-l font-bold">Vad gör <%= @group.group.name %>?</h4>
<p class="whitespace-pre-wrap text-sm"><%= @group.group.description %></p>
</div>
Expand Down

0 comments on commit 9a36cfb

Please sign in to comment.