Skip to content

Commit

Permalink
feat: add download taxonomy template button
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Nov 1, 2023
1 parent df8432d commit f8cfcb8
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 11 deletions.
48 changes: 41 additions & 7 deletions src/taxonomy/TaxonomyListPage.jsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,58 @@
import React from 'react';
import {
Button,
CardView,
Container,
DataTable,
Dropdown,
DropdownButton,
OverlayTrigger,
Spinner,
Tooltip,
} from '@edx/paragon';
import {
Add,
} from '@edx/paragon/icons';
import { StudioFooter } from '@edx/frontend-component-footer';
import { useIntl } from '@edx/frontend-platform/i18n';
import Header from '../header';
import SubHeader from '../generic/sub-header/SubHeader';
import { downloadTaxonomyTemplate } from './data/thunks';
import messages from './messages';
import TaxonomyCard from './taxonomy-card';
import { useTaxonomyListDataResponse, useIsTaxonomyListDataLoaded } from './hooks';

const TaxonomyListHeaderButtons = () => {
const intl = useIntl();
return (
<>
<OverlayTrigger
placement="top"
overlay={(
<Tooltip>
{intl.formatMessage(messages.downloadTemplateButtonHint)}
</Tooltip>
)}
>
<DropdownButton
variant="outline-primary"
title={intl.formatMessage(messages.downloadTemplateButtonLabel)}
>
<Dropdown.Item onClick={() => downloadTaxonomyTemplate('csv')}>
{intl.formatMessage(messages.downloadTemplateButtonCSVLabel)}
</Dropdown.Item>
<Dropdown.Item onClick={() => downloadTaxonomyTemplate('json')}>
{intl.formatMessage(messages.downloadTemplateButtonJSONLabel)}
</Dropdown.Item>
</DropdownButton>
</OverlayTrigger>
<Button iconBefore={Add} disabled>
{intl.formatMessage(messages.importButtonLabel)}
</Button>
</>
);
};

const TaxonomyListPage = () => {
const intl = useIntl();
const useTaxonomyListData = () => {
Expand All @@ -23,12 +63,6 @@ const TaxonomyListPage = () => {

const { taxonomyListData, isLoaded } = useTaxonomyListData();

const getHeaderButtons = () => (
// Download template and import buttons.
// TODO Add functionality to this buttons.
undefined
);

const getOrgSelect = () => (
// Organization select component
// TODO Add functionality to this component
Expand All @@ -50,7 +84,7 @@ const TaxonomyListPage = () => {
<SubHeader
title={intl.formatMessage(messages.headerTitle)}
titleActions={getOrgSelect()}
headerActions={getHeaderButtons()}
headerActions={<TaxonomyListHeaderButtons />}
hideBorder
/>
</Container>
Expand Down
13 changes: 13 additions & 0 deletions src/taxonomy/data/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export const getExportTaxonomyApiUrl = (pk, format) => new URL(
`api/content_tagging/v1/taxonomies/${pk}/export/?output_format=${format}&download=1`,
getApiBaseUrl(),
).href;
const getTaxonomyTemplateApiUrl = (format) => new URL(
`api/content_tagging/v1/taxonomies/import/template.${format}`,
getApiBaseUrl(),
).href;

/**
* Get list of taxonomies.
Expand All @@ -27,3 +31,12 @@ export async function getTaxonomyListData() {
export function getTaxonomyExportFile(pk, format) {
window.location.href = getExportTaxonomyApiUrl(pk, format);
}

/**
* Downloads the template file for import taxonomies
* @param {('json'|'csv')} format
* @returns {void}
*/
export function getTaxonomyTemplateFile(format) {
window.location.href = getTaxonomyTemplateApiUrl(format);
}
13 changes: 10 additions & 3 deletions src/taxonomy/data/thunks.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
// @ts-check
import { getTaxonomyExportFile } from './api';
import { getTaxonomyExportFile, getTaxonomyTemplateFile } from './api';

/**
* Downloads the file of the exported taxonomy
* @param {number} pk
* @param {string} format
* @returns {void}
*/
const exportTaxonomy = (pk, format) => (
export const exportTaxonomy = (pk, format) => (
getTaxonomyExportFile(pk, format)
);

export default exportTaxonomy;
/**
* Downloads the template file for import taxonomies
* @param {('json'|'csv')} format
* @returns {void}
*/
export const downloadTaxonomyTemplate = (format) => (
getTaxonomyTemplateFile(format)
);
2 changes: 1 addition & 1 deletion src/taxonomy/export-modal/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import PropTypes from 'prop-types';
import { useIntl } from '@edx/frontend-platform/i18n';
import messages from './messages';
import exportTaxonomy from '../data/thunks';
import { exportTaxonomy } from '../data/thunks';

const ExportModal = ({
taxonomyId,
Expand Down
12 changes: 12 additions & 0 deletions src/taxonomy/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ const messages = defineMessages({
id: 'course-authoring.taxonomy-list.button.download-template.label',
defaultMessage: 'Download template',
},
downloadTemplateButtonCSVLabel: {
id: 'course-authoring.taxonomy-list.button.download-template.csv.label',
defaultMessage: 'CSV template',
},
downloadTemplateButtonJSONLabel: {
id: 'course-authoring.taxonomy-list.button.download-template.json.label',
defaultMessage: 'JSON template',
},
downloadTemplateButtonHint: {
id: 'course-authoring.taxonomy-list.butotn.download-template.hint',
defaultMessage: 'Download example taxonomy',
},
importButtonLabel: {
id: 'course-authoring.taxonomy-list.button.import.label',
defaultMessage: 'Import',
Expand Down

0 comments on commit f8cfcb8

Please sign in to comment.