Skip to content

Commit

Permalink
chor: transform ListVotersModal to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
novacuum committed Feb 28, 2024
1 parent 7642ecb commit e6ffb9f
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 64 deletions.
62 changes: 0 additions & 62 deletions js/src/forum/components/ListVotersModal.js

This file was deleted.

71 changes: 71 additions & 0 deletions js/src/forum/components/ListVotersModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import type Mithril from 'mithril';
import app from 'flarum/forum/app';
import Modal, { IInternalModalAttrs } from 'flarum/common/components/Modal';
import avatar from 'flarum/common/helpers/avatar';
import username from 'flarum/common/helpers/username';
import Link from 'flarum/common/components/Link';
import LoadingIndicator from 'flarum/common/components/LoadingIndicator';
import PollModel from '../models/Poll';
import PollVote from '../models/PollVote';
import User from 'flarum/common/models/User';
import PollOption from '../models/PollOption';

interface ListVotersModalAttrs extends IInternalModalAttrs {
poll: PollModel;
onsubmit: (data: object) => Promise<void>;
}

export default class ListVotersModal extends Modal<ListVotersModalAttrs> {
oninit(vnode: Mithril.Vnode): void {
super.oninit(vnode);

this.loading = true;

app.store
.find('fof/polls', this.attrs.poll.id()!, {
include: 'votes,votes.user,votes.option',
})
.then(() => (this.loading = false))
.finally(() => m.redraw());
}

className(): string {
return 'Modal--medium VotesModal';
}

title(): Mithril.Children {
return app.translator.trans('fof-polls.forum.votes_modal.title');
}

content(): Mithril.Children {
const options = this.attrs.poll.options() as PollOption[];
return <div className="Modal-body">{this.loading ? <LoadingIndicator /> : options.map(this.optionContent.bind(this))}</div>;
}

optionContent(opt: PollOption): Mithril.Children {
const votes = (this.attrs.poll.votes() || []).filter((v) => opt.id() === v!.option()!.id()) as PollVote[];

return (
<div className="VotesModal-option">
<h2>{opt.answer()! + ':'}</h2>

{votes.length ? (
<div className="VotesModal-list">{votes.map(this.voteContent.bind(this))}</div>
) : (
<h4>{app.translator.trans('fof-polls.forum.modal.no_voters')}</h4>
)}
</div>
);
}

voteContent(vote: PollVote): Mithril.Children {
const user = vote.user() as User;
const attrs = user && { href: app.route.user(user) };

return (
<Link {...attrs}>
{avatar(user)} {username(user)}
</Link>
);
}
}
1 change: 0 additions & 1 deletion js/src/forum/components/form/FormError.js

This file was deleted.

7 changes: 7 additions & 0 deletions js/src/forum/components/form/FormError.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { NestedStringArray } from '@askvortsov/rich-icu-message-formatter';

export default class FormError extends Error {
constructor(props: NestedStringArray | string) {
super(props + '');
}
}
3 changes: 2 additions & 1 deletion js/src/forum/models/PollVote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export default class PollVote extends Model {
}

option() {
return Model.hasOne<PollOption>('option').call(this);
const result = Model.hasOne<PollOption>('option').call(this);
return result === false ? null : result;
}

user() {
Expand Down

0 comments on commit e6ffb9f

Please sign in to comment.