Skip to content

Commit

Permalink
Refactor Poll related components and methods for better readability a…
Browse files Browse the repository at this point in the history
…nd error handling
  • Loading branch information
novacuum committed Feb 19, 2024
1 parent 13001fe commit 2b204a7
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 194 deletions.
84 changes: 37 additions & 47 deletions js/dist/forum.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion js/dist/forum.js.map

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions js/src/forum/components/Poll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ export default class Poll extends Component<PollAttrs, PollState> {
const state = this.state;
const controls = PollControls.controls(poll, this);

controls.add('view', (<Button onclick={state.showVoters} icon="fas fa-poll">{t('fof-polls.forum.public_poll')}</Button>));

controls.add(
'view',
<Button onclick={state.showVoters} icon="fas fa-poll">
{t('fof-polls.forum.public_poll')}
</Button>
);

return (
<div className="Poll" data-id={poll.id()}>
Expand Down
5 changes: 3 additions & 2 deletions js/src/forum/models/Poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ export default class Poll extends Model {
return Model.hasMany<PollVote>('votes').call(this);
}

myVotes() {
return Model.hasMany<PollVote>('myVotes').call(this);
myVotes(): PollVote[] {
const myVotes = Model.hasMany<PollVote>('myVotes').call(this);
return myVotes ? (myVotes as PollVote[]) : [];
}

isGlobal() {
Expand Down
135 changes: 0 additions & 135 deletions js/src/forum/states/PollDirectoryState.ts

This file was deleted.

15 changes: 8 additions & 7 deletions js/src/forum/states/PollState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ export default class PollState {
public loadingOptions: boolean;
public useSubmitUI: boolean;
public showCheckMarks: boolean;
public boundChangeVote: (option: PollOption, evt: Event) => void;

constructor(poll: Poll) {
this.poll = poll;
this.pendingSubmit = false;
this.pendingOptions = null;
this.loadingOptions = false;
this.useSubmitUI = !this.poll?.canChangeVote() && this.poll?.allowMultipleVotes();
this.useSubmitUI = !poll?.canChangeVote() && poll?.allowMultipleVotes();
this.showCheckMarks = !app.session.user || (!poll.hasEnded() && poll.canVote() && (!this.hasVoted() || poll.canChangeVote()));
}

Expand All @@ -31,15 +30,17 @@ export default class PollState {
return this.useSubmitUI && this.pendingSubmit;
}

changeVote(option: PollOption, evt) {
changeVote(option: PollOption, evt: Event) {
const target = evt.target as HTMLInputElement;

if (!app.session.user) {
app.modal.show(LogInModal);
evt.target.checked = false;
target.checked = false;
return;
}

const optionIds = this.pendingOptions || new Set(this.poll.myVotes().map?.((v: PollVote) => v.option().id()));
const isUnvoting = optionIds.delete(option.id());
const optionIds = this.pendingOptions || new Set(this.poll.myVotes().map((v: PollVote) => v.option().id()));
const isUnvoting = optionIds.delete(option.id()!);
const allowsMultiple = this.poll.allowMultipleVotes();

if (!allowsMultiple) {
Expand All @@ -56,7 +57,7 @@ export default class PollState {
return;
}

return this.submit(optionIds, null, () => (evt.target.checked = isUnvoting));
return this.submit(optionIds, null, () => (target.checked = isUnvoting));
}

onsubmit() {
Expand Down

0 comments on commit 2b204a7

Please sign in to comment.