use Poll model with relations API in poll rendering (#9877)
* wip * remove dupe * use poll model relations in all cases * update mpollbody tests to use poll instance * update poll fetching login in pinned messages card * add pinned polls to room polls state * add spinner while relations are still loading * handle no poll in end poll dialog * strict errors * strict fix * more strict fix
This commit is contained in:
parent
b45b933a65
commit
544baa30ed
9 changed files with 350 additions and 670 deletions
|
@ -35,26 +35,34 @@ interface IProps extends IDialogProps {
|
|||
}
|
||||
|
||||
export default class EndPollDialog extends React.Component<IProps> {
|
||||
private onFinished = (endPoll: boolean): void => {
|
||||
const topAnswer = findTopAnswer(this.props.event, this.props.matrixClient, this.props.getRelationsForEvent);
|
||||
|
||||
const message =
|
||||
topAnswer === ""
|
||||
? _t("The poll has ended. No votes were cast.")
|
||||
: _t("The poll has ended. Top answer: %(topAnswer)s", { topAnswer });
|
||||
|
||||
private onFinished = async (endPoll: boolean): Promise<void> => {
|
||||
if (endPoll) {
|
||||
const endEvent = PollEndEvent.from(this.props.event.getId(), message).serialize();
|
||||
const room = this.props.matrixClient.getRoom(this.props.event.getRoomId());
|
||||
const poll = room?.polls.get(this.props.event.getId()!);
|
||||
|
||||
this.props.matrixClient
|
||||
.sendEvent(this.props.event.getRoomId(), endEvent.type, endEvent.content)
|
||||
.catch((e: any) => {
|
||||
console.error("Failed to submit poll response event:", e);
|
||||
Modal.createDialog(ErrorDialog, {
|
||||
title: _t("Failed to end poll"),
|
||||
description: _t("Sorry, the poll did not end. Please try again."),
|
||||
});
|
||||
if (!poll) {
|
||||
throw new Error("No poll instance found in room.");
|
||||
}
|
||||
|
||||
try {
|
||||
const responses = await poll.getResponses();
|
||||
const topAnswer = findTopAnswer(this.props.event, responses);
|
||||
|
||||
const message =
|
||||
topAnswer === ""
|
||||
? _t("The poll has ended. No votes were cast.")
|
||||
: _t("The poll has ended. Top answer: %(topAnswer)s", { topAnswer });
|
||||
|
||||
const endEvent = PollEndEvent.from(this.props.event.getId()!, message).serialize();
|
||||
|
||||
await this.props.matrixClient.sendEvent(this.props.event.getRoomId()!, endEvent.type, endEvent.content);
|
||||
} catch (e) {
|
||||
console.error("Failed to submit poll response event:", e);
|
||||
Modal.createDialog(ErrorDialog, {
|
||||
title: _t("Failed to end poll"),
|
||||
description: _t("Sorry, the poll did not end. Please try again."),
|
||||
});
|
||||
}
|
||||
}
|
||||
this.props.onFinished(endPoll);
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue