Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
use Poll model with relations API in poll rendering (#9877)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Kerry committed Feb 2, 2023
1 parent b45b933 commit 544baa3
Show file tree
Hide file tree
Showing 9 changed files with 347 additions and 667 deletions.
8 changes: 8 additions & 0 deletions res/css/views/messages/_MPollBody.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,16 @@ limitations under the License.
}

.mx_MPollBody_totalVotes {
display: flex;
flex-direction: inline;
justify-content: start;
color: $secondary-content;
font-size: $font-12px;

.mx_Spinner {
flex: 0;
margin-left: $spacing-8;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/views/context_menus/MessageContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export default class MessageContextMenu extends React.Component<IProps, IState>
return (
M_POLL_START.matches(mxEvent.getType()) &&
this.state.canRedact &&
!isPollEnded(mxEvent, MatrixClientPeg.get(), this.props.getRelationsForEvent)
!isPollEnded(mxEvent, MatrixClientPeg.get())
);
}

Expand Down
40 changes: 24 additions & 16 deletions src/components/views/dialogs/EndPollDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
private onFinished = async (endPoll: boolean): Promise<void> => {
if (endPoll) {
const room = this.props.matrixClient.getRoom(this.props.event.getRoomId());
const poll = room?.polls.get(this.props.event.getId()!);

const message =
topAnswer === ""
? _t("The poll has ended. No votes were cast.")
: _t("The poll has ended. Top answer: %(topAnswer)s", { topAnswer });
if (!poll) {
throw new Error("No poll instance found in room.");
}

if (endPoll) {
const endEvent = PollEndEvent.from(this.props.event.getId(), message).serialize();
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();

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."),
});
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);
};
Expand Down
Loading

0 comments on commit 544baa3

Please sign in to comment.