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