Browse Source

feat: disable edit/delete if primary key missing

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/498/head
Pranav C 3 years ago
parent
commit
4d92f35274
  1. 3
      packages/nc-gui/components/project/spreadsheet/components/expandedForm.vue
  2. 32
      packages/nc-gui/components/project/spreadsheet/rowsXcDataTable.vue
  3. 30
      packages/nc-gui/components/project/spreadsheet/views/xcGridView.vue

3
packages/nc-gui/components/project/spreadsheet/components/expandedForm.vue

@ -413,6 +413,9 @@ export default {
await this.reload()
} else if (Object.keys(updatedObj).length) {
if (!id) {
return this.$toast.info('Update not allowed for table which doesn\'t have primary Key').goAway(3000)
}
await this.api.update(id, updatedObj, this.oldRow)
} else {
return this.$toast.info('No columns to update').goAway(3000)

32
packages/nc-gui/components/project/spreadsheet/rowsXcDataTable.vue

@ -62,7 +62,15 @@
<v-spacer class="h-100" @dblclick="debug=true" />
<debug-metas v-if="debug" class="mr-3" />
<v-tooltip bottom>
<template #activator="{on}">
<v-icon v-if="!isPkAvail" color="warning" small class="mr-3" v-on="on">
mdi-information-outline
</v-icon>
</template>
<span class="caption"> Update & Delete not allowed since the table doesn't have any primary key
</span>
</v-tooltip>
<lock-menu v-if="_isUIAllowed('view-type')" v-model="viewStatus.type" />
<x-btn tooltip="Reload view data" outlined small text @click="reload">
<v-icon small class="mr-1" color="grey darken-3">
@ -208,6 +216,7 @@
:meta="meta"
:is-virtual="selectedView.type === 'vtable'"
:api="api"
:is-pk-avail="isPkAvail"
@onNewColCreation="onNewColCreation"
@onCellValueChange="onCellValueChange"
@insertNewRow="insertNewRow"
@ -631,8 +640,8 @@ export default {
if (
!this.meta || (
(this.meta.hasMany && this.meta.hasMany.length) ||
(this.meta.manyToMany && this.meta.manyToMany.length) ||
(this.meta.belongsTo && this.meta.belongsTo.length))
(this.meta.manyToMany && this.meta.manyToMany.length) ||
(this.meta.belongsTo && this.meta.belongsTo.length))
) {
return this.$toast.info('Please delete relations before deleting table.').goAway(3000)
}
@ -817,6 +826,10 @@ export default {
const id = this.meta.columns.filter(c => c.pk).map(c => rowObj[c._cn]).join('___')
if (!id) {
return this.$toast.info('Update not allowed for table which doesn\'t have primary Key').goAway(3000)
}
const newData = await this.api.update(id, {
[column._cn]: rowObj[column._cn]
}, { [column._cn]: oldRow[column._cn] })
@ -841,6 +854,11 @@ export default {
const rowObj = this.rowContextMenu.row
if (!this.rowContextMenu.rowMeta.new) {
const id = this.meta && this.meta.columns && this.meta.columns.filter(c => c.pk).map(c => rowObj[c._cn]).join('___')
if (!id) {
return this.$toast.info('Delete not allowed for table which doesn\'t have primary Key').goAway(3000)
}
await this.api.delete(id)
}
this.data.splice(this.rowContextMenu.index, 1)
@ -859,6 +877,11 @@ export default {
}
if (!rowMeta.new) {
const id = this.meta.columns.filter(c => c.pk).map(c => rowObj[c._cn]).join('___')
if (!id) {
return this.$toast.info('Delete not allowed for table which doesn\'t have primary Key').goAway(3000)
}
await this.api.delete(id)
}
this.data.splice(row, 1)
@ -991,6 +1014,9 @@ export default {
}
},
computed: {
isPkAvail() {
return this.meta && this.meta.columns.some(c => c.pk)
},
isGallery() {
return this.selectedView && this.selectedView.show_as === 'gallery'
},

30
packages/nc-gui/components/project/spreadsheet/views/xcGridView.vue

@ -27,7 +27,7 @@
@xcresized="resizingCol = null"
>
<!-- :style="columnsWidth[col._cn] ? `min-width:${columnsWidth[col._cn]}; max-width:${columnsWidth[col._cn]}` : ''"
-->
-->
<virtual-header-cell
v-if="col.virtual"
@ -136,13 +136,13 @@
:key="row + columnObj.alias"
class="cell pointer"
:class="{
'active' : !isPublicView && selected.col === col && selected.row === row && isEditable ,
'active' :!isPublicView && selected.col === col && selected.row === row && isEditable ,
'primary-column' : primaryValueColumn === columnObj._cn,
'text-center': isCentrallyAligned(columnObj),
'required': isRequired(columnObj,rowObj)
}"
:data-col="columnObj.alias"
@dblclick="makeEditable(col,row,columnObj.ai)"
@dblclick="makeEditable(col,row,columnObj.ai,rowMeta)"
@click="makeSelected(col,row);"
@contextmenu="showRowContextMenu($event,rowObj,rowMeta,row,col, columnObj)"
>
@ -162,7 +162,8 @@
<editable-cell
v-else-if="
!isLocked
(isPkAvail ||rowMeta.new) &&
!isLocked
&& !isPublicView
&& (editEnabled.col === col && editEnabled.row === row)
|| enableEditable(columnObj)
@ -190,11 +191,11 @@
:db-alias="nodes.dbAlias"
:value="rowObj[columnObj._cn]"
:sql-ui="sqlUi"
@enableedit="makeSelected(col,row);makeEditable(col,row,columnObj.ai)"
@enableedit="makeSelected(col,row);makeEditable(col,row,columnObj.ai, rowMeta)"
/>
</td>
</tr>
<tr v-if="!isLocked && !isPublicView && isEditable && relationType !== 'bt'">
<tr v-if="isPkAvail && !isLocked && !isPublicView && isEditable && relationType !== 'bt'">
<td :colspan="visibleColLength + 1" class="text-left pointer" @click="insertNewRow(true)">
<v-tooltip top>
<template #activator="{on}">
@ -214,7 +215,9 @@
<!-- <div is="style" v-html="resizeColStyle" />-->
<dynamic-style>
<template v-if="resizingCol">
[data-col="{{ resizingCol }}"]{min-width:{{ resizingColWidth }};max-width:{{ resizingColWidth }};width:{{ resizingColWidth }};}
[data-col="{{ resizingCol }}"]{min-width:{{ resizingColWidth }};max-width:{{
resizingColWidth
}};width:{{ resizingColWidth }};}
</template>
</dynamic-style>
</div>
@ -261,7 +264,8 @@ export default {
table: String,
isVirtual: Boolean,
isLocked: Boolean,
columnsWidth: { type: Object }
columnsWidth: { type: Object },
isPkAvail: Boolean
},
data: () => ({
resizingCol: null,
@ -426,6 +430,10 @@ export default {
return
}
if (e.key && e.key.length === 1) {
if (!this.isPkAvail && !this.data[this.selected.row].rowMeta.new) {
return this.$toast.info('Update not allowed for table which doesn\'t have primary Key').goAway(3000)
}
this.$set(this.data[this.selected.row].row, this.availableColumns[this.selected.col]._cn, '')
this.editEnabled = { ...this.selected }
}
@ -466,10 +474,14 @@ export default {
this.editEnabled = {}
}
},
makeEditable(col, row) {
makeEditable(col, row, _, rowMeta) {
if (this.isPublicView || !this.isEditable) {
return
}
if (!this.isPkAvail && !rowMeta.new) {
return this.$toast.info('Update not allowed for table which doesn\'t have primary Key').goAway(3000)
}
if (this.availableColumns[col].ai) {
return this.$toast.info('Auto Increment field is not editable').goAway(3000)
}

Loading…
Cancel
Save