From f5578dd050be4d23dc4860b89bf7879e4f0dedfa Mon Sep 17 00:00:00 2001 From: Pavel Tiunov Date: Wed, 19 Jun 2019 14:56:38 -0700 Subject: [PATCH] feat(playground): App layout for dashboard --- .../cubejs-playground/src/DashboardSource.js | 55 +++++++++++++++++-- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/packages/cubejs-playground/src/DashboardSource.js b/packages/cubejs-playground/src/DashboardSource.js index ec69389a1f4e8..d812c6aef948c 100644 --- a/packages/cubejs-playground/src/DashboardSource.js +++ b/packages/cubejs-playground/src/DashboardSource.js @@ -9,8 +9,35 @@ const prettier = require("prettier/standalone"); const plugins = [require("prettier/parser-babylon")]; const dashboardComponents = ` -import { Row, Col, Card } from 'antd'; +import { Row, Col, Card, Layout } from 'antd'; import 'antd/dist/antd.css'; +import './index.css' + +const AppLayout = ({ children }) => ( + + +
+

+ My Dashboard +

+
+
+ + {children} + +
+) const Dashboard = ({ children }) => ( {children} @@ -25,6 +52,12 @@ const DashboardItem = ({ children, title }) => ( ) `; +const indexCss = ` +body { + background-color: #f0f2f5 !important; +} +`; + const fetchWithRetry = (url, options, retries) => fetch(url, { ...options, retries }); class DashboardSource { @@ -43,15 +76,21 @@ class DashboardSource { } async persist() { + const updateIndexCss = this.appLayoutAdded ? [ + { ...this.indexCssFile, content: this.indexCssFile.content + indexCss } + ] : []; await fetchWithRetry('/playground/dashboard-app-files', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ - files: [{ ...this.appFile, content: prettier.format(this.dashboardAppCode(), { parser: "babylon", plugins }) }] + files: [ + { ...this.appFile, content: prettier.format(this.dashboardAppCode(), { parser: "babylon", plugins }) } + ].concat(updateIndexCss) }) }, 5); + this.appLayoutAdded = false; const dependencies = this.imports .filter(i => i.get('source').node.value.indexOf('.') !== 0) .map(i => { @@ -75,6 +114,7 @@ class DashboardSource { if (!this.appFile) { throw new Error(`src/App.js file not found. Can't parse dashboard app. Please delete dashboard-app directory and try to create it again.`); } + this.indexCssFile = sourceFiles.find(f => f.fileName.indexOf('src/index.css') !== -1); this.appAst = parse(this.appFile.content, { sourceFilename: this.appFile.fileName, sourceType: 'module', @@ -150,11 +190,16 @@ class DashboardSource { } }); if (!dashboardAdded && headerElement) { + this.appLayoutAdded = true; headerElement.parentPath.replaceWith( t.JSXElement( - t.JSXOpeningElement(t.JSXIdentifier('Dashboard'), []), - t.JSXClosingElement(t.JSXIdentifier('Dashboard')), - [] + t.JSXOpeningElement(t.JSXIdentifier('AppLayout'), []), + t.JSXClosingElement(t.JSXIdentifier('AppLayout')), + [t.JSXElement( + t.JSXOpeningElement(t.JSXIdentifier('Dashboard'), []), + t.JSXClosingElement(t.JSXIdentifier('Dashboard')), + [] + )] ) ); }