2022-04-06 14:55:12 -04:00
defmodule AggieditWeb.PostLive.Index do
use AggieditWeb , :live_view
2022-04-13 14:42:01 -04:00
alias Aggiedit.Roles
2022-04-06 14:55:12 -04:00
alias Aggiedit.Rooms
2022-04-13 18:55:02 -04:00
alias Aggiedit.Rooms.Post
2022-04-06 20:17:27 -04:00
alias Aggiedit.Repo
2022-04-06 14:55:12 -04:00
@impl true
2022-04-13 18:55:02 -04:00
def mount ( %{ " room_id " = > _room_id } = params , session , socket ) do
{ :ok , socket } = assign_socket_room_and_user_or_error ( params , session , socket )
2022-04-06 16:41:19 -04:00
case socket . assigns do
2022-04-14 13:43:34 -04:00
%{ :room = > room } ->
if connected? ( socket ) , do : Rooms . subscribe ( socket . assigns . room )
2022-04-15 15:00:42 -04:00
posts = room
|> Repo . preload ( posts : [ :user , :upload ] )
|> Map . get ( :posts )
votes = socket . assigns . current_user
|> Repo . preload ( :votes )
|> Map . get ( :votes )
|> Enum . reduce ( %{ } , fn v , a -> Map . put ( a , v . post_id , v ) end )
{ :ok , assign ( socket , %{ :posts = > posts , :votes = > votes } ) , temporary_assigns : [ posts : [ ] ] }
2022-04-13 16:17:28 -04:00
_ -> { :ok , socket }
2022-04-06 16:41:19 -04:00
end
2022-04-06 14:55:12 -04:00
end
2022-04-13 14:42:01 -04:00
@impl true
def handle_params ( %{ " id " = > id } = params , _url , socket ) do
2022-04-13 16:17:28 -04:00
post = Rooms . get_post! ( id )
if Roles . guard? ( socket . assigns . current_user , socket . assigns . live_action , post ) do
{ :noreply , apply_action ( socket , socket . assigns . live_action , params ) }
2022-04-13 14:42:01 -04:00
else
2022-04-13 16:17:28 -04:00
{ :noreply , socket |> put_flash ( :error , " You do not have permission to edit this post. " ) |> redirect ( to : Routes . post_index_path ( socket , :index , socket . assigns . room ) ) }
2022-04-13 14:42:01 -04:00
end
end
2022-04-06 14:55:12 -04:00
@impl true
def handle_params ( params , _url , socket ) do
{ :noreply , apply_action ( socket , socket . assigns . live_action , params ) }
end
2022-04-13 18:55:02 -04:00
defp apply_action ( socket , :edit , %{ " id " = > id } ) do
2022-04-06 14:55:12 -04:00
socket
|> assign ( :page_title , " Edit Post " )
2022-04-08 15:01:24 -04:00
|> assign ( :post , Rooms . get_post! ( id ) |> Repo . preload ( :upload ) )
2022-04-06 14:55:12 -04:00
end
defp apply_action ( socket , :new , _params ) do
socket
|> assign ( :page_title , " New Post " )
2022-04-07 17:52:32 -04:00
|> assign ( :post , % Post { } )
2022-04-06 14:55:12 -04:00
end
defp apply_action ( socket , :index , _params ) do
socket
|> assign ( :page_title , " Listing Posts " )
|> assign ( :post , nil )
end
2022-04-15 15:00:42 -04:00
def handle_event ( vote , %{ " id " = > id } , socket ) when vote in [ " upvote " , " downvote " ] do
post = Rooms . get_post! ( id )
if Roles . guard? ( socket . assigns . current_user , :vote , post ) do
Rooms . vote_post ( post , socket . assigns . current_user , vote )
{ :noreply , socket }
else
{ :noreply , socket |> put_flash ( :error , " You don't have permission to do that. " ) |> redirect ( to : Routes . post_show_path ( socket , :show , socket . assigns . room , post ) ) }
end
end
2022-04-06 14:55:12 -04:00
@impl true
def handle_event ( " delete " , %{ " id " = > id } , socket ) do
post = Rooms . get_post! ( id )
2022-04-13 14:42:01 -04:00
if Roles . guard? ( socket . assigns . current_user , :delete , post ) do
Rooms . delete_post ( post )
2022-04-13 16:17:28 -04:00
{ :noreply , socket |> put_flash ( :success , " Post deleted. " ) |> redirect ( to : Routes . post_index_path ( socket , :index , socket . assigns . room ) ) }
2022-04-13 14:42:01 -04:00
else
2022-04-13 16:17:28 -04:00
{ :noreply , socket |> put_flash ( :error , " You do not have permission to delete this post. " ) |> redirect ( to : Routes . post_index_path ( socket , :index , socket . assigns . room ) ) }
2022-04-13 14:42:01 -04:00
end
2022-04-06 14:55:12 -04:00
end
2022-04-14 13:43:34 -04:00
@impl true
2022-04-15 15:00:42 -04:00
def handle_info ( { action , post } , socket ) when action in [ :post_created , :post_updated , :post_deleted , :post_voted ] do
2022-04-14 13:43:34 -04:00
{ :noreply , update ( socket , :posts , fn posts ->
[ posts | post ]
end ) }
end
2022-04-06 14:55:12 -04:00
end