| 102 | ]; |
| 103 | |
| 104 | export default class UserInput extends Component<IProps, IState> { |
| 105 | constructor(props: IProps) { |
| 106 | super(props); |
| 107 | this.state = { |
| 108 | npcOptions: {}, |
| 109 | isExpanded: localStorage.getItem("isExpanded") === "true", |
| 110 | }; |
| 111 | } |
| 112 | |
| 113 | toggleExpand = () => { |
| 114 | this.setState( |
| 115 | (prevState) => ({ |
| 116 | isExpanded: !prevState.isExpanded, |
| 117 | }), |
| 118 | () => { |
| 119 | localStorage.setItem("isExpanded", this.state.isExpanded.toString()); |
| 120 | }, |
| 121 | ); |
| 122 | }; |
| 123 | |
| 124 | onSubmit = (event: React.FormEvent) => { |
| 125 | event.preventDefault(); |
| 126 | this.props.generate(this.state.npcOptions); |
| 127 | }; |
| 128 | |
| 129 | private getCharacterText() { |
| 130 | const elementData = document.getElementById("downloadData"); |
| 131 | |
| 132 | if (!elementData) { |
| 133 | alert("Unable to find character data"); |
| 134 | throw new Error("Missing element downloadData"); |
| 135 | } |
| 136 | |
| 137 | const body = (elementData.textContent || "").split("#").join("\r\n").split("#").join("\r\n"); |
| 138 | return body; |
| 139 | } |
| 140 | |
| 141 | private getExportFileName() { |
| 142 | const name = this.props.npc.description.name.split(" ")[0]; |
| 143 | const gender = this.props.npc.description.gender; |
| 144 | const race = this.props.npc.description.race.split(" ").join("_"); |
| 145 | const occupation = this.props.npc.description.occupation.split(" ").join("_"); |
| 146 | |
| 147 | const filename = name + "_" + gender + "_" + race + "_" + occupation; |
| 148 | |
| 149 | return filename; |
| 150 | } |
| 151 | |
| 152 | private downloadTxtFile: React.MouseEventHandler<HTMLButtonElement> = (ev) => { |
| 153 | ev.stopPropagation(); |
| 154 | |
| 155 | const element = document.createElement("a"); |
| 156 | const characterDetails = this.getCharacterText(); |
| 157 | const file = new Blob([characterDetails], { type: "text/plain" }); |
| 158 | element.href = URL.createObjectURL(file); |
| 159 | element.download = this.getExportFileName() + ".txt"; |
| 160 | document.body.appendChild(element); |
| 161 | element.click(); |
nothing calls this directly
no test coverage detected