Skip to content

Commit

Permalink
chore: fix some ts types (#101)
Browse files Browse the repository at this point in the history
* fix some ts types

* another one

* chore: format
  • Loading branch information
imorland authored Oct 23, 2024
1 parent c2db9fa commit e676131
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 30 deletions.
1 change: 1 addition & 0 deletions js/src/forum/components/PollView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default class PollView extends Component<PollAttrs, PollState> {
view(): Mithril.Children {
const poll = this.attrs.poll;
const state = this.state;
// @ts-expect-error
const controls = PollControls.controls(poll, this);

(poll.publicPoll() || poll.canEdit()) &&
Expand Down
66 changes: 39 additions & 27 deletions js/src/forum/components/PostPoll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ import PollImage from './Poll/PollImage';
export interface PostPollAttrs extends ComponentAttrs {
poll: Poll;
post?: Post;
tooltipVisible?: boolean;
}

export default class PostPoll extends Component<PostPollAttrs> {
loadingOptions: boolean = false;
useSubmitUI!: boolean;
pendingSubmit: boolean = false;
pendingOptions: any;
updateVisibility: () => void = () => {};

oninit(vnode: Mithril.Vnode<PostPollAttrs, this>) {
super.oninit(vnode);
Expand Down Expand Up @@ -206,11 +208,13 @@ export default class PostPoll extends Component<PostPollAttrs> {
changeVote(option: PollOption, evt: Event) {
if (!app.session.user) {
app.modal.show(LogInModal);
evt.target.checked = false;
if (evt.target instanceof HTMLInputElement) {
evt.target.checked = false;
}
return;
}

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

Expand All @@ -228,42 +232,49 @@ export default class PostPoll extends Component<PostPollAttrs> {
return;
}

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

onsubmit() {
return this.submit(this.pendingOptions, () => {
this.pendingOptions = null;
this.pendingSubmit = false;
});
return this.submit(
this.pendingOptions,
() => {
this.pendingOptions = null;
this.pendingSubmit = false;
},
null
);
}

submit(optionIds, cb, onerror) {
async submit(optionIds: Iterable<unknown> | ArrayLike<unknown>, cb: { (): void; (): void } | null, onerror: any) {
this.loadingOptions = true;
m.redraw();

return app
.request({
method: 'PATCH',
url: `${app.forum.attribute('apiUrl')}/fof/polls/${this.attrs.poll.id()}/votes`,
body: {
data: {
optionIds: Array.from(optionIds),
try {
try {
const res = await app.request({
method: 'PATCH',
url: `${app.forum.attribute<Poll>('apiUrl')}/fof/polls/${this.attrs.poll.id()}/votes`,
body: {
data: {
optionIds: Array.from(optionIds),
},
},
},
})
.then((res) => {
app.store.pushPayload(res);
});
app.store.pushPayload(res as any);
cb?.();
})
.catch((err) => {
} catch (err) {
onerror?.(err);
})
.finally(() => {
this.loadingOptions = false;
}
} finally {
this.loadingOptions = false;

m.redraw();
});
m.redraw();
}
}

showVoters() {
Expand Down Expand Up @@ -295,10 +306,11 @@ export default class PostPoll extends Component<PostPollAttrs> {
/**
* Alert before navigating away using browser's 'beforeunload' event
*/
preventClose(e) {
preventClose(e: Event) {
if (this.pendingOptions) {
e.preventDefault();
return true;
}
return undefined;
}
}
4 changes: 3 additions & 1 deletion js/src/forum/components/UploadPollImageButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default class UploadPollImageButton extends Button<UploadPollImageButtonA
fileName: string | undefined = undefined;
$input: JQuery<HTMLElement> | undefined;

// @ts-expect-error
view(vnode: Mithril.Vnode<UploadPollImageButtonAttrs>) {
this.attrs.loading = this.loading;
this.attrs.className = classList(this.attrs.className, 'Button', 'Button--inverted');
Expand Down Expand Up @@ -70,6 +71,7 @@ export default class UploadPollImageButton extends Button<UploadPollImageButtonA
.trigger('click')
.on('change', (e) => {
const body = new FormData();
// @ts-expect-error
body.append(this.attrs.name, $(e.target)[0].files[0]);

this.loading = true;
Expand All @@ -79,7 +81,7 @@ export default class UploadPollImageButton extends Button<UploadPollImageButtonA
.request<PollUploadObject>({
method: 'POST',
url: this.resourceUrl(),
serialize: (raw) => raw,
serialize: (raw: any) => raw,
body,
})
.then(this.success.bind(this), this.failure.bind(this));
Expand Down
5 changes: 3 additions & 2 deletions js/src/forum/utils/PollControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ export default {
controls(poll: Poll, context: Component): ItemList<Mithril.Children> {
const items = new ItemList<Mithril.Children>();

['poll', 'moderation', 'destructive'].forEach((section) => {
const controls = (this[section + 'Controls'](poll, context) as ItemList<Mithril.Children>).toArray();
const sections: ('poll' | 'moderation' | 'destructive')[] = ['poll', 'moderation', 'destructive'];
sections.forEach((section) => {
const controls = (this[`${section}Controls`](poll, context) as ItemList<Mithril.Children>).toArray();
if (controls.length) {
controls.forEach((item) => items.add(item.itemName, item));
items.add(section + 'Separator', <Separator />);
Expand Down

0 comments on commit e676131

Please sign in to comment.