From 965a794f8c0585ba333da2536d3bfa3919719573 Mon Sep 17 00:00:00 2001 From: Igor Krupenja Date: Tue, 19 Nov 2024 09:20:38 +0200 Subject: [PATCH] Extract to helper --- .../pages/Training/Intents/CommonIntents.tsx | 19 ++----------------- GUI/src/pages/Training/Intents/index.tsx | 19 ++----------------- GUI/src/utils/save-csv.ts | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+), 34 deletions(-) create mode 100644 GUI/src/utils/save-csv.ts diff --git a/GUI/src/pages/Training/Intents/CommonIntents.tsx b/GUI/src/pages/Training/Intents/CommonIntents.tsx index 6ee956a43..44950a778 100644 --- a/GUI/src/pages/Training/Intents/CommonIntents.tsx +++ b/GUI/src/pages/Training/Intents/CommonIntents.tsx @@ -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(); @@ -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', diff --git a/GUI/src/pages/Training/Intents/index.tsx b/GUI/src/pages/Training/Intents/index.tsx index e2b204157..e7c04170b 100644 --- a/GUI/src/pages/Training/Intents/index.tsx +++ b/GUI/src/pages/Training/Intents/index.tsx @@ -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; @@ -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', diff --git a/GUI/src/utils/save-csv.ts b/GUI/src/utils/save-csv.ts new file mode 100644 index 000000000..944164834 --- /dev/null +++ b/GUI/src/utils/save-csv.ts @@ -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); + } +};