-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate csv download button on all plotly v2 charts and tables
- Loading branch information
1 parent
548d780
commit 404b52f
Showing
7 changed files
with
151 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import React, { useState } from 'react'; | ||
import { | ||
Button, | ||
} from '@openedx/paragon'; | ||
|
||
import PropTypes from 'prop-types'; | ||
import { logError } from '@edx/frontend-platform/logging'; | ||
import { saveAs } from 'file-saver'; | ||
import EnterpriseDataApiService from '../../data/services/EnterpriseDataApiService'; | ||
|
||
const DownloadCSV = ({ | ||
startDate, endDate, chartType, activeTab, granularity, calculation, | ||
}) => { | ||
const [isLoading, setIsLoading] = useState(false); | ||
const downloadCsv = () => { | ||
setIsLoading(true); | ||
const chartUrl = activeTab; | ||
const enterpriseUUID = '9e4ce45148c44c2c86a9bb3ddd138d6b'; | ||
EnterpriseDataApiService.fetchPlotlyChartsCSV(enterpriseUUID, chartUrl, { | ||
start_date: startDate, | ||
end_date: endDate, | ||
granularity, | ||
calculation, | ||
chart_type: chartType, | ||
response_type: 'csv', | ||
}).then((response) => { | ||
const contentDisposition = response.headers['content-disposition']; | ||
let filename = 'download.csv'; // Default filename | ||
|
||
// Extract the filename from the content-disposition header if it exists | ||
if (contentDisposition && contentDisposition.indexOf('attachment') !== -1) { | ||
const matches = /filename="([^"]+)"/.exec(contentDisposition); | ||
if (matches != null && matches[1]) { | ||
[, filename] = matches; | ||
} | ||
} | ||
|
||
const blob = new Blob([response.data], { type: 'text/csv' }); | ||
saveAs(blob, filename); | ||
setIsLoading(false); | ||
}).catch((error) => { | ||
setIsLoading(false); | ||
logError(error); | ||
}); | ||
}; | ||
return ( | ||
<Button | ||
variant="primary" | ||
className="ml-3" | ||
onClick={() => downloadCsv()} | ||
> | ||
{isLoading ? 'loading' : 'Download CSV'} | ||
</Button> | ||
); | ||
}; | ||
|
||
DownloadCSV.propTypes = { | ||
startDate: PropTypes.string.isRequired, | ||
endDate: PropTypes.string.isRequired, | ||
chartType: PropTypes.string.isRequired, | ||
activeTab: PropTypes.string.isRequired, | ||
granularity: PropTypes.string.isRequired, | ||
calculation: PropTypes.string.isRequired, | ||
}; | ||
|
||
export default DownloadCSV; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,47 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import DownloadCSV from './DownloadCSV'; | ||
|
||
const Header = ({ title, subtitle }) => ( | ||
<div className="analytics-header"> | ||
<h2 className="analytics-header-title">{title}</h2> | ||
{subtitle && <p className="analytics-header-subtitle">{subtitle}</p>} | ||
const Header = ({ | ||
title, subtitle, startDate, endDate, isDownloadCSV, activeTab, granularity, calculation, chartType, | ||
}) => ( | ||
<div className="analytics-header d-flex justify-content-between"> | ||
<div> | ||
<h2 className="analytics-header-title">{title}</h2> | ||
{subtitle && <p className="analytics-header-subtitle">{subtitle}</p>} | ||
</div> | ||
{isDownloadCSV && ( | ||
<div className="mr-2"> | ||
<DownloadCSV | ||
startDate={startDate} | ||
endDate={endDate} | ||
activeTab={activeTab} | ||
granularity={granularity} | ||
calculation={calculation} | ||
chartType={chartType} | ||
/> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
|
||
Header.defaultProps = { | ||
subtitle: undefined, | ||
isDownloadCSV: false, | ||
granularity: 'day', | ||
calculation: 'count', | ||
}; | ||
|
||
Header.propTypes = { | ||
title: PropTypes.string.isRequired, | ||
subtitle: PropTypes.string, | ||
isDownloadCSV: PropTypes.bool, | ||
startDate: PropTypes.string.isRequired, | ||
endDate: PropTypes.string.isRequired, | ||
activeTab: PropTypes.string.isRequired, | ||
chartType: PropTypes.string.isRequired, | ||
granularity: PropTypes.string, | ||
calculation: PropTypes.string, | ||
}; | ||
|
||
export default Header; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const advancedAnalyticsV2QueryKeys = { | ||
chartCSV: (enterpriseId, chartName) => [...advancedAnalyticsV2QueryKeys.all, 'chartCSV', enterpriseId, chartName], | ||
}; | ||
|
||
export const chartTypes = { | ||
BUBBLE: 'bubble', | ||
TOP_SKILLS_ENROLLMENT: 'top_skills_enrollment', | ||
TOP_SKILLS_COMPLETION: 'top_skills_completion', | ||
COMPLETIONS_OVER_TIME: 'completions_over_time', | ||
TOP_COURSES_BY_COMPLETIONS: 'top_courses_by_completions', | ||
TOP_SUBJECTS_BY_COMPLETIONS: 'top_subjects_by_completions', | ||
}; | ||
|
||
export const ANALYTICS_TABS = { | ||
SKILLS: 'skills', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters