Skip to content
This repository has been archived by the owner on Jul 4, 2019. It is now read-only.

Commit

Permalink
WIP 1.3.0 (#32)
Browse files Browse the repository at this point in the history
1.2.1
  • Loading branch information
Charlie committed May 7, 2018
1 parent 03e70dd commit 3179b19
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 176 deletions.
196 changes: 101 additions & 95 deletions js/forum/dist/extension.js

Large diffs are not rendered by default.

34 changes: 18 additions & 16 deletions js/forum/src/components/EditPollModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export default class EditPollModal extends Modal {
<input className="FormControl"
type="text"
oninput={m.withAttr('value', this.updateAnswer.bind(this, answer))}
value={answer.data.attributes.answer}
value={answer.answer()}
placeholder={app.translator.trans('reflar-polls.forum.modal.answer_placeholder') + ' #' + (i + 1)}/>
</fieldset>
{i + 1 >= 3 ?
Expand Down Expand Up @@ -129,19 +129,24 @@ 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(),
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();
Expand All @@ -152,6 +157,7 @@ export default class EditPollModal extends Modal {
}
}


removeOption(option) {
app.request({
method: 'DELETE',
Expand Down Expand Up @@ -183,10 +189,6 @@ export default class EditPollModal extends Modal {
})
}

onhide() {
location.reload()
}

updateQuestion(question) {
if (question === '') {
alert(app.translator.trans('reflar-polls.forum.modal.include_question'))
Expand Down
22 changes: 7 additions & 15 deletions js/forum/src/components/PollModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}/>
</fieldset>
<div id="dtBox"></div>
</fieldset>
{i + 1 >= 3 ?
Button.component({
type: 'button',
Expand All @@ -101,8 +102,7 @@ export default class PollModal extends Modal {

<div className='Form-group'>
<fieldset style="margin-bottom: 15px" className="Poll-answer-input">
<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/>
<div id="dtBox"></div>
<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/>
</fieldset>
<div className="clear"></div>
{Switch.component({
Expand Down Expand Up @@ -136,17 +136,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', (
<a className="DiscussionComposer-changePoll" onclick={this.addPoll}><span className="PollLabel">{app.translator.trans('reflar-polls.forum.composer_discussion.edit')}</span></a>), 1);
});
}

objectSize(obj) {
var size = 0, key;
for (key in obj) {
Expand Down Expand Up @@ -180,7 +169,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();

Expand Down
101 changes: 57 additions & 44 deletions js/forum/src/components/PollVote.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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()) {
Expand All @@ -40,7 +42,45 @@ 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) {
var oldVoteId = this.voted().id()
var oldAnswerId = this.voted().option_id()
app.request({
method: 'PATCH',
url: app.forum.attribute('apiUrl') + '/votes/' + answer.id(),
errorHandler: this.onError.bind(this, el),
data: {
option_id: answer.id(),
poll_id: this.poll.id()
}
}).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() {

if (this.voted() !== false) {
return (
Expand All @@ -49,9 +89,10 @@ 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()
}
const percent = item.percent();
let percent = Math.round((item.votes() / this.poll.votes().length) * 100)
return (
<div className='PollOption PollVoted'>
<div
Expand All @@ -66,9 +107,9 @@ export default class PollVote extends Component {
{!this.poll.isEnded() && this.voted !== true ?
<label className="checkbox">
{voted ?
<input onchange={this.changeVote.bind(this, item.id())} type="checkbox" checked/>
<input onchange={this.changeVote.bind(this, item)} type="checkbox" checked/>
:
<input onchange={this.changeVote.bind(this, item.id())} type="checkbox"/>
<input onchange={this.changeVote.bind(this, item)} type="checkbox"/>
}
<span className="checkmark"/>
</label>
Expand Down Expand Up @@ -113,7 +154,7 @@ export default class PollVote extends Component {
<div className="PollOption">
<div className='PollBar'>
<label className="checkbox">
<input type="checkbox" onchange={this.addVote.bind(this, item.id())}/>
<input type="checkbox" onchange={this.addVote.bind(this, item)}/>
<span>{item.answer()}</span>
<span className="checkmark"/>
</label>
Expand Down Expand Up @@ -142,49 +183,21 @@ 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 (
<div className={classList({
voted: this.voted
})}>
{content}
</div>
);
}

addVote(answer, el) {
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(() => {
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()
})
}
}
}
17 changes: 15 additions & 2 deletions js/forum/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,31 @@ app.initializers.add('reflar-polls', app => {
User.prototype.canSelfEditPolls = Model.attribute('canSelfEditPolls');
User.prototype.canVote = Model.attribute('canVote');

DiscussionComposer.prototype.addPoll = function () {
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', (
<a className="DiscussionComposer-poll" onclick={this.addPoll}><span className="PollLabel">{app.translator.trans('reflar-polls.forum.composer_discussion.add_poll')}</span></a>), 1);
<a className="DiscussionComposer-poll" onclick={this.addPoll.bind(this)}>
{this.data().poll
?
<span className="PollLabel">{app.translator.trans('reflar-polls.forum.composer_discussion.edit')}</span>
:
<span className="PollLabel">{app.translator.trans('reflar-polls.forum.composer_discussion.add_poll')}</span>}

</a>), 1);
}
});

extend(DiscussionComposer.prototype, 'onsubmit', function() {
extend(DiscussionComposer.prototype, 'data', function (data) {
data.poll = undefined;
});
})

addPollBadege();
PollDiscussion();
PollControl();
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Controllers/CreateAnswerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);

Expand Down
8 changes: 5 additions & 3 deletions src/Api/Controllers/UpdateVoteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,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();
Expand Down

0 comments on commit 3179b19

Please sign in to comment.