Commit bfa8df67 authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Make sure user input is not overridden by export suggested filename

parent 82ed0375
import { Button, CardActions, Input } from '@hypothesis/frontend-shared'; import { Button, CardActions, Input } from '@hypothesis/frontend-shared';
import { useRef } from 'preact/hooks'; import { useMemo, useState } from 'preact/hooks';
import { downloadJSONFile } from '../../../shared/download-json-file'; import { downloadJSONFile } from '../../../shared/download-json-file';
import { withServices } from '../../service-context'; import { withServices } from '../../service-context';
...@@ -15,8 +15,6 @@ export type ExportAnnotationsProps = { ...@@ -15,8 +15,6 @@ export type ExportAnnotationsProps = {
toastMessenger: ToastMessengerService; toastMessenger: ToastMessengerService;
}; };
// TODO: does the Input need a label?
/** /**
* Render content for "export" tab panel: allow user to export annotations * Render content for "export" tab panel: allow user to export annotations
* with a specified filename. * with a specified filename.
...@@ -32,7 +30,11 @@ function ExportAnnotations({ ...@@ -32,7 +30,11 @@ function ExportAnnotations({
const exportCount = exportableAnnotations.length; const exportCount = exportableAnnotations.length;
const draftCount = store.countDrafts(); const draftCount = store.countDrafts();
const inputRef = useRef<HTMLInputElement | null>(null); const defaultFilename = useMemo(
() => suggestedFilename({ groupName: group?.name }),
[group],
);
const [customFilename, setCustomFilename] = useState<string>();
if (!exportReady) { if (!exportReady) {
return <LoadingSpinner />; return <LoadingSpinner />;
...@@ -42,7 +44,7 @@ function ExportAnnotations({ ...@@ -42,7 +44,7 @@ function ExportAnnotations({
e.preventDefault(); e.preventDefault();
try { try {
const filename = `${inputRef.current!.value}.json`; const filename = `${customFilename ?? defaultFilename}.json`;
const exportData = annotationsExporter.buildExportContent( const exportData = annotationsExporter.buildExportContent(
exportableAnnotations, exportableAnnotations,
); );
...@@ -75,8 +77,11 @@ function ExportAnnotations({ ...@@ -75,8 +77,11 @@ function ExportAnnotations({
<Input <Input
data-testid="export-filename" data-testid="export-filename"
id="export-filename" id="export-filename"
defaultValue={suggestedFilename({ groupName: group?.name })} defaultValue={defaultFilename}
elementRef={inputRef} value={customFilename}
onChange={e =>
setCustomFilename((e.target as HTMLInputElement).value)
}
required required
maxLength={250} maxLength={250}
/> />
......
...@@ -135,9 +135,12 @@ describe('ExportAnnotations', () => { ...@@ -135,9 +135,12 @@ describe('ExportAnnotations', () => {
it('downloads a file using user-entered filename appended with `.json`', () => { it('downloads a file using user-entered filename appended with `.json`', () => {
const wrapper = createComponent(); const wrapper = createComponent();
const filenameInput = wrapper.find(
'input[data-testid="export-filename"]',
);
wrapper.find('input[data-testid="export-filename"]').getDOMNode().value = filenameInput.getDOMNode().value = 'my-filename';
'my-filename'; filenameInput.simulate('change');
submitExportForm(wrapper); submitExportForm(wrapper);
......
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