Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve post mgmt #372

Merged
merged 3 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions app/assets/javascripts/application/posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
function removePost(group_id, membership_id, post_type_id, post_id) {
const button = document.getElementById(`post-action-button-${membership_id}-${post_type_id}`)
button.disabled = true
fetch(`/groups/${group_id}/memberships/${membership_id}/posts/${post_id}.json`,
{
headers: {
'X-CSRF-TOKEN': getCsrfToken(),
},
method: 'DELETE',
credentials: 'same-origin',
})
.then(response => {
if (response.status === 200) {
button.onclick = () => addPost(group_id, membership_id, post_type_id)

button.classList.remove("uk-button-danger")
button.classList.add("uk-button-success")

const buttonTexts = button.innerText.split('(')
buttonTexts[1] = "(Hozzáad)"
button.innerText = buttonTexts.join('')
button.disabled = false
}
})
}

function addPost(group_id, membership_id, post_type_id) {
const button = document.getElementById(`post-action-button-${membership_id}-${post_type_id}`)
button.disabled = true
fetch(`/groups/${group_id}/memberships/${membership_id}/posts.json`, {
headers: {
'X-CSRF-TOKEN': getCsrfToken(),
'Content-Type': 'application/json'
},
method: 'POST',
credentials: 'same-origin',
body: JSON.stringify({post_type_id: post_type_id})
}).then(response => {
if (response.status === 200) {
response.json().then(data => {
button.onclick = () => removePost(group_id, membership_id, post_type_id, data.post_id)

button.classList.remove("uk-button-success")
button.classList.add("uk-button-danger")

const buttonTexts = button.innerText.split('(')
buttonTexts[1] = "(Elvesz)"
button.innerText = buttonTexts.join('')

button.disabled = false
});
}
})
}
33 changes: 33 additions & 0 deletions app/assets/stylesheets/posts.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

.wrapper {
position: relative;
overflow: auto;
border: 1px solid lightgray;
white-space: nowrap;
}

.sticky-col {
position: -webkit-sticky;
position: sticky;
background-color: white;
}

.first-col {
width: 120px;
min-width: 120px;
max-width: 120px;
left: 0px;
white-space: normal;
}

td, thead {
background-color: white;
}

tr:hover > td {
background-color: #99baca;
}

tr:hover > .first-col {
background-color: #99baca;
}
33 changes: 29 additions & 4 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,49 @@ def index
@membership = Membership.find(params[:membership_id])
end

def group_posts
@group = Group.includes(:post_types, :memberships).find(params[:group_id])
authorize @group, :manage_posts?

@post_type_ids = @group.post_types.map(&:id)
.reject { |post_type_id| post_type_id.eql? PostType::LEADER_POST_ID }
@group_post_types = @group.post_types.reduce({}) do |hash, post_type|
hash.merge({ post_type.id => post_type })
end
@memberships = @group.memberships.active.includes(:posts, :user)
.sort { |a, b| hu_compare(a.user.full_name, b.user.full_name) }
end

def create
group = Group.find(params[:group_id])
membership = Membership.find(params[:membership_id])
post_type_id = params[:post_type_id].to_i
authorize Post.new(membership: membership, post_type_id: post_type_id)

new_post = CreatePost.call(group, membership, post_type_id)
return redirect_to group_path(group) if new_post.leader?
respond_to do |format|
format.html do
return redirect_to group_path(group) if new_post.leader?

redirect_back fallback_location: group_path(group)
redirect_back fallback_location: group_path(group)
end
format.json { render json: { post_id: new_post.id } }
end
end

def destroy
post_id = params[:id]
group_url = group_path(params[:group_id])
authorize Post.find(post_id)
return redirect_back fallback_location: group_url if DestroyPost.call(post_id)

redirect_back fallback_location: group_url, alert: t(:no_leader_error)
destroyed = DestroyPost.call(post_id)
respond_to do |format|
format.html do
return redirect_back fallback_location: group_url if destroyed

redirect_back fallback_location: group_url, alert: t(:no_leader_error)
end
format.json { return render plain: 'ok', status: :ok }
end
end
end
6 changes: 6 additions & 0 deletions app/decorators/membership_view_model_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ def evaluation_button
'uk-button-success')
end

def all_posts_button
return unless policy(current_group).manage_posts?

styled_link_to('Posztok kezelése', group_posts_path(current_group), 'uk-button-success')
end

def all_members_count
membership_view_model.group.memberships.count
end
Expand Down
1 change: 1 addition & 0 deletions app/views/groups/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<%= @viewmodel.join_group_button %>
<%= @viewmodel.withdraw_membership_button %>
<%= @viewmodel.evaluation_button %>
<%= @viewmodel.all_posts_button %>
</div>
</div>

Expand Down
44 changes: 44 additions & 0 deletions app/views/posts/group_posts.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<%= stylesheet_link_tag params[:controller] %>

<%= button_to "Vissza", group_path(@group.id), method: :get,
class: "uk-button uk-button-primary uk-button-large uk-margin-bottom" %>

<div class="view">
<div class="wrapper">
<table id="posts-table" class="table">
<thead>
<tr>
<th class="sticky-col first-col"></th>
<% @post_type_ids.each do |id| %>
<th><%= @group_post_types[id].name %></th>
<% end %>
</tr>
</thead>
<tbody>
<% @memberships.each do |membership| %>
<tr>
<td class="sticky-col first-col">
<%= link_to membership.user.full_name, profile_path(membership.user.screen_name), target: :blank, rel: "noreferrer" %>
</td>
<% @post_type_ids.each do |post_type_id| %>
<% post = membership.posts.to_a.find { |post| post.post_type_id == post_type_id } %>
<% post_type = @group_post_types[post_type_id] %>

<td class="uk-vertical-align">
<% if post.present? %>
<%= button_tag "#{post_type.name} (Elvesz)", class: "uk-button uk-button-danger uk-width-1-1 uk-align-center",
id: "post-action-button-#{membership.id}-#{post_type_id}", onclick: "removePost(#{@group.id}, #{membership.id},#{post_type_id}, #{post.id})" %>
<% else %>
<%= button_tag "#{post_type.name} (Hozzáad)", class: "uk-button uk-button-success uk-width-1-1 uk-align-center",
id: "post-action-button-#{membership.id}-#{post_type_id}", onclick: "addPost(#{@group.id}, #{membership.id},#{post_type_id})" %>
<% end %>
<% end %>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>

<%= button_to "Vissza", group_path(@group.id), method: :get,
class: "uk-button uk-button-primary uk-button-large uk-margin-top" %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
resources :posts, only: [:index, :create, :destroy]
end

get '/group_posts', to: "posts#group_posts", as: :posts
resources :post_types, only: [:index, :create, :destroy]

get '/evaluations/current', to: 'evaluations#current'
Expand Down
20 changes: 20 additions & 0 deletions spec/requests/posts_controller_spec_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
describe PostsController do

describe '#all_posts' do
before(:each) do
login_as(user)
end
let(:user) { group.leader.user }

context 'when the group exists' do
let(:group) { create(:group) }

it 'is ok' do
get "/groups/#{group.id}/group_posts"

expect(response).to have_http_status :ok
expect(response).to render_template :group_posts
end
end
end
end