mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
202 lines
5.9 KiB
202 lines
5.9 KiB
2 years ago
|
import { UITypes } from 'nocodb-sdk'
|
||
2 years ago
|
import type { ColumnType, TableType } from 'nocodb-sdk'
|
||
|
import type { Ref } from 'vue'
|
||
2 years ago
|
import { message } from 'ant-design-vue'
|
||
2 years ago
|
import dayjs from 'dayjs'
|
||
2 years ago
|
import { useI18n } from 'vue-i18n'
|
||
2 years ago
|
import {
|
||
|
NOCO,
|
||
|
extractPkFromRow,
|
||
|
extractSdkResponseErrorMsg,
|
||
2 years ago
|
getHTMLEncodedText,
|
||
2 years ago
|
useApi,
|
||
|
useInjectionState,
|
||
|
useNuxtApp,
|
||
|
useProject,
|
||
|
useProvideSmartsheetRowStore,
|
||
|
} from '#imports'
|
||
|
import type { Row } from '~/composables'
|
||
2 years ago
|
|
||
2 years ago
|
const [useProvideExpandedFormStore, useExpandedFormStore] = useInjectionState((meta: Ref<TableType>, row: Ref<Row>) => {
|
||
2 years ago
|
const { $e, $state, $api } = useNuxtApp()
|
||
|
const { api, isLoading: isCommentsLoading, error: commentsError } = useApi()
|
||
2 years ago
|
const { t } = useI18n()
|
||
2 years ago
|
|
||
2 years ago
|
const commentsOnly = ref(false)
|
||
2 years ago
|
const commentsAndLogs = ref<any[]>([])
|
||
2 years ago
|
const comment = ref('')
|
||
|
const commentsDrawer = ref(false)
|
||
2 years ago
|
const changedColumns = ref(new Set<string>())
|
||
2 years ago
|
const { project } = useProject()
|
||
2 years ago
|
const rowStore = useProvideSmartsheetRowStore(meta, row)
|
||
2 years ago
|
// todo
|
||
|
// const activeView = inject(ActiveViewInj)
|
||
|
|
||
|
// const { updateOrSaveRow, insertRow } = useViewData(meta, activeView as any)
|
||
|
|
||
|
// getters
|
||
|
const primaryValue = computed(() => {
|
||
|
if (row?.value?.row) {
|
||
|
const col = meta?.value?.columns?.find((c) => c.pv)
|
||
|
if (!col) {
|
||
|
return
|
||
|
}
|
||
|
const value = row.value.row?.[col.title as string]
|
||
|
const uidt = col.uidt
|
||
|
if (uidt === UITypes.Date) {
|
||
|
return (/^\d+$/.test(value) ? dayjs(+value) : dayjs(value)).format('YYYY-MM-DD')
|
||
|
} else if (uidt === UITypes.DateTime) {
|
||
|
return (/^\d+$/.test(value) ? dayjs(+value) : dayjs(value)).format('YYYY-MM-DD HH:mm')
|
||
|
} else if (uidt === UITypes.Time) {
|
||
|
let dateTime = dayjs(value)
|
||
|
if (!dateTime.isValid()) {
|
||
|
dateTime = dayjs(value, 'HH:mm:ss')
|
||
|
}
|
||
|
if (!dateTime.isValid()) {
|
||
|
dateTime = dayjs(`1999-01-01 ${value}`)
|
||
2 years ago
|
}
|
||
2 years ago
|
if (!dateTime.isValid()) {
|
||
|
return value
|
||
2 years ago
|
}
|
||
2 years ago
|
return dateTime.format('HH:mm:ss')
|
||
2 years ago
|
}
|
||
2 years ago
|
return value
|
||
|
}
|
||
|
})
|
||
|
|
||
|
// actions
|
||
|
const loadCommentsAndLogs = async () => {
|
||
|
if (!row.value) return
|
||
|
const rowId = extractPkFromRow(row.value.row, meta.value.columns as ColumnType[])
|
||
|
if (!rowId) return
|
||
|
commentsAndLogs.value =
|
||
|
(
|
||
|
await api.utils.commentList({
|
||
|
row_id: rowId,
|
||
|
fk_model_id: meta.value.id as string,
|
||
|
comments_only: commentsOnly.value,
|
||
|
})
|
||
|
)?.reverse?.() || []
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
const isYou = (email: string) => {
|
||
2 years ago
|
return $state.user?.value?.email === email
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
const saveComment = async () => {
|
||
|
try {
|
||
|
if (!row.value || !comment.value) return
|
||
2 years ago
|
const rowId = extractPkFromRow(row.value.row, meta.value.columns as ColumnType[])
|
||
|
if (!rowId) return
|
||
2 years ago
|
|
||
2 years ago
|
await api.utils.commentRow({
|
||
|
fk_model_id: meta.value?.id as string,
|
||
|
row_id: rowId,
|
||
2 years ago
|
// todo: description missing from argument type
|
||
2 years ago
|
description: comment.value,
|
||
2 years ago
|
} as any)
|
||
2 years ago
|
|
||
|
comment.value = ''
|
||
2 years ago
|
|
||
2 years ago
|
await loadCommentsAndLogs()
|
||
|
} catch (e: any) {
|
||
|
message.error(e.message)
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
$e('a:row-expand:comment')
|
||
|
}
|
||
|
|
||
|
const save = async () => {
|
||
2 years ago
|
let data
|
||
2 years ago
|
try {
|
||
|
// todo:
|
||
|
// if (this.presetValues) {
|
||
|
// // cater presetValues
|
||
|
// for (const k in this.presetValues) {
|
||
|
// this.$set(this.changedColumns, k, true);
|
||
|
// }
|
||
|
// }
|
||
|
|
||
2 years ago
|
const updateOrInsertObj = [...changedColumns.value].reduce((obj, col) => {
|
||
2 years ago
|
obj[col] = row.value.row[col]
|
||
|
return obj
|
||
|
}, {} as Record<string, any>)
|
||
|
|
||
2 years ago
|
if (row.value.rowMeta?.new) {
|
||
2 years ago
|
data = await $api.dbTableRow.create('noco', project.value.title as string, meta.value.title, updateOrInsertObj)
|
||
2 years ago
|
|
||
2 years ago
|
Object.assign(row.value, {
|
||
2 years ago
|
row: data,
|
||
|
rowMeta: {},
|
||
|
oldRow: { ...data },
|
||
2 years ago
|
})
|
||
2 years ago
|
} else if (Object.keys(updateOrInsertObj).length) {
|
||
|
const id = extractPkFromRow(row.value.row, meta.value.columns as ColumnType[])
|
||
2 years ago
|
|
||
2 years ago
|
if (!id) {
|
||
|
return message.info("Update not allowed for table which doesn't have primary Key")
|
||
|
}
|
||
|
await $api.dbTableRow.update(NOCO, project.value.title as string, meta.value.title, id, updateOrInsertObj)
|
||
|
for (const key of Object.keys(updateOrInsertObj)) {
|
||
|
// audit
|
||
|
$api.utils
|
||
|
.auditRowUpdate(id, {
|
||
|
fk_model_id: meta.value.id,
|
||
|
column_name: key,
|
||
|
row_id: id,
|
||
2 years ago
|
value: getHTMLEncodedText(updateOrInsertObj[key]),
|
||
|
prev_value: getHTMLEncodedText(row.value.oldRow[key]),
|
||
2 years ago
|
})
|
||
2 years ago
|
.then(async () => {
|
||
2 years ago
|
/** load latest comments/audit if right drawer is open */
|
||
2 years ago
|
if (commentsDrawer.value) {
|
||
|
await loadCommentsAndLogs()
|
||
|
}
|
||
|
})
|
||
2 years ago
|
}
|
||
|
} else {
|
||
2 years ago
|
// No columns to update
|
||
|
return message.info(t('msg.info.noColumnsToUpdate'))
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
// this.$emit('update:oldRow', { ...this.localState });
|
||
|
// this.changedColumns = {};
|
||
|
// this.$emit('input', this.localState);
|
||
|
// this.$emit('update:isNew', false);
|
||
2 years ago
|
|
||
2 years ago
|
message.success(`${primaryValue.value || 'Row'} updated successfully.`)
|
||
2 years ago
|
|
||
2 years ago
|
changedColumns.value = new Set()
|
||
2 years ago
|
} catch (e: any) {
|
||
2 years ago
|
message.error(`${t('msg.error.rowUpdateFailed')}: ${await extractSdkResponseErrorMsg(e)}`)
|
||
2 years ago
|
}
|
||
2 years ago
|
$e('a:row-expand:add')
|
||
2 years ago
|
return data
|
||
2 years ago
|
}
|
||
|
|
||
|
return {
|
||
2 years ago
|
...rowStore,
|
||
2 years ago
|
commentsOnly,
|
||
|
loadCommentsAndLogs,
|
||
|
commentsAndLogs,
|
||
|
isCommentsLoading,
|
||
|
commentsError,
|
||
|
saveComment,
|
||
|
comment,
|
||
|
isYou,
|
||
|
commentsDrawer,
|
||
|
row,
|
||
|
primaryValue,
|
||
|
save,
|
||
|
changedColumns,
|
||
|
}
|
||
|
}, 'expanded-form-store')
|
||
2 years ago
|
|
||
2 years ago
|
export { useProvideExpandedFormStore }
|
||
|
|
||
|
export function useExpandedFormStoreOrThrow() {
|
||
|
const expandedFormStore = useExpandedFormStore()
|
||
|
if (expandedFormStore == null) throw new Error('Please call `useExpandedFormStore` on the appropriate parent component')
|
||
|
return expandedFormStore
|
||
|
}
|