From b7850ee5f5c54adfe017d0756992540d51948d02 Mon Sep 17 00:00:00 2001 From: Charlie K Date: Sat, 5 May 2018 21:55:44 -0700 Subject: [PATCH 1/3] Start work on no refresh --- js/forum/dist/extension.js | 150 +++++++++-------------- js/forum/src/components/EditPollModal.js | 26 ++-- js/forum/src/components/PollModal.js | 19 +-- js/forum/src/components/PollVote.js | 75 ++++++------ js/forum/src/main.js | 13 +- 5 files changed, 117 insertions(+), 166 deletions(-) diff --git a/js/forum/dist/extension.js b/js/forum/dist/extension.js index 99b91cc..31722ea 100644 --- a/js/forum/dist/extension.js +++ b/js/forum/dist/extension.js @@ -171,7 +171,7 @@ System.register('reflar/polls/components/EditPollModal', ['flarum/extend', 'flar m('input', { className: 'FormControl', type: 'text', oninput: m.withAttr('value', _this3.updateAnswer.bind(_this3, answer)), - value: answer.data.attributes.answer, + value: answer.answer(), placeholder: app.translator.trans('reflar-polls.forum.modal.answer_placeholder') + ' #' + (i + 1) }) ), i + 1 >= 3 ? Button.component({ @@ -221,17 +221,14 @@ System.register('reflar/polls/components/EditPollModal', ['flarum/extend', 'flar value: function addAnswer(answer) { var _this4 = this; + var data = { + answer: this.newAnswer(), + poll_id: this.props.poll.id(), + user_id: this.pollCreator.id() + }; if (this.answers.length < 10) { - app.request({ - method: 'POST', - url: app.forum.attribute('apiUrl') + '/answers', - data: { - answer: this.newAnswer(), - poll_id: this.props.poll.id(), - user_id: this.pollCreator.id() - } - }).then(function (response) { - _this4.answers.push(response); + app.store.createRecord('answers').save(data).then(function (answer) { + _this4.answers.push(answer); _this4.newAnswer(''); m.redraw(); @@ -275,11 +272,6 @@ System.register('reflar/polls/components/EditPollModal', ['flarum/extend', 'flar } }); } - }, { - key: 'onhide', - value: function onhide() { - location.reload(); - } }, { key: 'updateQuestion', value: function updateQuestion(question) { @@ -448,8 +440,7 @@ System.register('reflar/polls/components/PollModal', ['flarum/extend', 'flarum/c m( 'fieldset', { style: 'margin-bottom: 15px', className: 'Poll-answer-input' }, - m('input', { style: 'opacity: 1', className: 'FormControl', type: 'text', 'data-field': 'datetime', value: this.endDate() || app.translator.trans('reflar-polls.forum.modal.date_placeholder'), id: 'dtInput', 'data-min': this.getMinDateTime(), readonly: true }), - m('div', { id: 'dtBox' }) + m('input', { style: 'opacity: 1; color: inherit', className: 'FormControl', type: 'text', 'data-field': 'datetime', value: this.endDate() || app.translator.trans('reflar-polls.forum.modal.date_placeholder'), id: 'dtInput', 'data-min': this.getMinDateTime(), readonly: true }) ), m('div', { className: 'clear' }), Switch.component({ @@ -481,25 +472,6 @@ System.register('reflar/polls/components/PollModal', ['flarum/extend', 'flarum/c value: function removeOption(option) { this.answer[option] = ''; } - }, { - key: 'onAdd', - value: function onAdd(pollArray) { - // Add data to DiscussionComposer post data - extend(DiscussionComposer.prototype, 'data', function (data) { - data.poll = pollArray; - }); - extend(DiscussionComposer.prototype, 'headerItems', function (items) { - items.replace('polls', m( - 'a', - { className: 'DiscussionComposer-changePoll', onclick: this.addPoll }, - m( - 'span', - { className: 'PollLabel' }, - app.translator.trans('reflar-polls.forum.composer_discussion.edit') - ) - ), 1); - }); - } }, { key: 'objectSize', value: function objectSize(obj) { @@ -539,7 +511,10 @@ System.register('reflar/polls/components/PollModal', ['flarum/extend', 'flarum/c return; } - this.onAdd(pollArray); + // Add data to DiscussionComposer post data + extend(DiscussionComposer.prototype, 'data', function (data) { + data.poll = pollArray; + }); app.modal.close(); @@ -555,10 +530,10 @@ System.register('reflar/polls/components/PollModal', ['flarum/extend', 'flarum/c });; 'use strict'; -System.register('reflar/polls/components/PollVote', ['flarum/extend', 'flarum/components/Button', 'flarum/Component', 'flarum/utils/classList', 'flarum/components/LogInModal', './ShowVotersModal'], function (_export, _context) { +System.register('reflar/polls/components/PollVote', ['flarum/extend', 'flarum/components/Button', 'flarum/Component', 'flarum/components/LogInModal', './ShowVotersModal'], function (_export, _context) { "use strict"; - var extend, Button, Component, classList, LogInModal, ShowVotersModal, PollVote; + var extend, Button, Component, LogInModal, ShowVotersModal, PollVote; return { setters: [function (_flarumExtend) { extend = _flarumExtend.extend; @@ -566,8 +541,6 @@ System.register('reflar/polls/components/PollVote', ['flarum/extend', 'flarum/co Button = _flarumComponentsButton.default; }, function (_flarumComponent) { Component = _flarumComponent.default; - }, function (_flarumUtilsClassList) { - classList = _flarumUtilsClassList.default; }, function (_flarumComponentsLogInModal) { LogInModal = _flarumComponentsLogInModal.default; }, function (_ShowVotersModal) { @@ -591,8 +564,11 @@ System.register('reflar/polls/components/PollVote', ['flarum/extend', 'flarum/co this.votes = []; this.voted = m.prop(false); this.user = app.session.user; + this.answers = []; - this.answers = this.poll ? this.poll.answers() : []; + this.poll.answers().forEach(function (answer) { + _this2.answers[answer.id()] = answer; + }); if (this.user !== undefined) { if (!this.user.canVote()) { @@ -619,8 +595,30 @@ System.register('reflar/polls/components/PollVote', ['flarum/extend', 'flarum/co app.modal.show(new ShowVotersModal(this.poll)); } }, { - key: 'voteView', - value: function voteView() { + key: 'onError', + value: function onError(el, error) { + el.srcElement.checked = false; + + app.alerts.show(error.alert); + } + }, { + key: 'changeVote', + value: function changeVote(answer, el) { + app.request({ + method: 'PATCH', + url: app.forum.attribute('apiUrl') + '/votes/' + answer, + errorHandler: this.onError.bind(this, el), + data: { + option_id: answer, + poll_id: this.poll.id() + } + }).then(function () { + location.reload(); + }); + } + }, { + key: 'view', + value: function view() { var _this3 = this; if (this.voted() !== false) { @@ -637,7 +635,7 @@ System.register('reflar/polls/components/PollVote', ['flarum/extend', 'flarum/co if (_this3.voted() !== true) { voted = _this3.voted().option_id() === item.data.attributes.id; } - var percent = item.percent(); + var percent = Math.round(item.votes() / _this3.poll.votes().length * 100); return m( 'div', { className: 'PollOption PollVoted' }, @@ -724,7 +722,7 @@ System.register('reflar/polls/components/PollVote', ['flarum/extend', 'flarum/co m( 'label', { className: 'checkbox' }, - m('input', { type: 'checkbox', onchange: _this3.addVote.bind(_this3, item.id()) }), + m('input', { type: 'checkbox', onchange: _this3.addVote.bind(_this3, item) }), m( 'span', null, @@ -757,53 +755,23 @@ System.register('reflar/polls/components/PollVote', ['flarum/extend', 'flarum/co ); } } - }, { - key: 'onError', - value: function onError(el, error) { - el.srcElement.checked = false; - - app.alerts.show(error.alert); - } - }, { - key: 'changeVote', - value: function changeVote(answer, el) { - app.request({ - method: 'PATCH', - url: app.forum.attribute('apiUrl') + '/votes/' + answer, - errorHandler: this.onError.bind(this, el), - data: { - option_id: answer, - poll_id: this.poll.id() - } - }).then(function () { - location.reload(); - }); - } - }, { - key: 'view', - value: function view() { - var content = this.voteView(); - - return m( - 'div', - { className: classList({ - voted: this.voted - }) }, - content - ); - } }, { key: 'addVote', value: function addVote(answer, el) { + var _this4 = this; + if (this.user === undefined) { app.modal.show(new LogInModal()); el.srcElement.checked = false; } else { app.store.createRecord('votes').save({ poll_id: this.poll.id(), - option_id: answer - }).then(function () { - location.reload(); + option_id: answer.id() + }).then(function (vote) { + _this4.answers[answer.id()].data.attributes.votes++; + _this4.voted(vote); + _this4.poll.data.relationships.votes.data.push(vote); + m.redraw(); }); } } @@ -968,20 +936,20 @@ System.register('reflar/polls/main', ['flarum/app', 'flarum/extend', 'flarum/com User.prototype.canSelfEditPolls = Model.attribute('canSelfEditPolls'); User.prototype.canVote = Model.attribute('canVote'); - DiscussionComposer.prototype.addPoll = function () { - app.modal.show(new PollModal()); - }; - // Add button to DiscussionComposer header extend(DiscussionComposer.prototype, 'headerItems', function (items) { if (app.session.user.canStartPolls()) { items.add('polls', m( 'a', - { className: 'DiscussionComposer-poll', onclick: this.addPoll }, - m( + { className: 'DiscussionComposer-poll', onclick: app.modal.show(new PollModal()) }, + this.data.poll ? m( 'span', { className: 'PollLabel' }, app.translator.trans('reflar-polls.forum.composer_discussion.add_poll') + ) : m( + 'span', + { className: 'PollLabel' }, + app.translator.trans('reflar-polls.forum.composer_discussion.edit') ) ), 1); } diff --git a/js/forum/src/components/EditPollModal.js b/js/forum/src/components/EditPollModal.js index 10ef6f8..67d44f4 100644 --- a/js/forum/src/components/EditPollModal.js +++ b/js/forum/src/components/EditPollModal.js @@ -88,7 +88,7 @@ export default class EditPollModal extends Modal { {i + 1 >= 3 ? @@ -130,18 +130,15 @@ export default class EditPollModal extends Modal { } addAnswer(answer) { + var data = { + answer: this.newAnswer(), + poll_id: this.props.poll.id(), + user_id: this.pollCreator.id() + } if (this.answers.length < 10) { - app.request({ - method: 'POST', - url: app.forum.attribute('apiUrl') + '/answers', - data: { - answer: this.newAnswer(), - poll_id: this.props.poll.id(), - user_id: this.pollCreator.id() - } - }).then( - response => { - this.answers.push(response); + app.store.createRecord('answers').save(data).then( + answer => { + this.answers.push(answer); this.newAnswer(''); m.redraw(); @@ -152,6 +149,7 @@ export default class EditPollModal extends Modal { } } + removeOption(option) { app.request({ method: 'DELETE', @@ -183,10 +181,6 @@ export default class EditPollModal extends Modal { }) } - onhide() { - location.reload() - } - updateQuestion(question) { if (question === '') { alert(app.translator.trans('reflar-polls.forum.modal.include_question')) diff --git a/js/forum/src/components/PollModal.js b/js/forum/src/components/PollModal.js index 7e56533..d966e6f 100644 --- a/js/forum/src/components/PollModal.js +++ b/js/forum/src/components/PollModal.js @@ -101,8 +101,7 @@ export default class PollModal extends Modal {
- -
+
{Switch.component({ @@ -136,17 +135,6 @@ export default class PollModal extends Modal { this.answer[option] = ''; } - onAdd(pollArray) { - // Add data to DiscussionComposer post data - extend(DiscussionComposer.prototype, 'data', function (data) { - data.poll = pollArray; - }); - extend(DiscussionComposer.prototype, 'headerItems', function (items) { - items.replace('polls', ( - {app.translator.trans('reflar-polls.forum.composer_discussion.edit')}), 1); - }); - } - objectSize(obj) { var size = 0, key; for (key in obj) { @@ -180,7 +168,10 @@ export default class PollModal extends Modal { return } - this.onAdd(pollArray); + // Add data to DiscussionComposer post data + extend(DiscussionComposer.prototype, 'data', function (data) { + data.poll = pollArray; + }); app.modal.close(); diff --git a/js/forum/src/components/PollVote.js b/js/forum/src/components/PollVote.js index 6be294d..19d2f01 100644 --- a/js/forum/src/components/PollVote.js +++ b/js/forum/src/components/PollVote.js @@ -1,7 +1,6 @@ import {extend} from 'flarum/extend'; import Button from 'flarum/components/Button'; import Component from 'flarum/Component'; -import classList from 'flarum/utils/classList'; import LogInModal from 'flarum/components/LogInModal'; import ShowVotersModal from './ShowVotersModal'; @@ -12,8 +11,11 @@ export default class PollVote extends Component { this.votes = []; this.voted = m.prop(false); this.user = app.session.user; + this.answers = [] - this.answers = this.poll ? this.poll.answers() : []; + this.poll.answers().forEach(answer => { + this.answers[answer.id()] = answer; + }) if (this.user !== undefined) { if (!this.user.canVote()) { @@ -40,7 +42,27 @@ export default class PollVote extends Component { app.modal.show(new ShowVotersModal(this.poll)) } - voteView() { + onError(el, error) { + el.srcElement.checked = false + + app.alerts.show(error.alert) + } + + changeVote(answer, el) { + app.request({ + method: 'PATCH', + url: app.forum.attribute('apiUrl') + '/votes/' + answer, + errorHandler: this.onError.bind(this, el), + data: { + option_id: answer, + poll_id: this.poll.id() + } + }).then(() => { + location.reload() + }); + } + + view() { if (this.voted() !== false) { return ( @@ -51,7 +73,7 @@ export default class PollVote extends Component { if (this.voted() !== true) { voted = this.voted().option_id() === item.data.attributes.id; } - const percent = item.percent(); + let percent = Math.round((item.votes() / this.poll.votes().length) * 100) return (
@@ -142,37 +164,6 @@ export default class PollVote extends Component { } } - onError(el, error) { - el.srcElement.checked = false - - app.alerts.show(error.alert) - } - - changeVote(answer, el) { - app.request({ - method: 'PATCH', - url: app.forum.attribute('apiUrl') + '/votes/' + answer, - errorHandler: this.onError.bind(this, el), - data: { - option_id: answer, - poll_id: this.poll.id() - } - }).then(() => { - location.reload() - }); - } - - view() { - let content = this.voteView(); - - return ( -
- {content} -
- ); - } addVote(answer, el) { if (this.user === undefined) { @@ -181,10 +172,14 @@ export default class PollVote extends Component { } else { app.store.createRecord('votes').save({ poll_id: this.poll.id(), - option_id: answer - }).then(() => { - location.reload() - }) + option_id: answer.id() + }).then( + vote => { + this.answers[answer.id()].data.attributes.votes++; + this.voted(vote); + this.poll.data.relationships.votes.data.push(vote) + m.redraw() + }) } } } diff --git a/js/forum/src/main.js b/js/forum/src/main.js index cd4d4eb..785b065 100644 --- a/js/forum/src/main.js +++ b/js/forum/src/main.js @@ -28,15 +28,18 @@ app.initializers.add('reflar-polls', app => { User.prototype.canSelfEditPolls = Model.attribute('canSelfEditPolls'); User.prototype.canVote = Model.attribute('canVote'); - DiscussionComposer.prototype.addPoll = function () { - app.modal.show(new PollModal()); - }; - // Add button to DiscussionComposer header extend(DiscussionComposer.prototype, 'headerItems', function (items) { if (app.session.user.canStartPolls()) { items.add('polls', ( - {app.translator.trans('reflar-polls.forum.composer_discussion.add_poll')}), 1); + + {this.data.poll + ? + {app.translator.trans('reflar-polls.forum.composer_discussion.add_poll')} + : + {app.translator.trans('reflar-polls.forum.composer_discussion.edit')}} + + ), 1); } }); From a4ccce422a1dfd5f08434fc1fa32936d5a1b3a11 Mon Sep 17 00:00:00 2001 From: Charlie K Date: Sun, 6 May 2018 20:15:16 -0700 Subject: [PATCH 2/3] Remove page refreshes, Closes #31 --- js/forum/dist/extension.js | 84 ++++++++++++++----- js/forum/src/components/EditPollModal.js | 8 ++ js/forum/src/components/PollModal.js | 3 +- js/forum/src/components/PollVote.js | 35 ++++++-- js/forum/src/main.js | 18 +++- .../Controllers/CreateAnswerController.php | 2 +- src/Api/Controllers/UpdateVoteController.php | 9 +- 7 files changed, 119 insertions(+), 40 deletions(-) diff --git a/js/forum/dist/extension.js b/js/forum/dist/extension.js index 31722ea..2098371 100644 --- a/js/forum/dist/extension.js +++ b/js/forum/dist/extension.js @@ -216,6 +216,14 @@ System.register('reflar/polls/components/EditPollModal', ['flarum/extend', 'flar ) )]; } + }, { + key: 'onhide', + value: function onhide() { + this.props.poll.answers = m.prop(this.answers); + this.props.poll.question = this.question; + this.props.poll.endDate = this.endDate; + m.redraw.strategy('all'); + } }, { key: 'addAnswer', value: function addAnswer(answer) { @@ -418,7 +426,8 @@ System.register('reflar/polls/components/PollModal', ['flarum/extend', 'flarum/c type: 'text', name: 'answer' + (i + 1), bidi: _this3.answer[i + 1], - placeholder: app.translator.trans('reflar-polls.forum.modal.answer_placeholder') + ' #' + (i + 1) }) + placeholder: app.translator.trans('reflar-polls.forum.modal.answer_placeholder') + ' #' + (i + 1) }), + m('div', { id: 'dtBox' }) ), i + 1 >= 3 ? Button.component({ type: 'button', @@ -604,22 +613,40 @@ System.register('reflar/polls/components/PollVote', ['flarum/extend', 'flarum/co }, { key: 'changeVote', value: function changeVote(answer, el) { + var _this3 = this; + + var oldVoteId = this.voted().id(); + var oldAnswerId = this.voted().option_id(); app.request({ method: 'PATCH', - url: app.forum.attribute('apiUrl') + '/votes/' + answer, + url: app.forum.attribute('apiUrl') + '/votes/' + answer.id(), errorHandler: this.onError.bind(this, el), data: { - option_id: answer, + option_id: answer.id(), poll_id: this.poll.id() } - }).then(function () { - location.reload(); + }).then(function (response) { + _this3.answers[answer.id()].data.attributes.votes++; + _this3.answers[oldAnswerId].data.attributes.votes--; + _this3.poll.data.relationships.votes.data.some(function (vote) { + if (typeof vote.id === "function") { + var id = vote.id(); + } else { + var id = vote.id; + } + if (oldVoteId === parseInt(id)) { + vote.option_id = m.prop(response.data.attributes.option_id); + return true; + } + }); + m.redraw.strategy('all'); + m.redraw(); }); } }, { key: 'view', value: function view() { - var _this3 = this; + var _this4 = this; if (this.voted() !== false) { return m( @@ -632,10 +659,11 @@ System.register('reflar/polls/components/PollVote', ['flarum/extend', 'flarum/co ), this.answers.map(function (item) { var voted = false; - if (_this3.voted() !== true) { - voted = _this3.voted().option_id() === item.data.attributes.id; + if (_this4.voted() !== true) { + voted = parseInt(_this4.voted().option_id()) === item.data.attributes.id; + m.redraw(); } - var percent = Math.round(item.votes() / _this3.poll.votes().length * 100); + var percent = Math.round(item.votes() / _this4.poll.votes().length * 100); return m( 'div', { className: 'PollOption PollVoted' }, @@ -648,16 +676,16 @@ System.register('reflar/polls/components/PollVote', ['flarum/extend', 'flarum/co config: function config(element) { $(element).tooltip({ placement: 'right' }); } }, - !_this3.poll.isEnded() && _this3.voted !== true ? m( + !_this4.poll.isEnded() && _this4.voted !== true ? m( 'label', { className: 'checkbox' }, - voted ? m('input', { onchange: _this3.changeVote.bind(_this3, item.id()), type: 'checkbox', checked: true }) : m('input', { onchange: _this3.changeVote.bind(_this3, item.id()), type: 'checkbox' }), + voted ? m('input', { onchange: _this4.changeVote.bind(_this4, item), type: 'checkbox', checked: true }) : m('input', { onchange: _this4.changeVote.bind(_this4, item), type: 'checkbox' }), m('span', { className: 'checkmark' }) ) : '', m('div', { style: '--width: ' + percent + '%', className: 'PollOption-active' }), m( 'label', - { style: !_this3.poll.isEnded() ? "margin-left: 25px" : '', className: 'PollAnswer' }, + { style: !_this4.poll.isEnded() ? "margin-left: 25px" : '', className: 'PollAnswer' }, m( 'span', null, @@ -682,7 +710,7 @@ System.register('reflar/polls/components/PollVote', ['flarum/extend', 'flarum/co className: 'Button Button--primary PublicPollButton', children: app.translator.trans('reflar-polls.forum.public_poll'), onclick: function onclick() { - app.modal.show(new ShowVotersModal(_this3.poll)); + app.modal.show(new ShowVotersModal(_this4.poll)); } }) : '', m('div', { className: 'clear' }), @@ -722,7 +750,7 @@ System.register('reflar/polls/components/PollVote', ['flarum/extend', 'flarum/co m( 'label', { className: 'checkbox' }, - m('input', { type: 'checkbox', onchange: _this3.addVote.bind(_this3, item) }), + m('input', { type: 'checkbox', onchange: _this4.addVote.bind(_this4, item) }), m( 'span', null, @@ -738,7 +766,7 @@ System.register('reflar/polls/components/PollVote', ['flarum/extend', 'flarum/co className: 'Button Button--primary PublicPollButton', children: app.translator.trans('reflar-polls.forum.public_poll'), onclick: function onclick() { - app.modal.show(new ShowVotersModal(_this3.poll)); + app.modal.show(new ShowVotersModal(_this4.poll)); } }) : '', this.poll.isEnded() ? m( @@ -758,7 +786,7 @@ System.register('reflar/polls/components/PollVote', ['flarum/extend', 'flarum/co }, { key: 'addVote', value: function addVote(answer, el) { - var _this4 = this; + var _this5 = this; if (this.user === undefined) { app.modal.show(new LogInModal()); @@ -768,9 +796,9 @@ System.register('reflar/polls/components/PollVote', ['flarum/extend', 'flarum/co poll_id: this.poll.id(), option_id: answer.id() }).then(function (vote) { - _this4.answers[answer.id()].data.attributes.votes++; - _this4.voted(vote); - _this4.poll.data.relationships.votes.data.push(vote); + _this5.answers[answer.id()].data.attributes.votes++; + _this5.voted(vote); + _this5.poll.data.relationships.votes.data.push(vote); m.redraw(); }); } @@ -936,25 +964,35 @@ System.register('reflar/polls/main', ['flarum/app', 'flarum/extend', 'flarum/com User.prototype.canSelfEditPolls = Model.attribute('canSelfEditPolls'); User.prototype.canVote = Model.attribute('canVote'); + DiscussionComposer.prototype.addPoll = function () { + app.modal.show(new PollModal()); + }; + // Add button to DiscussionComposer header extend(DiscussionComposer.prototype, 'headerItems', function (items) { if (app.session.user.canStartPolls()) { items.add('polls', m( 'a', - { className: 'DiscussionComposer-poll', onclick: app.modal.show(new PollModal()) }, - this.data.poll ? m( + { className: 'DiscussionComposer-poll', onclick: this.addPoll.bind(this) }, + this.data().poll ? m( 'span', { className: 'PollLabel' }, - app.translator.trans('reflar-polls.forum.composer_discussion.add_poll') + app.translator.trans('reflar-polls.forum.composer_discussion.edit') ) : m( 'span', { className: 'PollLabel' }, - app.translator.trans('reflar-polls.forum.composer_discussion.edit') + app.translator.trans('reflar-polls.forum.composer_discussion.add_poll') ) ), 1); } }); + extend(DiscussionComposer.prototype, 'onsubmit', function () { + extend(DiscussionComposer.prototype, 'data', function (data) { + data.poll = undefined; + }); + }); + addPollBadege(); PollDiscussion(); PollControl(); diff --git a/js/forum/src/components/EditPollModal.js b/js/forum/src/components/EditPollModal.js index 67d44f4..99c5abd 100644 --- a/js/forum/src/components/EditPollModal.js +++ b/js/forum/src/components/EditPollModal.js @@ -129,6 +129,14 @@ export default class EditPollModal extends Modal { ]; } + + onhide() { + this.props.poll.answers = m.prop(this.answers) + this.props.poll.question = this.question + this.props.poll.endDate = this.endDate + m.redraw.strategy('all') + } + addAnswer(answer) { var data = { answer: this.newAnswer(), diff --git a/js/forum/src/components/PollModal.js b/js/forum/src/components/PollModal.js index d966e6f..7ef1a39 100644 --- a/js/forum/src/components/PollModal.js +++ b/js/forum/src/components/PollModal.js @@ -80,7 +80,8 @@ export default class PollModal extends Modal { name={'answer' + (i + 1)} bidi={this.answer[i + 1]} placeholder={app.translator.trans('reflar-polls.forum.modal.answer_placeholder') + ' #' + (i + 1)}/> - +
+ {i + 1 >= 3 ? Button.component({ type: 'button', diff --git a/js/forum/src/components/PollVote.js b/js/forum/src/components/PollVote.js index 19d2f01..a12a197 100644 --- a/js/forum/src/components/PollVote.js +++ b/js/forum/src/components/PollVote.js @@ -49,17 +49,35 @@ export default class PollVote extends Component { } changeVote(answer, el) { + var oldVoteId = this.voted().id() + var oldAnswerId = this.voted().option_id() app.request({ method: 'PATCH', - url: app.forum.attribute('apiUrl') + '/votes/' + answer, + url: app.forum.attribute('apiUrl') + '/votes/' + answer.id(), errorHandler: this.onError.bind(this, el), data: { - option_id: answer, + option_id: answer.id(), poll_id: this.poll.id() } - }).then(() => { - location.reload() - }); + }).then( + response => { + this.answers[answer.id()].data.attributes.votes++; + this.answers[oldAnswerId].data.attributes.votes--; + this.poll.data.relationships.votes.data.some(vote => { + if (typeof vote.id === "function") { + var id = vote.id() + } else { + var id = vote.id + } + if (oldVoteId === parseInt(id)) { + vote.option_id = m.prop(response.data.attributes.option_id); + return true; + } + }) + m.redraw.strategy('all') + m.redraw() + } + ) } view() { @@ -71,7 +89,8 @@ export default class PollVote extends Component { {this.answers.map((item) => { let voted = false; if (this.voted() !== true) { - voted = this.voted().option_id() === item.data.attributes.id; + voted = parseInt(this.voted().option_id()) === item.data.attributes.id; + m.redraw() } let percent = Math.round((item.votes() / this.poll.votes().length) * 100) return ( @@ -88,9 +107,9 @@ export default class PollVote extends Component { {!this.poll.isEnded() && this.voted !== true ? diff --git a/js/forum/src/main.js b/js/forum/src/main.js index 785b065..c1be9d7 100644 --- a/js/forum/src/main.js +++ b/js/forum/src/main.js @@ -28,21 +28,31 @@ app.initializers.add('reflar-polls', app => { User.prototype.canSelfEditPolls = Model.attribute('canSelfEditPolls'); User.prototype.canVote = Model.attribute('canVote'); + DiscussionComposer.prototype.addPoll = function() { + app.modal.show(new PollModal()); + }; + // Add button to DiscussionComposer header extend(DiscussionComposer.prototype, 'headerItems', function (items) { if (app.session.user.canStartPolls()) { items.add('polls', ( - - {this.data.poll + + {this.data().poll ? - {app.translator.trans('reflar-polls.forum.composer_discussion.add_poll')} + {app.translator.trans('reflar-polls.forum.composer_discussion.edit')} : - {app.translator.trans('reflar-polls.forum.composer_discussion.edit')}} + {app.translator.trans('reflar-polls.forum.composer_discussion.add_poll')}} ), 1); } }); + extend(DiscussionComposer.prototype, 'onsubmit', function() { + extend(DiscussionComposer.prototype, 'data', function (data) { + data.poll = undefined; + }); + }) + addPollBadege(); PollDiscussion(); PollControl(); diff --git a/src/Api/Controllers/CreateAnswerController.php b/src/Api/Controllers/CreateAnswerController.php index a1dee84..3fc2b7f 100644 --- a/src/Api/Controllers/CreateAnswerController.php +++ b/src/Api/Controllers/CreateAnswerController.php @@ -47,7 +47,7 @@ public function __construct(AnswerValidator $validator) protected function data(ServerRequestInterface $request, Document $document) { $actor = $request->getAttribute('actor'); - $data = $request->getParsedBody(); + $data = array_get($request->getParsedBody(), 'data.attributes', []); $poll = Question::find($data['poll_id']); $answer = Answer::build($data['answer']); diff --git a/src/Api/Controllers/UpdateVoteController.php b/src/Api/Controllers/UpdateVoteController.php index 4b563e4..d6f4af1 100644 --- a/src/Api/Controllers/UpdateVoteController.php +++ b/src/Api/Controllers/UpdateVoteController.php @@ -17,6 +17,7 @@ use Flarum\Core\Access\AssertPermissionTrait; use Flarum\Core\Exception\FloodingException; use Flarum\Core\Exception\PermissionDeniedException; +use Flarum\core\User; use Psr\Http\Message\ServerRequestInterface; use Reflar\Polls\Api\Serializers\VoteSerializer; use Reflar\Polls\Question; @@ -51,13 +52,15 @@ protected function data(ServerRequestInterface $request, Document $document) $this->assertNotFlooding($actor); - Vote::where('user_id', $actor->id)->delete(); - if (Question::find($attributes['poll_id'])->isEnded()) { throw new PermissionDeniedException(); } - $vote = Vote::build($attributes['poll_id'], $actor->id, $attributes['option_id']); + $vote = Vote::where('user_id', $actor->id) + ->where('poll_id', $attributes['poll_id']) + ->first(); + + $vote->option_id = $attributes['option_id']; $actor->last_vote_time = new DateTime(); $actor->save(); From f02ed82a63fa2fd484f7bcbe50c4abd3f8853f51 Mon Sep 17 00:00:00 2001 From: Charlie K Date: Sun, 6 May 2018 20:51:37 -0700 Subject: [PATCH 3/3] Style --- js/forum/src/components/PollVote.js | 1 - src/Api/Controllers/UpdateVoteController.php | 1 - 2 files changed, 2 deletions(-) diff --git a/js/forum/src/components/PollVote.js b/js/forum/src/components/PollVote.js index a12a197..ea3c1fe 100644 --- a/js/forum/src/components/PollVote.js +++ b/js/forum/src/components/PollVote.js @@ -183,7 +183,6 @@ export default class PollVote extends Component { } } - addVote(answer, el) { if (this.user === undefined) { app.modal.show(new LogInModal()) diff --git a/src/Api/Controllers/UpdateVoteController.php b/src/Api/Controllers/UpdateVoteController.php index d6f4af1..f191034 100644 --- a/src/Api/Controllers/UpdateVoteController.php +++ b/src/Api/Controllers/UpdateVoteController.php @@ -17,7 +17,6 @@ use Flarum\Core\Access\AssertPermissionTrait; use Flarum\Core\Exception\FloodingException; use Flarum\Core\Exception\PermissionDeniedException; -use Flarum\core\User; use Psr\Http\Message\ServerRequestInterface; use Reflar\Polls\Api\Serializers\VoteSerializer; use Reflar\Polls\Question;