Skip to content

Commit 2a7010c

Browse files
committed
move action to store
1 parent e5e72cb commit 2a7010c

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

packages/compass-schema/src/components/export-schema-modal.tsx

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
type SchemaFormat,
2828
type ExportStatus,
2929
trackSchemaExportFailed,
30+
downloadSchema,
3031
} from '../stores/schema-export-reducer';
3132

3233
const loaderStyles = css({
@@ -87,21 +88,18 @@ const ExportSchemaModal: React.FunctionComponent<{
8788
onChangeSchemaExportFormat: (format: SchemaFormat) => Promise<void>;
8889
onClose: () => void;
8990
onExportedSchemaCopied: () => void;
90-
onExportedSchema: () => void;
91-
onSchemaExportFailed: (stage: string) => void;
91+
onSchemaDownload: () => void;
9292
}> = ({
9393
errorMessage,
9494
exportStatus,
9595
isOpen,
9696
exportFormat,
9797
exportedSchema,
98-
filename,
9998
onCancelSchemaExport,
10099
onChangeSchemaExportFormat,
101100
onClose,
102101
onExportedSchemaCopied,
103-
onExportedSchema,
104-
onSchemaExportFailed,
102+
onSchemaDownload,
105103
}) => {
106104
const onFormatOptionSelected = useCallback(
107105
(event: ChangeEvent<HTMLInputElement>) => {
@@ -112,25 +110,6 @@ const ExportSchemaModal: React.FunctionComponent<{
112110
[onChangeSchemaExportFormat]
113111
);
114112

115-
const handleSchemaDownload = useCallback(() => {
116-
try {
117-
if (!exportedSchema) return;
118-
const blob = new Blob([exportedSchema], {
119-
type: 'application/json',
120-
});
121-
const url = window.URL.createObjectURL(blob);
122-
const link = document.createElement('a');
123-
link.href = url;
124-
link.download = filename || 'export.json';
125-
link.click();
126-
window.URL.revokeObjectURL(url);
127-
onExportedSchema();
128-
} catch (error) {
129-
onSchemaExportFailed('download button clicked');
130-
throw error;
131-
}
132-
}, [exportedSchema, filename, onSchemaExportFailed, onExportedSchema]);
133-
134113
return (
135114
<Modal open={isOpen} setOpen={onClose}>
136115
<ModalHeader title="Export Schema" />
@@ -209,7 +188,7 @@ const ExportSchemaModal: React.FunctionComponent<{
209188
isLoading={exportStatus === 'inprogress'}
210189
loadingIndicator={<SpinLoader />}
211190
disabled={!exportedSchema}
212-
onClick={handleSchemaDownload}
191+
onClick={onSchemaDownload}
213192
data-testid="schema-export-download-button"
214193
>
215194
Export
@@ -235,5 +214,6 @@ export default connect(
235214
onCancelSchemaExport: cancelExportSchema,
236215
onChangeSchemaExportFormat: changeExportSchemaFormat,
237216
onClose: closeExportSchema,
217+
onSchemaDownload: downloadSchema,
238218
}
239219
)(ExportSchemaModal);

packages/compass-schema/src/stores/schema-export-reducer.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,30 @@ async function getSchemaByFormat({
153153
return JSON.stringify(schema, null, 2);
154154
}
155155

156+
export const downloadSchema = (): SchemaThunkAction<void> => {
157+
return (dispatch, getState) => {
158+
try {
159+
const {
160+
schemaExport: { exportedSchema, filename },
161+
} = getState();
162+
if (!exportedSchema) return;
163+
const blob = new Blob([exportedSchema], {
164+
type: 'application/json',
165+
});
166+
const url = window.URL.createObjectURL(blob);
167+
const link = document.createElement('a');
168+
link.href = url;
169+
link.download = filename || 'export.json';
170+
link.click();
171+
window.URL.revokeObjectURL(url);
172+
dispatch(trackSchemaExported);
173+
} catch (error) {
174+
dispatch(trackSchemaExportFailed('download button clicked'));
175+
throw error;
176+
}
177+
};
178+
};
179+
156180
export const trackSchemaExportFailed = (
157181
stage: string
158182
): SchemaThunkAction<void> => {

0 commit comments

Comments
 (0)