From b99c2af3c65816c4342d4d28340c9e96c42535f5 Mon Sep 17 00:00:00 2001 From: HendrikThePendric Date: Thu, 16 Jan 2025 17:40:20 +0100 Subject: [PATCH] feat: create superset embedded dashboards --- i18n/en.pot | 10 ++- .../CreateSupersetEmbeddedDashboard.js | 78 +++++++++++++++++-- yarn.lock | 2 +- 3 files changed, 79 insertions(+), 11 deletions(-) diff --git a/i18n/en.pot b/i18n/en.pot index 0313a576f..fab63ebe1 100644 --- a/i18n/en.pot +++ b/i18n/en.pot @@ -5,8 +5,8 @@ msgstr "" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1)\n" -"POT-Creation-Date: 2025-01-16T10:34:08.846Z\n" -"PO-Revision-Date: 2025-01-16T10:34:08.847Z\n" +"POT-Creation-Date: 2025-01-16T15:24:12.682Z\n" +"PO-Revision-Date: 2025-01-16T15:24:12.682Z\n" msgid "Untitled dashboard" msgstr "Untitled dashboard" @@ -38,6 +38,12 @@ msgstr "Cancel" msgid "New dashboard: configure external source (superset)" msgstr "New dashboard: configure external source (superset)" +msgid "Could not create dashboard" +msgstr "Could not create dashboard" + +msgid "An unknown error occurred" +msgstr "An unknown error occurred" + msgid "Back" msgstr "Back" diff --git a/src/components/DashboardsBar/CreateDashboardButton/CreateSupersetEmbeddedDashboard.js b/src/components/DashboardsBar/CreateDashboardButton/CreateSupersetEmbeddedDashboard.js index 0fbbff625..fbf71e425 100644 --- a/src/components/DashboardsBar/CreateDashboardButton/CreateSupersetEmbeddedDashboard.js +++ b/src/components/DashboardsBar/CreateDashboardButton/CreateSupersetEmbeddedDashboard.js @@ -1,3 +1,4 @@ +import { useDataMutation } from '@dhis2/app-runtime' import i18n from '@dhis2/d2-i18n' import { Button, @@ -6,30 +7,69 @@ import { ModalActions, ModalContent, ModalTitle, + NoticeBox, } from '@dhis2/ui' import PropTypes from 'prop-types' -import React from 'react' +import React, { useCallback, useState } from 'react' +import { useDispatch } from 'react-redux' +import { useHistory } from 'react-router-dom' +import { tFetchDashboards } from '../../../actions/dashboards.js' import { useSupersetEmbeddedDashboardFieldsState } from '../../../modules/useSupersetEmbeddedDashboardFieldsState.js' import { SupersetEmbeddedDashboardFields } from '../../SupersetEmbeddedDashboardFields.js' +const postDashboardQuery = { + resource: 'dashboards', + type: 'create', + data: ({ values }) => ({ + name: values.title || 'Untitled dashboard', + description: values.description, + code: values.code, + embedded: { + provider: 'SUPERSET', + id: values.supersetEmbedId, + options: { + hideTab: false, + hideChartControls: !values.showChartControls, + filters: { + visible: values.showFilters, + expanded: false, + }, + }, + }, + }), +} + export const CreateSupersetEmbeddedDashboard = ({ backToChooseDashboardModal, closeModal, }) => { + const dispatch = useDispatch() + const history = useHistory() + const [loading, setLoading] = useState(false) + const [postDashboard, { error }] = useDataMutation(postDashboardQuery) const { + hasFieldChanges, isSupersetEmbedIdValid, isSupersetEmbedIdFieldTouched, values, onChange, onSupersetEmbedIdFieldBlur, } = useSupersetEmbeddedDashboardFieldsState() - const postNewDashboard = () => { - console.log('POSTING...') - closeModal() - } + const handleSubmit = useCallback( + async (event) => { + event.preventDefault() + setLoading(true) + const { response } = await postDashboard({ values }) + await dispatch(tFetchDashboards()) + closeModal() + history.push(`/${response.uid}`) + }, + [values, postDashboard] + ) + return ( -
+ element?.focus()}> {i18n.t( 'New dashboard: configure external source (superset)', @@ -45,14 +85,36 @@ export const CreateSupersetEmbeddedDashboard = ({ values={values} onChange={onChange} onSupersetEmbedIdFieldBlur={onSupersetEmbedIdFieldBlur} + submitting={loading} /> + {error && ( + + {error?.details?.response?.errorReports[0] + ?.message ?? + i18n.t('An unknown error occurred')} + + )} - - diff --git a/yarn.lock b/yarn.lock index 1e4996ba6..0d100f6e2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -17835,7 +17835,7 @@ workerpool@^6.5.1: resolved "https://registry.yarnpkg.com/workerpool/-/workerpool-6.5.1.tgz#060f73b39d0caf97c6db64da004cd01b4c099544" integrity sha512-Fs4dNYcsdpYSAfVxhnl1L5zTksjvOJxtC5hzMNl+1t9B8hTJTdKDyZ5ju7ztgPy+ft9tBFXoOlDNiOT9WUXZlA== -"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0: +"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0": version "7.0.0" resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==