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

Remove users from a poll #854

Merged
merged 2 commits into from
Mar 2, 2020
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
1 change: 1 addition & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
['name' => 'vote#get', 'url' => '/votes/get/{pollId}', 'verb' => 'GET'],
['name' => 'vote#set', 'url' => '/vote/set/', 'verb' => 'POST'],
['name' => 'vote#write', 'url' => '/vote/write/', 'verb' => 'POST'],
['name' => 'vote#delete', 'url' => '/votes/delete/', 'verb' => 'POST'],

['name' => 'option#get', 'url' => '/options/get/{pollId}', 'verb' => 'GET'],
['name' => 'option#add', 'url' => '/option/add/', 'verb' => 'POST'],
Expand Down
34 changes: 34 additions & 0 deletions lib/Controller/VoteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public function get($pollId) {
*/
public function set($pollId, $option, $userId, $setTo) {

$this->logger->alert('Deleting vote no. ' . $option);
try {
$vote = $this->mapper->findSingleVote($pollId, $option['pollOptionText'], $userId);
$vote->setVoteAnswer($setTo);
Expand All @@ -158,6 +159,39 @@ public function set($pollId, $option, $userId, $setTo) {
}
}


/**
* delete
* @NoAdminRequired
* @param integer $voteId
* @param string $userId
* @param integer $pollId
* @return DataResponse
*/
public function delete($voteId = 0, $userId = '', $pollId = 0) {
$this->logger->alert('Deleting vote no. ' . $voteId);

try {
if ($voteId) {
$vote = $this->mapper->delete($voteId);
$this->logger->alert('Deleting vote no. ' . $voteId);
return new DataResponse(null, Http::STATUS_OK);
} elseif ($pollId && $userId) {
$votes = $this->mapper->deleteByPollAndUser($pollId, $userId);
$this->logger->alert('Deleting votes from ' . $userId . ' in poll ' . $pollId);
return new DataResponse(null, Http::STATUS_OK);
} elseif ($pollId) {
$votes = $this->mapper->deleteByPoll($pollId);
$this->logger->alert('Deleting all votes in poll ' . $pollId);
return new DataResponse(null, Http::STATUS_OK);
} else {
return DataResponse(null, Http::STATUS_NOT_FOUND);
}
} catch (DoesNotExistException $e) {
return DataResponse(null, Http::STATUS_NOT_FOUND);
}
}

/**
* Public functions
*/
Expand Down
11 changes: 10 additions & 1 deletion src/js/components/Base/UserDiv.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<div v-if="description" class="description">
{{ description }}
</div>
<Avatar :disable-menu="true" :user="userId"
<Avatar :disable-menu="disableMenu" :menu-position="menuPosition" :user="userId"
:is-guest="!Boolean(OC.currentUser)"
:display-name="displayName"

Expand All @@ -35,6 +35,7 @@
<div v-if="!hideNames" class="user-name">
{{ displayName }}
</div>
<slot />
</div>
</template>

Expand All @@ -53,6 +54,14 @@ export default {
type: Boolean,
default: false
},
disableMenu: {
type: Boolean,
default: false
},
menuPosition: {
type: String,
default: 'left'
},
userId: {
type: String,
default: undefined
Expand Down
13 changes: 11 additions & 2 deletions src/js/components/VoteTable/VoteHeaderPublic.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
{{ t('polls', 'Your personal link to this poll: %n', 1, personalLink) }}
<a class="icon icon-clippy" @click="copyLink()" />
</div>
<Modal v-show="!isValidUser &!expired & modal" class="modal"
:can-close="false">
<Modal v-show="!isValidUser &!expired & modal" :can-close="false">
<div class="modal__content">
<h2>{{ t('polls', 'Enter your name!') }}</h2>

Expand Down Expand Up @@ -192,6 +191,7 @@ export default {
.voteHeader {
margin: 8px 24px;
}

.modal__content {
padding: 14px;
display: flex;
Expand All @@ -210,6 +210,15 @@ export default {
}
}

.modal__buttons {
display: flex;
justify-content: end;
.button {
margin-left: 10px;
margin-right: 0;
}
}

.personal-link {
display: flex;
padding: 4px 12px;
Expand Down
89 changes: 73 additions & 16 deletions src/js/components/VoteTable/VoteTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

<template lang="html">
<div class="vote-table">
<div class="header">
<div class="sticky" />
<div class="vote-table__header">
<div class="vote-table__user-column" />

<VoteTableHeader v-for="(option) in sortedOptions"
:key="option.id"
Expand All @@ -33,31 +33,63 @@

<div v-for="(participant) in participants" :key="participant.userId" :class="{currentuser: (participant.userId === currentUser) }">
<UserDiv :key="participant.userId"
class="sticky"
class="vote-table__user-column"
:disable-menu="true"
:class="{currentuser: (participant.userId === currentUser) }"
:user-id="participant.userId"
:display-name="participant.displayName" />
:display-name="participant.displayName">
<Actions v-if="acl.allowEdit" class="action">
<ActionButton icon="icon-delete"
@click="confirmDelete(participant.userId)">
{{ t('polls', 'Delete votes') }}
</ActionButton>
</Actions>
</UserDiv>

<VoteTableItem v-for="(option) in sortedOptions"
:key="option.id"
:user-id="participant.userId"
:option="option"
@voteClick="setVote(option, participant.userId)" />
</div>

<Modal v-if="modal">
<div class="modal__content">
<h2>{{ t('polls', 'Do you want to remove {username} from poll?', { username: userToRemove }) }}</h2>
<div class="modal__buttons">
<ButtonDiv :title="t('polls', 'No')"
@click="modal = false" />
<ButtonDiv :primary="true" :title="t('polls', 'Yes')"
@click="removeUser()" />
</div>
</div>
</Modal>
</div>
</template>

<script>
import VoteTableItem from './VoteTableItem'
import VoteTableHeader from './VoteTableHeader'
import { mapState, mapGetters } from 'vuex'
import { Actions, ActionButton, Modal } from '@nextcloud/vue'

export default {
name: 'VoteTable',
components: {
Actions,
ActionButton,
Modal,
VoteTableHeader,
VoteTableItem
},

data() {
return {
modal: false,
userToRemove: ''
}
},

computed: {
...mapState({
poll: state => state.poll,
Expand All @@ -75,6 +107,19 @@ export default {
},

methods: {
removeUser() {
this.$store.dispatch('deleteVotes', {
userId: this.userToRemove
})
this.modal = false
this.userToRemove = ''
},

confirmDelete(userId) {
this.userToRemove = userId
this.modal = true
},

setVote(option, userId) {
this.$store
.dispatch('setVoteAsync', {
Expand All @@ -94,16 +139,34 @@ export default {
</script>

<style lang="scss" scoped>
.user-row.sticky,
.header > .sticky {
.user-row.vote-table__user-column,
.vote-table__header > .vote-table__user-column {
position: sticky;
left: 0;
background-color: var(--color-main-background);
width: 170px;
width: 230px;
flex: 0 0 auto;
}

.header {
.modal__content {
padding: 14px;
display: flex;
flex-direction: column;
color: var(--color-main-text);
input {
width: 100%;
}
}
.modal__buttons {
display: flex;
justify-content: end;
.button {
margin-left: 10px;
margin-right: 0;
}
}

.vote-table__header {
height: 150px;
}

Expand Down Expand Up @@ -139,7 +202,7 @@ export default {
flex: 1;
}

&.header {
&.vote-table__header {
order: 1;
}

Expand Down Expand Up @@ -172,12 +235,6 @@ export default {
margin: 0;

}
// &.currentuser {
// display: flex;
// > .user-row.currentuser {
// display: none;
// }
// }
}

&> .currentuser {
Expand All @@ -188,7 +245,7 @@ export default {
}
}

&> .header, {
&> .vote-table__header, {
height: initial;
padding-left: initial;
display: flex;
Expand Down
20 changes: 20 additions & 0 deletions src/js/store/modules/votes.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ const mutations = {
Object.assign(state, payload)
},

deleteVotes(state, payload) {
state.list = state.list.filter(vote => vote.userId !== payload.userId)
},

setVote(state, payload) {
const index = state.list.findIndex(vote =>
parseInt(vote.pollId) === payload.pollId
Expand Down Expand Up @@ -172,6 +176,22 @@ const actions = {
})
},

deleteVotes(context, payload) {
const endPoint = 'apps/polls/votes/delete/'
return axios.post(OC.generateUrl(endPoint), {
pollId: context.rootState.poll.id,
voteId: 0,
userId: payload.userId
})
.then(() => {
context.commit('deleteVotes', payload)
OC.Notification.showTemporary(t('polls', 'User {userId} removed', payload), { type: 'success' })
}, (error) => {
console.error('Error deleting votes', { error: error.response }, { payload: payload })
throw error
})
},

setVoteAsync(context, payload) {
let endPoint = 'apps/polls/vote/set/'

Expand Down