Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: sync #8378

Merged
merged 4 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/nc-gui/components/smartsheet/Cell.vue
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ const vModel = computed({
} else if (val !== props.modelValue) {
currentRow.value.rowMeta.changed = true
emit('update:modelValue', val)
if (column.value.pk) {
if (column.value.pk || column.value.unique) {
updateWhenEditCompleted()
} else if (isAutoSaved(column.value)) {
syncValue()
Expand Down
12 changes: 10 additions & 2 deletions packages/nc-gui/components/smartsheet/column/EditOrAdd.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const isKanban = inject(IsKanbanInj, ref(false))

const readOnly = computed(() => props.readonly)

const { isMysql, isMssql, isXcdbBase } = useBase()
const { isMysql, isMssql, isDatabricks, isXcdbBase } = useBase()

const reloadDataTrigger = inject(ReloadViewDataHookInj)

Expand Down Expand Up @@ -387,10 +387,18 @@ if (props.fromTableExplorer) {
!isVirtualCol(formState) &&
!isAttachment(formState) &&
!isMssql(meta!.source_id) &&
!(isMysql(meta!.source_id) && (isJSON(formState) || isTextArea(formState)))
!(isMysql(meta!.source_id) && (isJSON(formState) || isTextArea(formState))) &&
!(isDatabricks(meta!.source_id) && formState.unique)
"
v-model:value="formState"
/>

<div
v-if="isDatabricks(meta!.source_id) && !formState.cdf && ![UITypes.MultiSelect, UITypes.Checkbox, UITypes.Rating, UITypes.Attachment, UITypes.Lookup, UITypes.Rollup, UITypes.Formula, UITypes.Barcode, UITypes.QrCode, UITypes.CreatedTime, UITypes.LastModifiedTime, UITypes.CreatedBy, UITypes.LastModifiedBy].includes(formState.uidt)"
class="mt-3"
>
<a-checkbox v-model:checked="formState.unique"> Set as Unique </a-checkbox>
</div>
mertmit marked this conversation as resolved.
Show resolved Hide resolved
</div>

<div
Expand Down
92 changes: 56 additions & 36 deletions packages/nc-gui/components/smartsheet/expanded-form/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -212,36 +212,44 @@ const onDuplicateRow = () => {
const save = async () => {
isSaving.value = true

let kanbanClbk
if (activeView.value?.type === ViewTypes.KANBAN) {
kanbanClbk = (row: any, isNewRow: boolean) => {
addOrEditStackRow(row, isNewRow)
try {
let kanbanClbk
if (activeView.value?.type === ViewTypes.KANBAN) {
kanbanClbk = (row: any, isNewRow: boolean) => {
addOrEditStackRow(row, isNewRow)
}
}
}

if (isNew.value) {
await _save(rowState.value, undefined, {
kanbanClbk,
})
} else {
await _save(undefined, undefined, {
kanbanClbk,
})
_loadRow()
}
if (isNew.value) {
await _save(rowState.value, undefined, {
kanbanClbk,
})
} else {
await _save(undefined, undefined, {
kanbanClbk,
})
_loadRow()
}

if (!props.skipReload) {
reloadTrigger?.trigger()
reloadViewDataTrigger?.trigger()
}
if (!props.skipReload) {
reloadTrigger?.trigger()
reloadViewDataTrigger?.trigger()
}

isUnsavedFormExist.value = false
isUnsavedFormExist.value = false

if (props.closeAfterSave) {
isExpanded.value = false
}
if (props.closeAfterSave) {
isExpanded.value = false
}

emits('createdRecord', _row.value.row)
emits('createdRecord', _row.value.row)
} catch (e: any) {
if (isNew.value) {
message.error(`Add row failed: ${await extractSdkResponseErrorMsg(e)}`)
} else {
message.error(`${t('msg.error.rowUpdateFailed')}: ${await extractSdkResponseErrorMsg(e)}`)
}
}
mertmit marked this conversation as resolved.
Show resolved Hide resolved

isSaving.value = false
}
Expand Down Expand Up @@ -387,15 +395,23 @@ useActiveKeyupListener(

e.stopPropagation()

if (isNew.value) {
await _save(rowState.value)
reloadHook?.trigger(null)
} else {
await save()
reloadHook?.trigger(null)
}
if (!saveRowAndStay.value) {
onClose()
try {
if (isNew.value) {
await _save(rowState.value)
reloadHook?.trigger(null)
} else {
await save()
reloadHook?.trigger(null)
}
if (!saveRowAndStay.value) {
onClose()
}
} catch (e: any) {
if (isNew.value) {
message.error(`Add row failed: ${await extractSdkResponseErrorMsg(e)}`)
} else {
message.error(`${t('msg.error.rowUpdateFailed')}: ${await extractSdkResponseErrorMsg(e)}`)
}
}
// on alt + n create new record
} else if (e.code === 'KeyN') {
Expand All @@ -422,9 +438,13 @@ useActiveKeyupListener(
okText: t('general.save'),
cancelText: t('labels.discard'),
onOk: async () => {
await _save(rowState.value)
reloadHook?.trigger(null)
addNewRow()
try {
await _save(rowState.value)
reloadHook?.trigger(null)
addNewRow()
} catch (e: any) {
message.error(`${t('msg.error.rowUpdateFailed')}: ${await extractSdkResponseErrorMsg(e)}`)
}
},
onCancel: () => {
addNewRow()
Expand Down
3 changes: 2 additions & 1 deletion packages/nc-gui/composables/useData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ export function useData(args: {

return updatedRowData
} catch (e: any) {
message.error(`${t('msg.error.rowUpdateFailed')} ${await extractSdkResponseErrorMsg(e)}`)
toUpdate.row[property] = toUpdate.oldRow[property]
message.error(`${t('msg.error.rowUpdateFailed')}: ${await extractSdkResponseErrorMsg(e)}`)
} finally {
if (toUpdate.rowMeta) toUpdate.rowMeta.saving = false
}
Expand Down
186 changes: 91 additions & 95 deletions packages/nc-gui/composables/useExpandedFormStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,129 +174,125 @@ const [useProvideExpandedFormStore, useExpandedFormStore] = useInjectionState((m
} = {},
) => {
let data
try {
const isNewRow = row.value.rowMeta?.new ?? false

if (isNewRow) {
const { getMeta } = useMetas()
const isNewRow = row.value.rowMeta?.new ?? false

const { missingRequiredColumns, insertObj } = await populateInsertObject({
meta: meta.value,
ltarState,
getMeta,
row: row.value.row,
throwError: true,
})
if (isNewRow) {
const { getMeta } = useMetas()

if (missingRequiredColumns.size) return
const { missingRequiredColumns, insertObj } = await populateInsertObject({
meta: meta.value,
ltarState,
getMeta,
row: row.value.row,
throwError: true,
})

data = await $api.dbTableRow.create('noco', base.value.id as string, meta.value.id, {
...insertObj,
...(ltarState || {}),
})
if (missingRequiredColumns.size) return

Object.assign(row.value, {
row: data,
rowMeta: {},
oldRow: { ...data },
data = await $api.dbTableRow.create('noco', base.value.id as string, meta.value.id, {
...insertObj,
...(ltarState || {}),
})

Object.assign(row.value, {
row: data,
rowMeta: {},
oldRow: { ...data },
})

if (!undo) {
const id = extractPkFromRow(data, meta.value?.columns as ColumnType[])
const pkData = rowPkData(row.value.row, meta.value?.columns as ColumnType[])

// TODO remove linked record
addUndo({
redo: {
fn: async (rowData: any) => {
await $api.dbTableRow.create('noco', base.value.id as string, meta.value.id, { ...pkData, ...rowData })
await loadKanbanData()
reloadTrigger?.trigger()
},
args: [clone(insertObj)],
},
undo: {
fn: async (id: string) => {
const res: any = await $api.dbViewRow.delete(
'noco',
base.value.id as string,
meta.value?.id as string,
activeView.value?.id as string,
encodeURIComponent(id),
)
if (res.message) {
throw new Error(res.message)
}

await loadKanbanData()
reloadTrigger?.trigger()
},
args: [id],
},
scope: defineViewScope({ view: activeView.value }),
})
}
} else {
const updateOrInsertObj = [...changedColumns.value].reduce((obj, col) => {
obj[col] = row.value.row[col]
return obj
}, {} as Record<string, any>)
if (Object.keys(updateOrInsertObj).length) {
const id = extractPkFromRow(row.value.row, meta.value.columns as ColumnType[])

if (!id) {
return message.info("Update not allowed for table which doesn't have primary Key")
}

await $api.dbTableRow.update(NOCO, base.value.id as string, meta.value.id, encodeURIComponent(id), updateOrInsertObj)

if (!undo) {
const id = extractPkFromRow(data, meta.value?.columns as ColumnType[])
const pkData = rowPkData(row.value.row, meta.value?.columns as ColumnType[])
const undoObject = [...changedColumns.value].reduce((obj, col) => {
obj[col] = row.value.oldRow[col]
return obj
}, {} as Record<string, any>)

// TODO remove linked record
addUndo({
redo: {
fn: async (rowData: any) => {
await $api.dbTableRow.create('noco', base.value.id as string, meta.value.id, { ...pkData, ...rowData })
fn: async (id: string, data: Record<string, any>) => {
await $api.dbTableRow.update(NOCO, base.value.id as string, meta.value.id, encodeURIComponent(id), data)
await loadKanbanData()

reloadTrigger?.trigger()
},
args: [clone(insertObj)],
args: [id, clone(updateOrInsertObj)],
},
undo: {
fn: async (id: string) => {
const res: any = await $api.dbViewRow.delete(
'noco',
base.value.id as string,
meta.value?.id as string,
activeView.value?.id as string,
encodeURIComponent(id),
)
if (res.message) {
throw new Error(res.message)
}

fn: async (id: string, data: Record<string, any>) => {
await $api.dbTableRow.update(NOCO, base.value.id as string, meta.value.id, encodeURIComponent(id), data)
await loadKanbanData()
reloadTrigger?.trigger()
},
args: [id],
args: [id, clone(undoObject)],
},
scope: defineViewScope({ view: activeView.value }),
})
}
} else {
const updateOrInsertObj = [...changedColumns.value].reduce((obj, col) => {
obj[col] = row.value.row[col]
return obj
}, {} as Record<string, any>)
if (Object.keys(updateOrInsertObj).length) {
const id = extractPkFromRow(row.value.row, meta.value.columns as ColumnType[])

if (!id) {
return message.info("Update not allowed for table which doesn't have primary Key")
}

await $api.dbTableRow.update(NOCO, base.value.id as string, meta.value.id, encodeURIComponent(id), updateOrInsertObj)

if (!undo) {
const undoObject = [...changedColumns.value].reduce((obj, col) => {
obj[col] = row.value.oldRow[col]
return obj
}, {} as Record<string, any>)

addUndo({
redo: {
fn: async (id: string, data: Record<string, any>) => {
await $api.dbTableRow.update(NOCO, base.value.id as string, meta.value.id, encodeURIComponent(id), data)
await loadKanbanData()

reloadTrigger?.trigger()
},
args: [id, clone(updateOrInsertObj)],
},
undo: {
fn: async (id: string, data: Record<string, any>) => {
await $api.dbTableRow.update(NOCO, base.value.id as string, meta.value.id, encodeURIComponent(id), data)
await loadKanbanData()
reloadTrigger?.trigger()
},
args: [id, clone(undoObject)],
},
scope: defineViewScope({ view: activeView.value }),
})
}

if (commentsDrawer.value) {
await loadCommentsAndLogs()
}
} else {
// No columns to update
message.info(t('msg.info.noColumnsToUpdate'))
return
if (commentsDrawer.value) {
await loadCommentsAndLogs()
}
} else {
// No columns to update
message.info(t('msg.info.noColumnsToUpdate'))
return
}
}

if (activeView.value?.type === ViewTypes.KANBAN && kanbanClbk) {
kanbanClbk(row.value, isNewRow)
}

changedColumns.value = new Set()
} catch (e: any) {
console.error(e)
message.error(`${t('msg.error.rowUpdateFailed')}: ${await extractSdkResponseErrorMsg(e)}`)
if (activeView.value?.type === ViewTypes.KANBAN && kanbanClbk) {
kanbanClbk(row.value, isNewRow)
}

changedColumns.value = new Set()
$e('a:row-expand:add')
return data
}
Expand Down