|
| 1 | +/* |
| 2 | +Copyright 2021 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import React from 'react'; |
| 18 | +import { _t } from '../../../languageHandler'; |
| 19 | +import { replaceableComponent } from "../../../utils/replaceableComponent"; |
| 20 | +import { IBodyProps } from "./IBodyProps"; |
| 21 | +import { IPollAnswer, IPollContent, POLL_START_EVENT_TYPE } from '../../../polls/consts'; |
| 22 | +import StyledRadioButton from '../elements/StyledRadioButton'; |
| 23 | + |
| 24 | +// TODO: [andyb] Use extensible events library when ready |
| 25 | +const TEXT_NODE_TYPE = "org.matrix.msc1767.text"; |
| 26 | + |
| 27 | +interface IState { |
| 28 | + selected?: string; |
| 29 | +} |
| 30 | + |
| 31 | +@replaceableComponent("views.messages.MPollBody") |
| 32 | +export default class MPollBody extends React.Component<IBodyProps, IState> { |
| 33 | + constructor(props: IBodyProps) { |
| 34 | + super(props); |
| 35 | + |
| 36 | + this.state = { |
| 37 | + selected: null, |
| 38 | + }; |
| 39 | + } |
| 40 | + |
| 41 | + private selectOption(answerId: string) { |
| 42 | + this.setState({ selected: answerId }); |
| 43 | + } |
| 44 | + |
| 45 | + private onOptionSelected = (e: React.FormEvent<HTMLInputElement>): void => { |
| 46 | + this.selectOption(e.currentTarget.value); |
| 47 | + }; |
| 48 | + |
| 49 | + render() { |
| 50 | + const pollStart: IPollContent = |
| 51 | + this.props.mxEvent.getContent()[POLL_START_EVENT_TYPE.name]; |
| 52 | + const pollId = this.props.mxEvent.getId(); |
| 53 | + |
| 54 | + return <div className="mx_MPollBody"> |
| 55 | + <h2>{ pollStart.question[TEXT_NODE_TYPE] }</h2> |
| 56 | + <div className="mx_MPollBody_allOptions"> |
| 57 | + { |
| 58 | + pollStart.answers.map((answer: IPollAnswer) => { |
| 59 | + const checked = this.state.selected === answer.id; |
| 60 | + const classNames = `mx_MPollBody_option${ |
| 61 | + checked ? " mx_MPollBody_option_checked": "" |
| 62 | + }`; |
| 63 | + return <div |
| 64 | + key={answer.id} |
| 65 | + className={classNames} |
| 66 | + onClick={() => this.selectOption(answer.id)} |
| 67 | + > |
| 68 | + <StyledRadioButton |
| 69 | + name={`poll_answer_select-${pollId}`} |
| 70 | + value={answer.id} |
| 71 | + checked={this.state.selected === answer.id} |
| 72 | + onChange={this.onOptionSelected} |
| 73 | + > |
| 74 | + <div className="mx_MPollBody_optionVoteCount"> |
| 75 | + { _t("%(number)s votes", { number: 0 }) } |
| 76 | + </div> |
| 77 | + <div className="mx_MPollBody_optionText"> |
| 78 | + { answer[TEXT_NODE_TYPE] } |
| 79 | + </div> |
| 80 | + </StyledRadioButton> |
| 81 | + <div className="mx_MPollBody_popularityBackground"> |
| 82 | + <div className="mx_MPollBody_popularityAmount" /> |
| 83 | + </div> |
| 84 | + </div>; |
| 85 | + }) |
| 86 | + } |
| 87 | + </div> |
| 88 | + <div className="mx_MPollBody_totalVotes"> |
| 89 | + { _t( "Based on %(total)s votes", { total: 0 } ) } |
| 90 | + </div> |
| 91 | + </div>; |
| 92 | + } |
| 93 | +} |
0 commit comments