DataPanel.tsx 1.6 KB

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