Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import * as Widgets from 'components/widgets';
  3. import { get, apiCall } from '../../../../helpers/api';
  4. import { formatBigNumber } from '../../../../helpers/misc';
  5. import { Dimmer, Loader, Table, Icon } from 'semantic-ui-react';
  6.  
  7. let messages = {};
  8.  
  9. export default class ChartContainer extends Component {
  10. state = {
  11. data: null,
  12. isLoading: false
  13. }
  14.  
  15. apiCall = null
  16.  
  17. componentWillMount() {
  18. messages = this.props.messages;
  19. this.getData(this.props);
  20. }
  21.  
  22. componentWillReceiveProps(nextProps) {
  23. let emptyObj = {},
  24. prevWidget = this.props.widget || emptyObj,
  25. nextWidget = nextProps.widget || emptyObj,
  26. prevQuery = this.props.query || emptyObj;
  27.  
  28. if(nextWidget) {
  29. if((nextWidget.filters !== prevWidget.filters && nextWidget.filters !== prevQuery.filters)
  30. || nextWidget.isUpdated !== prevWidget.isUpdated
  31. || (nextWidget.options !== prevWidget.options && (nextWidget.filters || nextWidget.query) && nextWidget.options)
  32. || nextWidget.query !== prevWidget.query
  33. ) {
  34. this.getData(nextProps);
  35. }
  36. }
  37. }
  38.  
  39. componentWillUnmount() {
  40. this.cancelApiCall();
  41. }
  42.  
  43. cancelApiCall() {
  44. if(this.apiCall) {
  45. this.apiCall.cancel();
  46. }
  47. }
  48.  
  49. render() {
  50. let Content = null,
  51. presentation = this.props.config.presentation;
  52.  
  53. if(presentation) {
  54. if(typeof presentation.component === "string") {
  55. Content = Widgets[presentation.component];
  56. } else if(typeof presentation.component === "function") {
  57. Content = presentation.component;
  58. }
  59. } else {
  60. console.warn("Widget '"+this.props.widget.name+"' with type '"+this.props.widget.type+"' is missing key 'content' in the config");
  61. }
  62.  
  63. if(this.props.widget.filters === undefined && this.props.config.sidebar && this.props.config.sidebar.filters) {
  64. Content = Widgets["WidgetEmpty"];
  65. }
  66.  
  67. //TODO: DONT SEND ALL PROPS?
  68. return (
  69. !this.state.isLoading && Content ?
  70. <Content data={this.state.data} {...this.props} />
  71. : this.state.isLoading ?
  72. (<Dimmer active inverted>
  73. <Loader active inverted></Loader>
  74. </Dimmer>)
  75. : <div></div>
  76. );
  77. }
  78.  
  79. getData(props) {
  80. if(props.config.container.endpoint) {
  81. if(props.widget.filters !== undefined || (!this.props.config.sidebar || !this.props.config.sidebar.filters)) {
  82. this.cancelApiCall();
  83. this.apiCall = apiCall();
  84.  
  85. this.setState({
  86. data: null,
  87. isLoading: true
  88. });
  89.  
  90. let options = props.widget.options;
  91. let filters = (props.query && props.query.filters) || (props.widget.filters);
  92.  
  93. get(props.config.container.endpoint, {...filters, ...options}, this.apiCall.token).then(response => {
  94. let {data = [], stats = null, warnings = []} = response,
  95. hasData = data && data.length >= 1,
  96. info = hasData ? null : messages.info.noData,
  97. popup = null;
  98.  
  99. let configStats = this.props.config.presentation.stats;
  100. if(configStats && hasData && (stats || warnings.length)) {
  101. popup = this.getPopup(warnings, stats, configStats);
  102. }
  103.  
  104. this.props.widgetContentUpdate(popup, null, info, hasData);
  105.  
  106. this.setState({
  107. isLoading: false,
  108. data: hasData ? data : null
  109. });
  110. }).catch(e => {
  111. if(e.type) {
  112. if(e.type === "error") {
  113. this.setState({
  114. isLoading: false
  115. });
  116. this.props.widgetContentUpdate(null, e.message);
  117. }
  118. } else {
  119. console.warn(e);
  120. }
  121. });
  122. }
  123. } else {
  124. console.warn("Widget '"+this.props.widget.name+"' with type '"+this.props.widget.type+"' is missing key 'endpoint' in the config");
  125. }
  126. }
  127.  
  128. getPopup = (warnings = [], stats = {}, configStats) => {
  129. return {warning: warnings.length > 0, icon: 'info', content: <div>
  130. <h5>Dataset</h5>
  131. <Table size='small' basic compact definition>
  132. <Table.Body>
  133. {configStats.map((stat, i) => (
  134. <Table.Row key={i}>
  135. <Table.Cell>{stat.name}</Table.Cell>
  136. <Table.Cell>{formatBigNumber(stats[stat.key]) || "-"}</Table.Cell>
  137. </Table.Row>
  138. ))}
  139. </Table.Body>
  140. </Table>
  141. { warnings.length > 0 &&
  142. <span>
  143. <h5><Icon className='warning'/>Warnings</h5>
  144. {warnings.map((warning, index) => (
  145. <p key={index}>{warning}</p>
  146. ))}
  147. </span>
  148. }
  149. </div>};
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement