createQuery.story.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import {
  20. SuperChart,
  21. ChartDataProvider,
  22. SupersetClient,
  23. } from '@superset-ui/core';
  24. import Expandable from './Expandable';
  25. import VerifyCORS, { renderError } from './VerifyCORS';
  26. export default function createQueryStory({
  27. choices,
  28. }: {
  29. choices: {
  30. [key: string]: {
  31. chartType: string;
  32. formData: {
  33. [key: string]: any;
  34. };
  35. };
  36. };
  37. }) {
  38. const keys = Object.keys(choices);
  39. const story = (
  40. host: string,
  41. mode: string | number,
  42. width: number,
  43. height: number,
  44. formData: any,
  45. ) => {
  46. const { chartType } = choices[mode];
  47. return (
  48. <div style={{ margin: 16 }}>
  49. <VerifyCORS host={host}>
  50. {() => (
  51. <ChartDataProvider
  52. client={SupersetClient}
  53. formData={JSON.parse(formData.replace(/&quot;/g, '"'))}
  54. >
  55. {({ loading, payload, error }) => {
  56. if (loading) return <div>Loading!</div>;
  57. if (error) return renderError(error);
  58. if (payload)
  59. return (
  60. <>
  61. <SuperChart
  62. chartType={chartType}
  63. width={width}
  64. height={height}
  65. formData={payload.formData}
  66. // @TODO fix typing
  67. // all vis's now expect objects but api/v1/ returns an array
  68. queriesData={payload.queriesData}
  69. />
  70. <br />
  71. <Expandable expandableWhat="payload">
  72. <pre style={{ fontSize: 11 }}>
  73. {JSON.stringify(payload, null, 2)}
  74. </pre>
  75. </Expandable>
  76. </>
  77. );
  78. return null;
  79. }}
  80. </ChartDataProvider>
  81. )}
  82. </VerifyCORS>
  83. </div>
  84. );
  85. };
  86. story.args = {
  87. host: 'localhost:8088',
  88. mode: keys[0],
  89. width: '400',
  90. height: '400',
  91. formData: JSON.stringify(choices[keys[0]].formData, null, 2),
  92. };
  93. story.argTypes = {
  94. host: {
  95. control: 'text',
  96. description: 'Superset App host for CORS request',
  97. },
  98. mode: { control: 'select', options: keys, description: 'Choose mode' },
  99. width: { control: 'text', description: 'Vis width' },
  100. height: { control: 'text', description: 'Vis height' },
  101. formData: { control: 'text', description: 'Override formData' },
  102. };
  103. return story;
  104. }