Files
ThreadNet-Web/src/components/views/dialogs/EndPollDialog.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

77 lines
2.8 KiB
TypeScript
Raw Normal View History

2021-12-08 14:56:48 +00:00
/*
Copyright 2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import React from "react";
import { MatrixEvent, MatrixClient } from "matrix-js-sdk/src/matrix";
import { PollEndEvent } from "matrix-js-sdk/src/extensible_events_v1/PollEndEvent";
2021-12-08 14:56:48 +00:00
import { _t } from "../../../languageHandler";
import QuestionDialog from "./QuestionDialog";
import { findTopAnswer } from "../messages/MPollBody";
import Modal from "../../../Modal";
import ErrorDialog from "./ErrorDialog";
import { GetRelationsForEvent } from "../rooms/EventTile";
2021-12-08 14:56:48 +00:00
interface IProps {
2021-12-08 14:56:48 +00:00
matrixClient: MatrixClient;
event: MatrixEvent;
onFinished: (success?: boolean) => void;
getRelationsForEvent?: GetRelationsForEvent;
2021-12-08 14:56:48 +00:00
}
export default class EndPollDialog extends React.Component<IProps> {
private onFinished = async (endPoll: boolean): Promise<void> => {
2021-12-08 14:56:48 +00:00
if (endPoll) {
const room = this.props.matrixClient.getRoom(this.props.event.getRoomId());
const poll = room?.polls.get(this.props.event.getId()!);
2021-12-08 14:56:48 +00:00
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("poll|end_message_no_votes") : _t("poll|end_message", { 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("poll|error_ending_title"),
description: _t("poll|error_ending_description"),
2022-06-14 17:51:51 +01:00
});
}
2021-12-08 14:56:48 +00:00
}
this.props.onFinished(endPoll);
};
public render(): React.ReactNode {
2021-12-08 14:56:48 +00:00
return (
<QuestionDialog
title={_t("poll|end_title")}
description={_t("poll|end_description")}
button={_t("poll|end_title")}
2021-12-08 14:56:48 +00:00
onFinished={(endPoll: boolean) => this.onFinished(endPoll)}
/>
);
}
}