import { ComputedRef, Ref, ref } from 'vue'; import { useQuasar } from 'quasar'; import { useI18n } from 'vue-i18n'; import { assignTableRows } from 'src/helpers/assignTableRows'; import type { Table } from 'src/stores/main/models'; interface Params { tableId: ComputedRef; isAdminPage: ComputedRef; selectedTable: Ref; fetchTable: (id: string) => Promise; } /** * Loads the table for TablePage. Tables are stored data only here — the * live/synced sources (Google Sheets, external URLs, WooCommerce) belong to * vTables Pro, and fetch_table returns just their metadata. */ export function useTableDataFetch({ tableId, isAdminPage, selectedTable, fetchTable, }: Params) { const $q = useQuasar(); const { t } = useI18n(); const loading: Ref = ref(false); async function onFetchTable({ onReset }: { onReset?: boolean } = {}) { loading.value = true; const { response, error } = await fetchTable(tableId.value); if (error) { if (isAdminPage.value) { $q.notify({ type: 'negative', message: t('messages.unable_to_fetch_table'), }); } loading.value = false; return; } if (response.data?.source?.rows?.length) { response.data.source.rows = assignTableRows(response.data.source.rows); } selectedTable.value = response.data; if (onReset) { $q.notify({ type: 'positive', message: t('messages.settings_have_been_reset'), }); } loading.value = false; } return { loading, onFetchTable, }; }