Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.85 KB | None | 0 0
  1. {
  2. slotId: "1",
  3. initialized: "Y",
  4. label: "some information about slot 1"
  5. }
  6.  
  7. const viewOptions = [
  8. {
  9. text: "slot info",
  10. value: "slotInfo",
  11. description: "display information about slots"
  12. }
  13. ];
  14.  
  15. <Dropdown
  16. value={viewOptions}
  17. />
  18.  
  19. import React, { Component } from "react";
  20. import ReactDOM from "react-dom";
  21. import axios from "axios";
  22.  
  23. import {
  24. Dropdown,
  25. Form,
  26. Divider,
  27. Input,
  28. Message,
  29. Loader,
  30. Table
  31. } from "semantic-ui-react";
  32.  
  33. import "./styles.css";
  34.  
  35. var MockAdapter = require("axios-mock-adapter");
  36. var mock = new MockAdapter(axios);
  37.  
  38. mock.onGet("/slotInfo").reply(200, {
  39. data: {
  40. slotInfo: [
  41. {
  42. slotId: "1",
  43. initialized: "Y",
  44. label: "some information about slot 1"
  45. },
  46. {
  47. slotId: "2",
  48. initialized: "N",
  49. label: "some information about slot 2"
  50. },
  51. {
  52. slotId: "3",
  53. initialized: "Y",
  54. label: "some information about slot 3"
  55. },
  56. {
  57. slotId: "4",
  58. initialized: "N",
  59. label: "some information about slot 4"
  60. },
  61.  
  62. {
  63. slotId: "5",
  64. initialized: "N",
  65. label: "some information about slot 5"
  66. },
  67.  
  68. {
  69. slotId: "6",
  70. initialized: "Y",
  71. label: "some information about slot 6"
  72. },
  73.  
  74. {
  75. slotId: "7",
  76. initialized: "N",
  77. label: "some information about slot 7"
  78. },
  79.  
  80. {
  81. slotId: "8",
  82. initialized: "N",
  83. label: "some information about slot 8"
  84. },
  85.  
  86. {
  87. slotId: "9",
  88. initialized: "Y",
  89. label: "some information about slot 9"
  90. },
  91.  
  92. {
  93. slotId: "10",
  94. initialized: "N",
  95. label: "some information about slot 10"
  96. },
  97. {
  98. slotId: "11",
  99. initialized: "Y",
  100. label: "some information about slot 11"
  101. }
  102. ]
  103. }
  104. });
  105.  
  106. //values for viewOptions dropdown
  107. const viewOptions = [
  108. {
  109. text: "slotInfo",
  110. value: "slotInfo",
  111. description: "Show slot info"
  112. }
  113. ];
  114.  
  115. class App extends Component {
  116. constructor(props) {
  117. super(props);
  118.  
  119. this.state = {
  120. viewSelection: "",
  121. isLoading: true,
  122. slotId: ""
  123. };
  124. }
  125.  
  126. handleViewSelection = (e, { value }) => {
  127. this.setState({ viewSelection: value }, () => {
  128. console.log("view election --> ", this.state.viewSelection);
  129. });
  130. };
  131.  
  132. onSlotIdChange = async (e, { value }) => {
  133. this.setState({ slotId: value, isLoading: false }, () => {
  134. console.log(
  135. "chosen slotId updated to state callback -->",
  136. this.state.slotId
  137. );
  138. });
  139.  
  140. if (!value) {
  141. this.handleClear();
  142. return;
  143. }
  144. };
  145.  
  146. handleClear = () => {
  147. this.setState({ slotId: "", isLoading: true }, () => {
  148. console.log("slotId cleared");
  149. });
  150. };
  151.  
  152. render() {
  153. const { viewSelection, isLoading, slotId } = this.state;
  154. return (
  155. <div
  156. style={{ display: "flex", minHeight: "100vh", flexDirection: "column" }}
  157. >
  158. <div style={{ flex: 1 }}>
  159. <div
  160. style={{
  161. display: "flex",
  162. flexDirection: "column",
  163. justifyContent: "center",
  164. alignItems: "center",
  165. flex: "1"
  166. }}
  167. >
  168. <div style={{ width: "1000px" }}>
  169. <Divider style={{ marginTop: "7em" }} horizontal>
  170. View Data
  171. </Divider>
  172. <Message style={{ marginTop: "2em" }} info>
  173. <Message.Header>
  174. Want to see more specific information? Log in &nbsp;
  175. <a href="/">here</a>.
  176. </Message.Header>
  177. </Message>
  178. <Form.Field style={{ marginTop: "2em" }}>
  179. <label>Select data to view</label>
  180. <Dropdown
  181. scrolling
  182. placeholder="Pick an option"
  183. value={viewSelection}
  184. fluid
  185. selection
  186. multiple={false}
  187. search
  188. options={viewOptions}
  189. onChange={this.handleViewSelection}
  190. />
  191. </Form.Field>
  192.  
  193. {this.state.viewSelection && (
  194. <div style={{ marginTop: "2em" }}>
  195. {viewSelection && viewSelection === "slotInfo" ? (
  196. <>
  197. <label style={{ display: "block", marginTop: "2em" }}>
  198. Select a Slot ID to init
  199. </label>
  200. <GetSlotIds
  201. slotId={slotId}
  202. onSlotIdChange={this.onSlotIdChange}
  203. />
  204. </>
  205. ) : null}
  206. </div>
  207. )}
  208. </div>
  209. </div>
  210. </div>
  211. </div>
  212. );
  213. }
  214. }
  215.  
  216. class GetSlotIds extends Component {
  217. constructor(props) {
  218. super(props);
  219.  
  220. this.state = { slotId: "", slotInfo: [] };
  221. }
  222.  
  223. async componentDidMount() {
  224. await this.getSlotIds();
  225. }
  226.  
  227. getSlotIds = async () => {
  228. try {
  229. const { data } = await axios.get("/slotInfo");
  230. console.log("Slot info ---> ", data);
  231. const slotInfo = data.data.slotInfo;
  232. this.setState({ slotInfo: slotInfo });
  233. } catch (error) {
  234. console.error(Error(`Error getting slotIds: ${error.message}`));
  235. }
  236. };
  237.  
  238. render() {
  239. const { onSlotIdChange, slotId } = this.props;
  240. const { slotInfo } = this.state;
  241. return (
  242. <div>
  243. <Form.Field required>
  244. <Dropdown
  245. id="slotId"
  246. value={slotId}
  247. onChange={onSlotIdChange}
  248. selection
  249. fluid
  250. placeholder="Please select a slot id"
  251. clearable
  252. options={slotInfo.map(slotId => {
  253. return {
  254. text: slotId.slotId,
  255. value: slotId.slotId,
  256. key: slotId.slotId
  257. };
  258. })}
  259. />
  260. </Form.Field>
  261. <div style={{ marginTop: "2em" }}>
  262. <Table inverted celled>
  263. <Table.Header fullWidth>
  264. <Table.Row>
  265. <Table.HeaderCell>Slot Id</Table.HeaderCell>
  266. <Table.HeaderCell>Initialized</Table.HeaderCell>
  267. <Table.HeaderCell>Label</Table.HeaderCell>
  268. </Table.Row>
  269. </Table.Header>
  270.  
  271. <Table.Body>
  272. {Object.values(slotInfo).map(({ slotId, initialized, label }) => {
  273. return (
  274. <Table.Row>
  275. <Table.Cell>{slotId}</Table.Cell>
  276. <Table.Cell>{initialized}</Table.Cell>
  277. <Table.Cell>{label}</Table.Cell>
  278. </Table.Row>
  279. );
  280. })}
  281. </Table.Body>
  282. </Table>
  283. </div>
  284. </div>
  285. );
  286. }
  287. }
  288.  
  289. const rootElement = document.getElementById("root");
  290. ReactDOM.render(<App />, rootElement);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement