Commit 77c2259f authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Restrict annotations export to JSON

parent 3bd366f0
......@@ -27,7 +27,8 @@ export type ExportAnnotationsProps = {
};
type ExportFormat = {
value: 'json' | 'csv' | 'text' | 'html';
/** Unique format identifier used also as file extension */
value: 'json' | 'csv' | 'txt' | 'html';
name: string;
};
......@@ -36,18 +37,20 @@ const exportFormats: ExportFormat[] = [
value: 'json',
name: 'JSON',
},
{
value: 'csv',
name: 'CSV',
},
{
value: 'text',
name: 'Text',
},
{
value: 'html',
name: 'HTML',
},
// TODO Enable these formats when implemented
// {
// value: 'csv',
// name: 'CSV',
// },
// {
// value: 'txt',
// name: 'Text',
// },
// {
// value: 'html',
// name: 'HTML',
// },
];
/**
......@@ -118,12 +121,16 @@ function ExportAnnotations({
e.preventDefault();
try {
const format = exportFormat.value;
const annotationsToExport =
selectedUser?.annotations ?? exportableAnnotations;
const filename = `${customFilename ?? defaultFilename}.json`;
const filename = `${customFilename ?? defaultFilename}.${format}`;
if (format === 'json') {
const exportData =
annotationsExporter.buildExportContent(annotationsToExport);
annotationsExporter.buildJSONExportContent(annotationsToExport);
downloadJSONFile(exportData, filename);
}
} catch (e) {
toastMessenger.error('Exporting annotations failed');
}
......
......@@ -34,7 +34,7 @@ describe('ExportAnnotations', () => {
beforeEach(() => {
fakeAnnotationsExporter = {
buildExportContent: sinon.stub().returns({}),
buildJSONExportContent: sinon.stub().returns({}),
};
fakeToastMessenger = {
error: sinon.stub(),
......@@ -240,9 +240,9 @@ describe('ExportAnnotations', () => {
submitExportForm(wrapper);
assert.calledOnce(fakeAnnotationsExporter.buildExportContent);
assert.calledOnce(fakeAnnotationsExporter.buildJSONExportContent);
assert.calledWith(
fakeAnnotationsExporter.buildExportContent,
fakeAnnotationsExporter.buildJSONExportContent,
annotationsToExport,
);
assert.notCalled(fakeToastMessenger.error);
......@@ -298,9 +298,9 @@ describe('ExportAnnotations', () => {
submitExportForm(wrapper);
assert.calledOnce(fakeAnnotationsExporter.buildExportContent);
assert.calledOnce(fakeAnnotationsExporter.buildJSONExportContent);
assert.calledWith(
fakeAnnotationsExporter.buildExportContent,
fakeAnnotationsExporter.buildJSONExportContent,
selectedUserAnnotations,
);
});
......@@ -326,7 +326,7 @@ describe('ExportAnnotations', () => {
context('when exporting annotations fails', () => {
it('displays error toast message', () => {
fakeAnnotationsExporter.buildExportContent.throws(
fakeAnnotationsExporter.buildJSONExportContent.throws(
new Error('Error exporting'),
);
......@@ -335,7 +335,7 @@ describe('ExportAnnotations', () => {
submitExportForm(wrapper);
assert.notCalled(fakeDownloadJSONFile);
assert.calledOnce(fakeAnnotationsExporter.buildExportContent);
assert.calledOnce(fakeAnnotationsExporter.buildJSONExportContent);
assert.calledWith(
fakeToastMessenger.error,
'Exporting annotations failed',
......
......@@ -22,7 +22,7 @@ export class AnnotationsExporter {
this._store = store;
}
buildExportContent(
buildJSONExportContent(
annotations: APIAnnotationData[],
/* istanbul ignore next - test seam */
now = new Date(),
......
......@@ -27,7 +27,7 @@ describe('AnnotationsExporter', () => {
},
];
const result = exporter.buildExportContent(annotations, now);
const result = exporter.buildJSONExportContent(annotations, now);
assert.deepEqual(result, {
export_date: now.toISOString(),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment