Skip to content

Commit

Permalink
Extract to helper
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorKrupenja committed Nov 19, 2024
1 parent d764fab commit 965a794
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 34 deletions.
19 changes: 2 additions & 17 deletions GUI/src/pages/Training/Intents/CommonIntents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import IntentExamplesTable from './IntentExamplesTable';
import LoadingDialog from '../../../components/LoadingDialog';
import ConnectServiceToIntentModal from 'pages/ConnectServiceToIntentModal';
import withAuthorization, { ROLES } from 'hoc/with-authorization';
import { saveCsv } from 'utils/save-csv';

const CommonIntents: FC = () => {
const { t } = useTranslation();
Expand Down Expand Up @@ -276,23 +277,7 @@ const CommonIntents: FC = () => {
mutationFn: (intentModelData: { intentName: string }) =>
downloadExamples(intentModelData),
onSuccess: async (data) => {
// @ts-ignore
const blob = new Blob([data], { type: 'text/csv' });
const fileName = selectedIntent?.id + '.csv';

if (window.showSaveFilePicker) {
const handle = await window.showSaveFilePicker({ suggestedName: fileName });
const writable = await handle.createWritable();
await writable.write(blob);
writable.close();
} else {
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
}
saveCsv(data, selectedIntent?.id || '');

toast.open({
type: 'success',
Expand Down
19 changes: 2 additions & 17 deletions GUI/src/pages/Training/Intents/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { Rule, RuleDTO } from '../../../types/rule';
import { addStoryOrRule, deleteStoryOrRule } from '../../../services/stories';
import IntentTabList from './IntentTabList';
import useStore from "../../../store/store";
import { saveCsv } from 'utils/save-intent-examples-csv';

type Response = {
name: string;
Expand Down Expand Up @@ -459,23 +460,7 @@ const Intents: FC = () => {
mutationFn: (intentModelData: { intentName: string }) =>
downloadExamples(intentModelData),
onSuccess: async (data) => {
// @ts-ignore
const blob = new Blob([data], { type: 'text/csv' });
const fileName = selectedIntent?.id + '.csv';

if (window.showSaveFilePicker) {
const handle = await window.showSaveFilePicker({ suggestedName: fileName });
const writable = await handle.createWritable();
await writable.write(blob);
writable.close();
} else {
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
}
saveCsv(data, selectedIntent?.id || '');

toast.open({
type: 'success',
Expand Down
19 changes: 19 additions & 0 deletions GUI/src/utils/save-csv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const saveCsv = async (data: unknown, name: string) => {
// @ts-ignore
const blob = new Blob([data], { type: 'text/csv' });
const fileName = name + '.csv';

if (window.showSaveFilePicker) {
const handle = await window.showSaveFilePicker({ suggestedName: fileName });
const writable = await handle.createWritable();
await writable.write(blob);
writable.close();
} else {
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
}
};

0 comments on commit 965a794

Please sign in to comment.