Fix issue creating columns due to 'draft' concept when mutating table schemas

This commit is contained in:
Andrew Kingston 2022-12-19 12:22:07 +00:00
parent 1d648d2e9a
commit 75eb66ecd7
2 changed files with 34 additions and 46 deletions

View File

@ -102,7 +102,7 @@
// in the case of internal tables the sourceId will just be undefined // in the case of internal tables the sourceId will just be undefined
$: tableOptions = $tables.list.filter( $: tableOptions = $tables.list.filter(
opt => opt =>
opt._id !== $tables.draft._id && opt._id !== $tables.selected._id &&
opt.type === table.type && opt.type === table.type &&
table.sourceId === opt.sourceId table.sourceId === opt.sourceId
) )
@ -112,7 +112,7 @@
async function saveColumn() { async function saveColumn() {
if (field.type === AUTO_TYPE) { if (field.type === AUTO_TYPE) {
field = buildAutoColumn($tables.draft.name, field.name, field.subtype) field = buildAutoColumn($tables.selected.name, field.name, field.subtype)
} }
if (field.type !== LINK_TYPE) { if (field.type !== LINK_TYPE) {
delete field.fieldName delete field.fieldName
@ -310,7 +310,7 @@
newError.name = `${PROHIBITED_COLUMN_NAMES.join( newError.name = `${PROHIBITED_COLUMN_NAMES.join(
", " ", "
)} are not allowed as column names` )} are not allowed as column names`
} else if (inUse($tables.draft, fieldInfo.name, originalName)) { } else if (inUse($tables.selected, fieldInfo.name, originalName)) {
newError.name = `Column name already in use.` newError.name = `Column name already in use.`
} }
if (fieldInfo.fieldName && fieldInfo.tableId) { if (fieldInfo.fieldName && fieldInfo.tableId) {

View File

@ -84,55 +84,43 @@ export function createTablesStore() {
primaryDisplay = false, primaryDisplay = false,
indexes, indexes,
}) => { }) => {
let promise let draft = cloneDeep(get(derivedStore).selected)
store.update(state => {
// delete the original if renaming
// need to handle if the column had no name, empty string
if (originalName != null && originalName !== field.name) {
delete state.draft.schema[originalName]
state.draft._rename = {
old: originalName,
updated: field.name,
}
}
// Optionally set display column // delete the original if renaming
if (primaryDisplay) { // need to handle if the column had no name, empty string
state.draft.primaryDisplay = field.name if (originalName != null && originalName !== field.name) {
} else if (state.draft.primaryDisplay === originalName) { delete draft.schema[originalName]
const fields = Object.keys(state.draft.schema) draft._rename = {
// pick another display column randomly if unselecting old: originalName,
state.draft.primaryDisplay = fields.filter( updated: field.name,
name => name !== originalName || name !== field
)[0]
} }
if (indexes) {
state.draft.indexes = indexes
}
state.draft.schema = {
...state.draft.schema,
[field.name]: cloneDeep(field),
}
promise = save(state.draft)
return state
})
if (promise) {
await promise
} }
// Optionally set display column
if (primaryDisplay) {
draft.primaryDisplay = field.name
} else if (draft.primaryDisplay === originalName) {
const fields = Object.keys(draft.schema)
// pick another display column randomly if unselecting
draft.primaryDisplay = fields.filter(
name => name !== originalName || name !== field
)[0]
}
if (indexes) {
draft.indexes = indexes
}
draft.schema = {
...draft.schema,
[field.name]: cloneDeep(field),
}
await save(draft)
} }
const deleteField = async field => { const deleteField = async field => {
let promise let draft = cloneDeep(get(derivedStore).selected)
store.update(state => { delete draft.schema[field.name]
delete state.draft.schema[field.name] await save(draft)
promise = save(state.draft)
return state
})
if (promise) {
await promise
}
} }
return { return {