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() await this.reload()
} else if (Object.keys(updatedObj).length) { } 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) await this.api.update(id, updatedObj, this.oldRow)
} else { } else {
return this.$toast.info('No columns to update').goAway(3000) 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" /> <v-spacer class="h-100" @dblclick="debug=true" />
<debug-metas v-if="debug" class="mr-3" /> <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" /> <lock-menu v-if="_isUIAllowed('view-type')" v-model="viewStatus.type" />
<x-btn tooltip="Reload view data" outlined small text @click="reload"> <x-btn tooltip="Reload view data" outlined small text @click="reload">
<v-icon small class="mr-1" color="grey darken-3"> <v-icon small class="mr-1" color="grey darken-3">
@ -208,6 +216,7 @@
:meta="meta" :meta="meta"
:is-virtual="selectedView.type === 'vtable'" :is-virtual="selectedView.type === 'vtable'"
:api="api" :api="api"
:is-pk-avail="isPkAvail"
@onNewColCreation="onNewColCreation" @onNewColCreation="onNewColCreation"
@onCellValueChange="onCellValueChange" @onCellValueChange="onCellValueChange"
@insertNewRow="insertNewRow" @insertNewRow="insertNewRow"
@ -631,8 +640,8 @@ export default {
if ( if (
!this.meta || ( !this.meta || (
(this.meta.hasMany && this.meta.hasMany.length) || (this.meta.hasMany && this.meta.hasMany.length) ||
(this.meta.manyToMany && this.meta.manyToMany.length) || (this.meta.manyToMany && this.meta.manyToMany.length) ||
(this.meta.belongsTo && this.meta.belongsTo.length)) (this.meta.belongsTo && this.meta.belongsTo.length))
) { ) {
return this.$toast.info('Please delete relations before deleting table.').goAway(3000) 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('___') 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, { const newData = await this.api.update(id, {
[column._cn]: rowObj[column._cn] [column._cn]: rowObj[column._cn]
}, { [column._cn]: oldRow[column._cn] }) }, { [column._cn]: oldRow[column._cn] })
@ -841,6 +854,11 @@ export default {
const rowObj = this.rowContextMenu.row const rowObj = this.rowContextMenu.row
if (!this.rowContextMenu.rowMeta.new) { 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('___') 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) await this.api.delete(id)
} }
this.data.splice(this.rowContextMenu.index, 1) this.data.splice(this.rowContextMenu.index, 1)
@ -859,6 +877,11 @@ export default {
} }
if (!rowMeta.new) { if (!rowMeta.new) {
const id = this.meta.columns.filter(c => c.pk).map(c => rowObj[c._cn]).join('___') 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) await this.api.delete(id)
} }
this.data.splice(row, 1) this.data.splice(row, 1)
@ -991,6 +1014,9 @@ export default {
} }
}, },
computed: { computed: {
isPkAvail() {
return this.meta && this.meta.columns.some(c => c.pk)
},
isGallery() { isGallery() {
return this.selectedView && this.selectedView.show_as === 'gallery' 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" @xcresized="resizingCol = null"
> >
<!-- :style="columnsWidth[col._cn] ? `min-width:${columnsWidth[col._cn]}; max-width:${columnsWidth[col._cn]}` : ''" <!-- :style="columnsWidth[col._cn] ? `min-width:${columnsWidth[col._cn]}; max-width:${columnsWidth[col._cn]}` : ''"
--> -->
<virtual-header-cell <virtual-header-cell
v-if="col.virtual" v-if="col.virtual"
@ -136,13 +136,13 @@
:key="row + columnObj.alias" :key="row + columnObj.alias"
class="cell pointer" class="cell pointer"
:class="{ :class="{
'active' : !isPublicView && selected.col === col && selected.row === row && isEditable , 'active' :!isPublicView && selected.col === col && selected.row === row && isEditable ,
'primary-column' : primaryValueColumn === columnObj._cn, 'primary-column' : primaryValueColumn === columnObj._cn,
'text-center': isCentrallyAligned(columnObj), 'text-center': isCentrallyAligned(columnObj),
'required': isRequired(columnObj,rowObj) 'required': isRequired(columnObj,rowObj)
}" }"
:data-col="columnObj.alias" :data-col="columnObj.alias"
@dblclick="makeEditable(col,row,columnObj.ai)" @dblclick="makeEditable(col,row,columnObj.ai,rowMeta)"
@click="makeSelected(col,row);" @click="makeSelected(col,row);"
@contextmenu="showRowContextMenu($event,rowObj,rowMeta,row,col, columnObj)" @contextmenu="showRowContextMenu($event,rowObj,rowMeta,row,col, columnObj)"
> >
@ -162,7 +162,8 @@
<editable-cell <editable-cell
v-else-if=" v-else-if="
!isLocked (isPkAvail ||rowMeta.new) &&
!isLocked
&& !isPublicView && !isPublicView
&& (editEnabled.col === col && editEnabled.row === row) && (editEnabled.col === col && editEnabled.row === row)
|| enableEditable(columnObj) || enableEditable(columnObj)
@ -190,11 +191,11 @@
:db-alias="nodes.dbAlias" :db-alias="nodes.dbAlias"
:value="rowObj[columnObj._cn]" :value="rowObj[columnObj._cn]"
:sql-ui="sqlUi" :sql-ui="sqlUi"
@enableedit="makeSelected(col,row);makeEditable(col,row,columnObj.ai)" @enableedit="makeSelected(col,row);makeEditable(col,row,columnObj.ai, rowMeta)"
/> />
</td> </td>
</tr> </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)"> <td :colspan="visibleColLength + 1" class="text-left pointer" @click="insertNewRow(true)">
<v-tooltip top> <v-tooltip top>
<template #activator="{on}"> <template #activator="{on}">
@ -214,7 +215,9 @@
<!-- <div is="style" v-html="resizeColStyle" />--> <!-- <div is="style" v-html="resizeColStyle" />-->
<dynamic-style> <dynamic-style>
<template v-if="resizingCol"> <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> </template>
</dynamic-style> </dynamic-style>
</div> </div>
@ -261,7 +264,8 @@ export default {
table: String, table: String,
isVirtual: Boolean, isVirtual: Boolean,
isLocked: Boolean, isLocked: Boolean,
columnsWidth: { type: Object } columnsWidth: { type: Object },
isPkAvail: Boolean
}, },
data: () => ({ data: () => ({
resizingCol: null, resizingCol: null,
@ -426,6 +430,10 @@ export default {
return return
} }
if (e.key && e.key.length === 1) { 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.$set(this.data[this.selected.row].row, this.availableColumns[this.selected.col]._cn, '')
this.editEnabled = { ...this.selected } this.editEnabled = { ...this.selected }
} }
@ -466,10 +474,14 @@ export default {
this.editEnabled = {} this.editEnabled = {}
} }
}, },
makeEditable(col, row) { makeEditable(col, row, _, rowMeta) {
if (this.isPublicView || !this.isEditable) { if (this.isPublicView || !this.isEditable) {
return 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) { if (this.availableColumns[col].ai) {
return this.$toast.info('Auto Increment field is not editable').goAway(3000) return this.$toast.info('Auto Increment field is not editable').goAway(3000)
} }

Loading…
Cancel
Save