Skip to content

Commit

Permalink
Merge pull request #77 from datasektionen/feat/new-applications-page
Browse files Browse the repository at this point in the history
feat: new application page and possible to approve people
  • Loading branch information
adriansalamon authored Oct 4, 2023
2 parents a8d4a2c + 5277216 commit 85d184a
Show file tree
Hide file tree
Showing 23 changed files with 468 additions and 193 deletions.
5 changes: 5 additions & 0 deletions lib/haj/policy/policy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,10 @@ defmodule Haj.Policy do
allow role: :admin
allow current_group_member: :chefsgruppen
end

action :approve do
allow :is_chef
allow role: :admin
end
end
end
2 changes: 1 addition & 1 deletion lib/haj_web/components/components/steps.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<%= "Steg #{Enum.find_index(@step, fn step -> step.status == :current end) + 1} av #{length(@step)}" %>
</p>
<ol role="list" class="ml-8 flex items-center space-x-5">
<li :for={{step, index} <- Enum.with_index(@step)} class="relative md:flex md:flex-1">
<li :for={{step, _index} <- Enum.with_index(@step)} class="relative md:flex md:flex-1">
<.link
:if={step.status == :complete}
navigate={step.to}
Expand Down
11 changes: 7 additions & 4 deletions lib/haj_web/components/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ defmodule HajWeb.CoreComponents do

slot :col, required: true do
attr :label, :string
attr :class, :string
end

slot :action, doc: "the slot for showing user actions in the last table column"
Expand All @@ -497,10 +498,12 @@ defmodule HajWeb.CoreComponents do

~H"""
<div class="overflow-y-auto px-4 sm:overflow-visible sm:px-0">
<table class={["mt-11", @small && "w-full", @small || "w-[40rem] sm:w-full"]}>
<table class={["mt-6", @small && "w-full", @small || "w-[40rem] sm:w-full"]}>
<thead class="text-[0.8125rem] text-left leading-6 text-zinc-500">
<tr>
<th :for={col <- @col} class="p-0 pr-6 pb-4 font-normal"><%= col[:label] %></th>
<th :for={col <- @col} class={["p-0 pr-6 pb-4 font-normal", Map.get(col, :class, "")]}>
<%= col[:label] %>
</th>
<th class="relative p-0 pb-4"><span class="sr-only"><%= gettext("Actions") %></span></th>
</tr>
</thead>
Expand All @@ -513,7 +516,7 @@ defmodule HajWeb.CoreComponents do
<td
:for={{col, i} <- Enum.with_index(@col)}
phx-click={@row_click && @row_click.(row)}
class={["relative p-0", @row_click && "hover:cursor-pointer"]}
class={["relative p-0", @row_click && "hover:cursor-pointer", Map.get(col, :class, "")]}
>
<div class="block py-4 pr-6">
<span class="absolute -inset-y-px right-0 -left-4 group-hover:bg-zinc-50 sm:rounded-l-xl" />
Expand Down Expand Up @@ -580,7 +583,7 @@ defmodule HajWeb.CoreComponents do

~H"""
<div class="overflow-y-auto px-4 sm:overflow-visible sm:px-0">
<table class={["mt-11", @small && "w-full", @small || "w-[40rem] sm:w-full"]}>
<table class={["mt-6", @small && "w-full", @small || "w-[40rem] sm:w-full"]}>
<thead class="text-[0.8125rem] text-left leading-6 text-zinc-500">
<tr>
<th :for={col <- @col} class="p-0 pr-6 pb-4 font-normal"><%= col[:label] %></th>
Expand Down
8 changes: 8 additions & 0 deletions lib/haj_web/components/layouts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ defmodule HajWeb.Layouts do
/>
</.nav_link_group>
<.nav_link
:if={Policy.authorize?(:applications_read, @current_user)}
navigate={~p"/applications"}
icon_name={:user_plus}
title="Ansökningar"
active={@active_tab == :applications}
/>
<.nav_link
navigate={~p"/songs"}
icon_name={:musical_note}
Expand Down
8 changes: 4 additions & 4 deletions lib/haj_web/components/layouts/live.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div class="fixed inset-0 bg-gray-600 bg-opacity-75" phx-click={hide_mobile_sidebar()}></div>
<div
id="mobile-sidebar"
class="bg-burgandy-500 relative flex min-h-screen w-full max-w-xs flex-1 flex-col pt-4"
class="bg-burgandy-500 relative flex h-screen w-full max-w-xs flex-1 flex-col overflow-y-auto pt-4"
>
<div class="mt-2 flex w-full flex-row items-center justify-between px-4">
<svg class="-mt-1 inline-block h-8 w-8 fill-white" viewBox="0 0 391.85 526.59">
Expand All @@ -19,7 +19,7 @@
<.icon name={:chevron_left} class="h-full w-full text-white" />
</button>
</div>
<nav class="px-4 pt-6">
<nav class="px-4 py-6">
<.sidebar_nav_links
:if={@current_user}
current_user={@current_user}
Expand Down Expand Up @@ -52,7 +52,7 @@
</div>
<!-- Sidebar -->
<div class="flex h-0 flex-1 flex-col">
<nav class="overflow-y-auto px-4">
<nav class="overflow-y-auto px-4 pb-6">
<.sidebar_nav_links
:if={@current_user}
current_user={@current_user}
Expand All @@ -70,7 +70,7 @@
</div>
</div>
<!-- Top bar -->
<div class="flex w-screen flex-col lg:pl-64">
<div class="flex min-h-screen w-screen flex-col lg:pl-64">
<div
:if={@current_user}
class="flex w-full flex-row items-center justify-between border-b px-4 py-4 lg:px-6 lg:py-4"
Expand Down
4 changes: 4 additions & 0 deletions lib/haj_web/components/live_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ defmodule HajWeb.LiveHelpers do

