DataPanel.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import React, { Component, ComponentClass } from 'react';
  2. import _ from 'lodash';
  3. export interface OuterProps {
  4. type: string;
  5. queries: any[];
  6. isVisible: boolean;
  7. }
  8. export interface AddedProps {
  9. data: any[];
  10. }
  11. interface State {
  12. isLoading: boolean;
  13. data: any[];
  14. }
  15. const DataPanel = (ComposedComponent: ComponentClass<AddedProps & OuterProps>) => {
  16. class Wrapper extends Component<OuterProps, State> {
  17. public static defaultProps = {
  18. isVisible: true,
  19. };
  20. constructor(props: OuterProps) {
  21. super(props);
  22. this.state = {
  23. isLoading: false,
  24. data: [],
  25. };
  26. }
  27. public componentDidMount() {
  28. this.issueQueries();
  29. }
  30. public issueQueries = () => {
  31. const { queries, isVisible } = this.props;
  32. if (!isVisible) {
  33. return;
  34. }
  35. if (!queries.length) {
  36. this.setState({ data: [{ message: 'no queries' }] });
  37. return;
  38. }
  39. this.setState({ isLoading: true });
  40. };
  41. public render() {
  42. const { data, isLoading } = this.state;
  43. if (!data.length) {
  44. return (
  45. <div className="no-data">
  46. <p>No Data</p>
  47. </div>
  48. );
  49. }
  50. if (isLoading) {
  51. return (
  52. <div className="loading">
  53. <p>Loading</p>
  54. </div>
  55. );
  56. }
  57. return <ComposedComponent {...this.props} data={data} />;
  58. }
  59. }
  60. return Wrapper;
  61. };
  62. export default DataPanel;