Skip to content

Commit

Permalink
feat: add import taxonomy button
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Nov 8, 2023
1 parent 080b03e commit 9887bf3
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/taxonomy/TaxonomyListPage.jsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
import React from 'react';
import {
Button,
CardView,
Container,
DataTable,
Spinner,
} 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 { actions as importActions } from './import-tags';
import messages from './messages';
import TaxonomyCard from './taxonomy-card';
import { useTaxonomyListDataResponse, useIsTaxonomyListDataLoaded } from './data/apiHooks';

const TaxonomyListHeaderButtons = () => {
const intl = useIntl();
return (
<>
<Button variant="outline-primary" disabled>
{intl.formatMessage(messages.downloadTemplateButtonLabel)}
</Button>
<Button iconBefore={Add} onClick={() => importActions.importTaxonomy(intl)}>
{intl.formatMessage(messages.importButtonLabel)}
</Button>
</>
);
};

const TaxonomyListPage = () => {
const intl = useIntl();
const useTaxonomyListData = () => {
Expand All @@ -23,12 +43,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 +64,7 @@ const TaxonomyListPage = () => {
<SubHeader
title={intl.formatMessage(messages.headerTitle)}
titleActions={getOrgSelect()}
headerActions={getHeaderButtons()}
headerActions={<TaxonomyListHeaderButtons />}
hideBorder
/>
</Container>
Expand Down
75 changes: 75 additions & 0 deletions src/taxonomy/import-tags/data/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// @ts-check
import { intlShape } from '@edx/frontend-platform/i18n';

Check failure on line 2 in src/taxonomy/import-tags/data/actions.js

View workflow job for this annotation

GitHub Actions / tests

'intlShape' is defined but never used

import messages from '../messages';
import { importNewTaxonomy } from './api';

/**
* Import a new taxonomy
* @param {intlShape} intl
* @returns {Promise<void>}
*/
const importTaxonomy = async (intl) => {
const selectFile = async () => new Promise((resolve) => {
const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.accept = '.json,.csv';
fileInput.addEventListener('change', (event) => {
const file = event.target.files[0];
if (!file) {
resolve(null);
}
resolve(file);
});

document.body.appendChild(fileInput);
fileInput.click();
});

const getTaxonomyName = () => {
let taxonomyName = null;
while (!taxonomyName) {
taxonomyName = prompt(intl.formatMessage(messages.promptTaxonomyName));

Check warning on line 32 in src/taxonomy/import-tags/data/actions.js

View workflow job for this annotation

GitHub Actions / tests

Unexpected prompt

if (taxonomyName == null) {
break;
}

if (!taxonomyName) {
alert(intl.formatMessage(messages.promptTaxonomyNameRequired));

Check warning on line 39 in src/taxonomy/import-tags/data/actions.js

View workflow job for this annotation

GitHub Actions / tests

Unexpected alert
}
}
return taxonomyName;
};

const getTaxonomyDescription = () => prompt(intl.formatMessage(messages.promptTaxonomyDescription));

Check warning on line 45 in src/taxonomy/import-tags/data/actions.js

View workflow job for this annotation

GitHub Actions / tests

Unexpected prompt

const file = await selectFile();

if (!file) {
return;
}

const taxonomyName = getTaxonomyName();
if (taxonomyName == null) {
return;
}

const taxonomyDescription = getTaxonomyDescription();
if (taxonomyDescription == null) {
return;
}

importNewTaxonomy(taxonomyName, taxonomyDescription, file)
.then(() => {
alert(intl.formatMessage(messages.importTaxonomySuccess));

Check warning on line 65 in src/taxonomy/import-tags/data/actions.js

View workflow job for this annotation

GitHub Actions / tests

Unexpected alert
})
.catch((error) => {
alert(intl.formatMessage(messages.importTaxonomyError));

Check warning on line 68 in src/taxonomy/import-tags/data/actions.js

View workflow job for this annotation

GitHub Actions / tests

Unexpected alert
console.error(error.response);

Check warning on line 69 in src/taxonomy/import-tags/data/actions.js

View workflow job for this annotation

GitHub Actions / tests

Unexpected console statement
});
};

export default {
importTaxonomy,
};
28 changes: 28 additions & 0 deletions src/taxonomy/import-tags/data/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// @ts-check
import { camelCaseObject, getConfig } from '@edx/frontend-platform';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';

const getApiBaseUrl = () => getConfig().STUDIO_BASE_URL;

const getTaxonomyImportApiUrl = () => new URL(
'api/content_tagging/v1/taxonomies/import/',
getApiBaseUrl(),
).href;

/**
* Import a new taxonomy
* @param {string} taxonomyName
* @param {string} taxonomyDescription
* @param {File} file
* @returns {Promise<Object>}
*/ // eslint-disable-next-line import/prefer-default-export
export async function importNewTaxonomy(taxonomyName, taxonomyDescription, file) {
const formData = new FormData();
formData.append('taxonomy_name', taxonomyName);
formData.append('taxonomy_description', taxonomyDescription);
formData.append('file', file);

const { data } = await getAuthenticatedHttpClient().post(getTaxonomyImportApiUrl(), formData);

return camelCaseObject(data);
}
5 changes: 5 additions & 0 deletions src/taxonomy/import-tags/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import actions from './data/actions';

export {
actions, // eslint-disable-line import/prefer-default-export
};
26 changes: 26 additions & 0 deletions src/taxonomy/import-tags/messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { defineMessages } from '@edx/frontend-platform/i18n';

const messages = defineMessages({
promptTaxonomyName: {
id: 'course-authoring.import-tags.prompt.taxonomy-name',
defaultMessage: 'Enter a name for the new taxonomy',
},
promptTaxonomyNameRequired: {
id: 'course-authoring.import-tags.prompt.taxonomy-name.required',
defaultMessage: 'You must enter a name for the new taxonomy',
},
promptTaxonomyDescription: {
id: 'course-authoring.import-tags.prompt.taxonomy-description',
defaultMessage: 'Enter a description for the new taxonomy',
},
importTaxonomySuccess: {
id: 'course-authoring.import-tags.success',
defaultMessage: 'Taxonomy imported successfully',
},
importTaxonomyError: {
id: 'course-authoring.import-tags.error',
defaultMessage: 'Import failed - see details in the browser console',
},
});

export default messages;

0 comments on commit 9887bf3

Please sign in to comment.