Updated functionality for group join requests approve, reject, leave

• Updated:
- functionality for group join requests approve, reject, leave
This commit is contained in:
mgabdev 2020-09-11 17:22:33 -05:00
parent f8e9c99e17
commit 8c4f4899e7
5 changed files with 28 additions and 34 deletions

@ -37,12 +37,15 @@ class Api::V1::Groups::AccountsController < Api::BaseController
end
def destroy
authorize @group, :leave?
GroupAccount.where(group: @group, account_id: current_account.id).destroy_all
if current_user.allows_group_in_home_feed?
current_user.force_regeneration!
@join_request = GroupJoinRequest.where(group: @group, account_id: current_account.id)
if @join_request.count > 0
@join_request.destroy_all
else
authorize @group, :leave?
GroupAccount.where(group: @group, account_id: current_account.id).destroy_all
if current_user.allows_group_in_home_feed?
current_user.force_regeneration!
end
end
render json: @group, serializer: REST::GroupRelationshipSerializer, relationships: relationships

@ -16,29 +16,21 @@ class Api::V1::Groups::RequestsController < Api::BaseController
render json: @accounts, each_serializer: REST::AccountSerializer
end
def create
authorize @group, :leave?
GroupJoinRequest.where(group: @group, account_id: current_account.id).destroy_all
render json: @group, serializer: REST::GroupRelationshipSerializer, relationships: relationships
end
def approve_request
GroupJoinRequest.where(group: @group, account_id: params[:accountId]).destroy_all
GroupAccount.create(group: @group, account_id: params[:accountId])
render json: {"message": "ok"}
end
def reject_request
GroupJoinRequest.where(group: @group, account_id: params[:accountId]).destroy_all
render json: {"message": "ok"}
def respond_to_request
if params[:type] === 'reject'
GroupJoinRequest.where(group: @group, account_id: params[:accountId]).destroy_all
render json: { message: "ok", type: 'reject', accountId: params[:accountId] }
elsif params[:type] === 'approve'
GroupJoinRequest.where(group: @group, account_id: params[:accountId]).destroy_all
GroupAccount.create(group: @group, account_id: params[:accountId])
render json: { message: "ok", type: 'approve', accountId: params[:accountId] }
else
render json: { message: "error", error: true }, status: 422
end
end
private
def relationships
GroupRelationshipsPresenter.new([@group.id], current_user.account_id)
end
def set_group
@group = Group.find(params[:group_id])
end

@ -702,8 +702,8 @@ export function expandJoinRequestsFail(id, error) {
export const approveJoinRequest = (accountId, groupId) => (dispatch, getState) => {
if (!me) return
api(getState).post(`/api/v1/groups/${groupId}/join_requests/approve`, { accountId }).then((response) => {
dispatch(approveJoinRequestSuccess(accountId, groupId))
api(getState).post(`/api/v1/groups/${groupId}/join_requests/respond`, { accountId, type: 'approve' }).then((response) => {
dispatch(approveJoinRequestSuccess(response.data.accountId, groupId))
}).catch((error) => {
dispatch(approveJoinRequestFail(accountId, groupId, error))
})
@ -729,8 +729,9 @@ export function approveJoinRequestFail(accountId, groupId, error) {
export const rejectJoinRequest = (accountId, groupId) => (dispatch, getState) => {
if (!me) return
api(getState).delete(`/api/v1/groups/${groupId}/join_requests/reject`, { accountId }).then((response) => {
dispatch(rejectJoinRequestSuccess(accountId, groupId))
api(getState).post(`/api/v1/groups/${groupId}/join_requests/respond`, { accountId, type: 'reject' }).then((response) => {
console.log("response:", response)
dispatch(rejectJoinRequestSuccess(response.data.accountId, groupId))
}).catch((error) => {
dispatch(rejectJoinRequestFail(accountId, groupId, error))
})

@ -179,8 +179,7 @@ export default function userLists(state = initialState, action) {
return appendToList(state, 'group_join_requests', action.id, action.accounts, action.next);
case GROUP_JOIN_REQUESTS_APPROVE_SUCCESS:
case GROUP_JOIN_REQUESTS_REJECT_SUCCESS:
return state.updateIn(['group_join_requests', action.groupId, 'items'], list => list.filterNot(item => item === action.id));
return state.updateIn(['group_join_requests', action.groupId, 'items'], list => list.filterNot(item => item === action.accountId));
default:
return state;
}

@ -440,10 +440,9 @@ Rails.application.routes.draw do
resources :relationships, only: :index, controller: 'groups/relationships'
resource :accounts, only: [:show, :create, :update, :destroy], controller: 'groups/accounts'
resource :removed_accounts, only: [:show, :create, :destroy], controller: 'groups/removed_accounts'
resource :join_requests, only: [:show, :create], controller: 'groups/requests'
resource :join_requests, only: [:show], controller: 'groups/requests'
post '/join_requests/approve', to: 'groups/requests#approve_request'
delete '/join_requests/reject', to: 'groups/requests#reject_request'
post '/join_requests/respond', to: 'groups/requests#respond_to_request'
resource :pin, only: :create, controller: 'groups/pins'
post :unpin, to: 'groups/pins#destroy'