DataPanel.tsx 1.6 KB

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