From e3b4181079b3904053f2ac5d621c0f4434c18d34 Mon Sep 17 00:00:00 2001 From: Jeremy Foster Date: Fri, 22 Mar 2024 07:21:09 -0700 Subject: [PATCH] HOSTSD-295 Fix dashboard (#122) --- .../src/components/dashboard/Dashboard.tsx | 154 ++++++++---------- .../filter/FilteredOrganizations.tsx | 4 +- .../src/components/filter/FilteredTenants.tsx | 6 +- 3 files changed, 68 insertions(+), 96 deletions(-) diff --git a/src/dashboard/src/components/dashboard/Dashboard.tsx b/src/dashboard/src/components/dashboard/Dashboard.tsx index e19deb8a..0d038c2a 100644 --- a/src/dashboard/src/components/dashboard/Dashboard.tsx +++ b/src/dashboard/src/components/dashboard/Dashboard.tsx @@ -137,6 +137,42 @@ export const Dashboard = () => { values.tenant, ]); + const handleExport = React.useCallback( + async (filter: { + search?: string; + tenantId?: number; + organizationId?: number; + organizationName?: string; + operatingSystemItemId?: number; + serviceNowKey?: string; + startDate?: string; + endDate?: string; + }) => { + const toastLoading = toast.loading('Generating Excel document...'); + try { + await download({ + search: filter.search, + tenantId: filter.tenantId, + organizationId: filter.organizationId, + organizationName: filter.organizationName, + operatingSystemItemId: filter.operatingSystemItemId, + serviceNowKey: filter.serviceNowKey, + startDate: filter.startDate, + endDate: filter.endDate, + }); + toast.dismiss(toastLoading); + toast.success('Excel document has been downloaded successfully.'); + } catch (ex) { + toast.dismiss(toastLoading); + + const error = ex as Error; + toast.error('Failed to download data. ' + error.message); + console.error(error); + } + }, + [download], + ); + return ( <> 1} /> @@ -204,23 +240,11 @@ export const Dashboard = () => { } }} showExport - onExport={async () => { - const toastLoading = toast.loading('Generating Excel document...'); - - try { - await download({ - tenantId: dashboardTenant?.id, - organizationId: dashboardOrganization?.id, - }); - toast.dismiss(toastLoading); - toast.success('Excel document has been downloaded successfully.'); - } catch (ex) { - toast.dismiss(toastLoading); - - const error = ex as Error; - toast.error('Failed to download data. ' + error.message); - console.error(error); - } + onExport={() => { + handleExport({ + tenantId: dashboardTenant?.id, + organizationId: dashboardOrganization?.id, + }); }} /> )} @@ -235,22 +259,8 @@ export const Dashboard = () => { serverItems={dashboardServerItems} loading={!isReadyOrganizations || !isReadyServerItems} showExport - onExport={async () => { - const toastLoading = toast.loading('Generating Excel document...'); - - try { - await download({ - tenantId: dashboardTenant?.id, - }); - toast.dismiss(toastLoading); - toast.success('Excel document has been downloaded successfully.'); - } catch (ex) { - toast.dismiss(toastLoading); - - const error = ex as Error; - toast.error('Failed to download data. ' + error.message); - console.error(error); - } + onExport={() => { + handleExport({ tenantId: dashboardTenant?.id }); }} /> )} @@ -258,27 +268,15 @@ export const Dashboard = () => { large={!!dashboardOrganization || !!dashboardOperatingSystemItem || !!dashboardServerItem} serverItems={dashboardServerItem ? [dashboardServerItem] : dashboardServerItems} showExport - onExport={async (startDate, endDate) => { - const toastLoading = toast.loading('Generating Excel document...'); - - try { - await downloadHistory({ - tenantId: dashboardTenant?.id, - organizationId: dashboardOrganization?.id, - operatingSystemItemId: dashboardOperatingSystemItem?.id, - serviceNowKey: dashboardServerItem?.serviceNowKey, - startDate: startDate, - endDate: endDate, - }); - toast.dismiss(toastLoading); - toast.success('Excel document has been downloaded successfully.'); - } catch (ex) { - toast.dismiss(toastLoading); - - const error = ex as Error; - toast.error('Failed to download data. ' + error.message); - console.error(error); - } + onExport={(startDate, endDate) => { + handleExport({ + tenantId: dashboardTenant?.id, + organizationId: dashboardOrganization?.id, + operatingSystemItemId: dashboardOperatingSystemItem?.id, + serviceNowKey: dashboardServerItem?.serviceNowKey, + startDate: startDate, + endDate: endDate, + }); }} /> {showAllocationByStorageVolume && ( @@ -362,23 +360,11 @@ export const Dashboard = () => { await updateDashboard({}); } }} - onExport={async (search) => { - const toastLoading = toast.loading('Generating Excel document...'); - - try { - await download({ - tenantId: dashboardTenant?.id, - organizationName: search ? search : undefined, - }); - toast.dismiss(toastLoading); - toast.success('Excel document has been downloaded successfully.'); - } catch (ex) { - toast.dismiss(toastLoading); - - const error = ex as Error; - toast.error('Failed to download data. ' + error.message); - console.error(error); - } + onExport={(search) => { + handleExport({ + tenantId: dashboardTenant?.id, + organizationName: search ? search : undefined, + }); }} /> )} @@ -399,25 +385,13 @@ export const Dashboard = () => { await updateDashboard({ tenant, organization, operatingSystemItem, serverItem }); }} showExport - onExport={async (search) => { - const toastLoading = toast.loading('Generating Excel document...'); - - try { - await download({ - tenantId: dashboardTenant?.id, - organizationId: dashboardOrganization?.id, - operatingSystemItemId: dashboardOperatingSystemItem?.id, - search: search ? search : undefined, - }); - toast.dismiss(toastLoading); - toast.success('Excel document has been downloaded successfully.'); - } catch (ex) { - toast.dismiss(toastLoading); - - const error = ex as Error; - toast.error('Failed to download data. ' + error.message); - console.error(error); - } + onExport={(search) => { + handleExport({ + tenantId: dashboardTenant?.id, + organizationId: dashboardOrganization?.id, + operatingSystemItemId: dashboardOperatingSystemItem?.id, + search: search ? search : undefined, + }); }} /> )} diff --git a/src/dashboard/src/components/filter/FilteredOrganizations.tsx b/src/dashboard/src/components/filter/FilteredOrganizations.tsx index e8d24288..bc39a224 100644 --- a/src/dashboard/src/components/filter/FilteredOrganizations.tsx +++ b/src/dashboard/src/components/filter/FilteredOrganizations.tsx @@ -46,7 +46,7 @@ export const FilteredOrganizations = ({}: IFilteredOrganizationsProps) => { setValues((values) => ({ ...values, organization: organizations[0] })); }, [setFilteredOrganizations, organizations, setValues, filteredOrganizations.length]); - return ( + return filteredOrganizationOptions.length > 1 ? ( { setLoading(false); }} /> - ) : ( - <> - ); + ) : null; };