diff --git a/packages/nc-gui-v2/components/smartsheet/Grid.vue b/packages/nc-gui-v2/components/smartsheet/Grid.vue index 3a9efe60d6..ec0ca7dece 100644 --- a/packages/nc-gui-v2/components/smartsheet/Grid.vue +++ b/packages/nc-gui-v2/components/smartsheet/Grid.vue @@ -15,7 +15,7 @@ const editEnabled = ref(false) provide('isForm', false) provide('isGrid', true) -const { loadData, paginationData, formattedData: data } = useViewData(meta) +const { loadData, paginationData, formattedData: data, updateRowProperty } = useViewData(meta) onMounted(() => loadData({})) @@ -131,6 +131,7 @@ onKeyStroke(['Enter'], (e) => { v-model="row[columnObj.title]" :column="columnObj" :edit-enabled="editEnabled && selected.col === colIndex && selected.row === rowIndex" + @update:modelValue="updateRowProperty(row,columnObj.title)" /> diff --git a/packages/nc-gui-v2/composables/useViewData.ts b/packages/nc-gui-v2/composables/useViewData.ts index 0c8a1115ca..23771381c4 100644 --- a/packages/nc-gui-v2/composables/useViewData.ts +++ b/packages/nc-gui-v2/composables/useViewData.ts @@ -1,29 +1,90 @@ -import type { Api, PaginatedType, TableType } from 'nocodb-sdk' -import type { ComputedRef, Ref } from 'vue' -import { useNuxtApp } from '#app' -import useProject from '~/composables/useProject' +import type { Api, PaginatedType, TableType } from "nocodb-sdk"; +import type { ComputedRef, Ref } from "vue"; +import { useNuxtApp } from "#app"; +import useProject from "~/composables/useProject"; const formatData = (list: Array>) => list.map((row) => ({ row: { ...row }, oldRow: { ...row }, - rowMeta: {}, - })) + rowMeta: {} + })); export default (meta: Ref | ComputedRef | undefined) => { - const data = ref>>() - const formattedData = ref; oldRow: Record }>>() - const paginationData = ref() + const data = ref>>(); + const formattedData = ref; oldRow: Record; rowMeta?: any }>>(); + const paginationData = ref(); - const { project } = useProject() - const { $api } = useNuxtApp() + const { project } = useProject(); + const { $api } = useNuxtApp(); - const loadData = async (params: Parameters['dbTableRow']['list']>[3] = {}) => { - if (!project?.value?.id || !meta?.value?.id) return - const response = await $api.dbTableRow.list('noco', project.value.id, meta.value.id, params) - data.value = response.list - formattedData.value = formatData(response.list) - } + const loadData = async (params: Parameters["dbTableRow"]["list"]>[3] = {}) => { + if (!project?.value?.id || !meta?.value?.id) return; + const response = await $api.dbTableRow.list("noco", project.value.id, meta.value.id, params); + data.value = response.list; + formattedData.value = formatData(response.list); + }; - return { data, loadData, paginationData, formattedData } + const updateRowProperty = async (row: Record, property: string) => { + const id = meta?.value?.columns?.filter(c => c.pk) + .map(c => row[c.title as string]) + .join("___") as string; + + const newData = await $api.dbTableRow.update( + "noco", + project?.value.id as string, + meta?.value.id as string, + id, + { + [property]: row[property] + } + // todo: + // { + // query: { ignoreWebhook: !saved } + // } + ); + + /* + + todo: audit + + // audit + this.$api.utils + .auditRowUpdate(id, { + fk_model_id: this.meta.id, + column_name: column.title, + row_id: id, + value: getPlainText(rowObj[column.title]), + prev_value: getPlainText(oldRow[column.title]) + }) + .then(() => {}) + */ + + }; + const insertRow = async (row: Record, rowIndex = formattedData.value?.length) => { + // todo: implement insert row + + + const insertObj = meta?.value?.columns?.reduce((o: any, col) => { + if (!col.ai && row?.[col.title as string] !== null) { + o[col.title as string] = row?.[col.title as string]; + } + return o; + }, {}); + + const insertedData = await $api.dbTableRow.create( + "noco", + project?.value.id as string, + meta?.value.id as string, + insertObj + ); + + formattedData.value?.splice(rowIndex ?? 0, 1, { + row: insertedData, + rowMeta: {}, + oldRow: { ...insertedData } + }); + }; + + return { data, loadData, paginationData, formattedData, insertRow, updateRowProperty }; }