def format_date(other), do: other

@colors ~w"#8dd3c7 #ffffb3 #bebada #fb8072 #80b1d3 #fdb462 #b3de69 #fccde5 #d9d9d9 #bc80bd #ccebc5 #ffed6f"

def get_color(:bg, index), do: Enum.at(@colors, rem(index - 1, 12), "#4e79a7")

def pick_text_color(hex_color) do
colors = hex_to_rgb(hex_color)

Expand Down
147 changes: 0 additions & 147 deletions lib/haj_web/live/applications_live.ex

This file was deleted.

91 changes: 91 additions & 0 deletions lib/haj_web/live/applications_live/approve_component.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
defmodule HajWeb.ApplicationsLive.ApproveComponent do
use HajWeb, :live_component

alias Haj.Policy
alias Haj.Spex

def update(%{application: app} = assigns, socket) do
memberships =
Haj.Spex.get_show_groups_for_user(app.user_id)
|> Enum.map(fn %{id: id} -> id end)

{:ok, socket |> assign(assigns) |> assign(memberships: memberships)}
end

def handle_event("approve", %{"show_group" => show_group_id}, socket) do
show_group = Haj.Spex.get_show_group!(show_group_id)
application = socket.assigns.application

if Policy.authorize?(
:applications_approve,
socket.assigns.current_user,
show_group
) do
if Spex.is_member_of_show_group?(application.user.id, show_group_id) do
push_flash(
:error,
"#{application.user.full_name} är redan medlem i #{show_group.group.name}."
)
else
case Haj.Spex.create_group_membership(%{
user_id: application.user.id,
show_group_id: show_group_id,
role: :gruppis
}) do
{:ok, _} ->
push_flash(
:info,
"#{application.user.full_name} antogs till #{show_group.group.name}."
)

{:error, _} ->
push_flash(:error, "Något gick fel.")
end
end
else
push_flash(:error, "Du har inte rättigheter att anta till denna grupp.")
end

{:noreply, socket}
end

def render(assigns) do
~H"""
<div>
<.header>
Antag <%= @application.user.full_name %>
<:subtitle>Antag personen till grupper.</:subtitle>
</.header>
<.form
:let={_f}
for={%{}}
as={:group}
phx-submit="approve"
class="mt-2 flex flex-col gap-2"
phx-target={@myself}
>
<.input
type="select"
name="show_group"
prompt="Välj grupp"
class="flex-grow"
value=""
options={group_options(@application.application_show_groups, @memberships)}
/>
<div class="flex flex-row justify-end">
<.button type="submit">
Antag
</.button>
</div>
</.form>
</div>
"""
end

defp group_options(show_groups, memberships) do
Enum.reject(show_groups, fn asg -> asg.show_group_id in memberships end)
|> Enum.map(fn asg -> [key: asg.show_group.group.name, value: asg.show_group.id] end)
end
end
Loading

0 comments on commit 85d184a

Please sign in to comment.