2021-12-08 14:56:48 +00:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2021-12-08 14:56:48 +00:00
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
2024-09-09 14:57:16 +01:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2021-12-08 14:56:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import React from "react";
|
2024-03-25 12:48:48 +00:00
|
|
|
import { MatrixEvent, MatrixClient, TimelineEvents } from "matrix-js-sdk/src/matrix";
|
2023-01-13 10:02:33 -07:00
|
|
|
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";
|
2022-11-21 21:54:24 -06:00
|
|
|
import { GetRelationsForEvent } from "../rooms/EventTile";
|
2021-12-08 14:56:48 +00:00
|
|
|
|
2023-02-28 10:31:48 +00:00
|
|
|
interface IProps {
|
2021-12-08 14:56:48 +00:00
|
|
|
matrixClient: MatrixClient;
|
|
|
|
|
event: MatrixEvent;
|
2023-02-28 10:31:48 +00:00
|
|
|
onFinished: (success?: boolean) => void;
|
2022-11-21 21:54:24 -06:00
|
|
|
getRelationsForEvent?: GetRelationsForEvent;
|
2021-12-08 14:56:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default class EndPollDialog extends React.Component<IProps> {
|
2023-02-03 09:22:26 +13:00
|
|
|
private onFinished = async (endPoll: boolean): Promise<void> => {
|
2021-12-08 14:56:48 +00:00
|
|
|
if (endPoll) {
|
2023-02-03 09:22:26 +13:00
|
|
|
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
|
|
|
|
2023-02-03 09:22:26 +13: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 =
|
2023-10-03 19:17:26 +01:00
|
|
|
topAnswer === "" ? _t("poll|end_message_no_votes") : _t("poll|end_message", { topAnswer });
|
2023-02-03 09:22:26 +13:00
|
|
|
|
|
|
|
|
const endEvent = PollEndEvent.from(this.props.event.getId()!, message).serialize();
|
|
|
|
|
|
2024-03-25 12:48:48 +00:00
|
|
|
await this.props.matrixClient.sendEvent(
|
|
|
|
|
this.props.event.getRoomId()!,
|
|
|
|
|
endEvent.type as keyof TimelineEvents,
|
|
|
|
|
endEvent.content as TimelineEvents[keyof TimelineEvents],
|
|
|
|
|
);
|
2023-02-03 09:22:26 +13:00
|
|
|
} catch (e) {
|
|
|
|
|
console.error("Failed to submit poll response event:", e);
|
|
|
|
|
Modal.createDialog(ErrorDialog, {
|
2023-10-03 19:17:26 +01:00
|
|
|
title: _t("poll|error_ending_title"),
|
|
|
|
|
description: _t("poll|error_ending_description"),
|
2022-06-14 17:51:51 +01:00
|
|
|
});
|
2023-02-03 09:22:26 +13:00
|
|
|
}
|
2021-12-08 14:56:48 +00:00
|
|
|
}
|
|
|
|
|
this.props.onFinished(endPoll);
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
public render(): React.ReactNode {
|
2021-12-08 14:56:48 +00:00
|
|
|
return (
|
|
|
|
|
<QuestionDialog
|
2023-10-03 19:17:26 +01:00
|
|
|
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)}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|