Mutate properly
This commit is contained in:
parent
82c0f6f076
commit
6c70d09fc2
|
@ -5,16 +5,24 @@ import { memo } from "../../../utils"
|
||||||
export const createStores = () => {
|
export const createStores = () => {
|
||||||
const definition = memo(null)
|
const definition = memo(null)
|
||||||
const schemaMutations = memo({})
|
const schemaMutations = memo({})
|
||||||
|
const subSchemaMutations = memo({})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
definition,
|
definition,
|
||||||
schemaMutations,
|
schemaMutations,
|
||||||
|
subSchemaMutations,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const deriveStores = context => {
|
export const deriveStores = context => {
|
||||||
const { API, definition, schemaOverrides, datasource, schemaMutations } =
|
const {
|
||||||
context
|
API,
|
||||||
|
definition,
|
||||||
|
schemaOverrides,
|
||||||
|
datasource,
|
||||||
|
schemaMutations,
|
||||||
|
subSchemaMutations,
|
||||||
|
} = context
|
||||||
|
|
||||||
const schema = derived(definition, $definition => {
|
const schema = derived(definition, $definition => {
|
||||||
let schema = getDatasourceSchema({
|
let schema = getDatasourceSchema({
|
||||||
|
@ -40,8 +48,8 @@ export const deriveStores = context => {
|
||||||
// Derives the total enriched schema, made up of the saved schema and any
|
// Derives the total enriched schema, made up of the saved schema and any
|
||||||
// prop and user overrides
|
// prop and user overrides
|
||||||
const enrichedSchema = derived(
|
const enrichedSchema = derived(
|
||||||
[schema, schemaOverrides, schemaMutations],
|
[schema, schemaOverrides, schemaMutations, subSchemaMutations],
|
||||||
([$schema, $schemaOverrides, $schemaMutations]) => {
|
([$schema, $schemaOverrides, $schemaMutations, $subSchemaMutations]) => {
|
||||||
if (!$schema) {
|
if (!$schema) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
@ -52,6 +60,18 @@ export const deriveStores = context => {
|
||||||
...$schemaOverrides?.[field],
|
...$schemaOverrides?.[field],
|
||||||
...$schemaMutations[field],
|
...$schemaMutations[field],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($subSchemaMutations[field]) {
|
||||||
|
enrichedSchema[field].schema ??= {}
|
||||||
|
for (const [fieldName, mutation] of Object.entries(
|
||||||
|
$subSchemaMutations[field]
|
||||||
|
)) {
|
||||||
|
enrichedSchema[field].schema[fieldName] = {
|
||||||
|
...enrichedSchema[field].schema[fieldName],
|
||||||
|
...mutation,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
return enrichedSchema
|
return enrichedSchema
|
||||||
}
|
}
|
||||||
|
@ -83,6 +103,7 @@ export const createActions = context => {
|
||||||
viewV2,
|
viewV2,
|
||||||
nonPlus,
|
nonPlus,
|
||||||
schemaMutations,
|
schemaMutations,
|
||||||
|
subSchemaMutations,
|
||||||
schema,
|
schema,
|
||||||
notifications,
|
notifications,
|
||||||
} = context
|
} = context
|
||||||
|
@ -151,16 +172,21 @@ export const createActions = context => {
|
||||||
if (!field || !mutation) {
|
if (!field || !mutation) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
schemaMutations.update($schemaMutations => {
|
if (fromNestedField) {
|
||||||
if (fromNestedField) {
|
subSchemaMutations.update($subSchemaMutations => {
|
||||||
const result = { ...$schemaMutations }
|
return {
|
||||||
result[fromNestedField] ??= { schema: {} }
|
...$subSchemaMutations,
|
||||||
result[fromNestedField].schema[field] = {
|
[fromNestedField]: {
|
||||||
...result[fromNestedField].schema[field],
|
...$subSchemaMutations[fromNestedField],
|
||||||
...mutation,
|
[field]: {
|
||||||
|
...($subSchemaMutations[fromNestedField] || {})[field],
|
||||||
|
...mutation,
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
return result
|
})
|
||||||
} else {
|
} else {
|
||||||
|
schemaMutations.update($schemaMutations => {
|
||||||
return {
|
return {
|
||||||
...$schemaMutations,
|
...$schemaMutations,
|
||||||
[field]: {
|
[field]: {
|
||||||
|
@ -168,8 +194,8 @@ export const createActions = context => {
|
||||||
...mutation,
|
...mutation,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adds schema mutations for multiple fields at once
|
// Adds schema mutations for multiple fields at once
|
||||||
|
@ -198,6 +224,7 @@ export const createActions = context => {
|
||||||
}
|
}
|
||||||
const $definition = get(definition)
|
const $definition = get(definition)
|
||||||
const $schemaMutations = get(schemaMutations)
|
const $schemaMutations = get(schemaMutations)
|
||||||
|
const $subSchemaMutations = get(subSchemaMutations)
|
||||||
const $schema = get(schema)
|
const $schema = get(schema)
|
||||||
let newSchema = {}
|
let newSchema = {}
|
||||||
|
|
||||||
|
@ -207,6 +234,17 @@ export const createActions = context => {
|
||||||
...$schema[column],
|
...$schema[column],
|
||||||
...$schemaMutations[column],
|
...$schemaMutations[column],
|
||||||
}
|
}
|
||||||
|
if ($subSchemaMutations[column]) {
|
||||||
|
newSchema[column].schema ??= {}
|
||||||
|
for (const [fieldName, mutation] of Object.entries(
|
||||||
|
$subSchemaMutations[column]
|
||||||
|
)) {
|
||||||
|
newSchema[column].schema[fieldName] = {
|
||||||
|
...newSchema[column].schema[fieldName],
|
||||||
|
...mutation,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Save the changes, then reset our local mutations
|
// Save the changes, then reset our local mutations
|
||||||
|
@ -219,6 +257,7 @@ export const createActions = context => {
|
||||||
|
|
||||||
const resetSchemaMutations = () => {
|
const resetSchemaMutations = () => {
|
||||||
schemaMutations.set({})
|
schemaMutations.set({})
|
||||||
|
subSchemaMutations.set({})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adds a row to the datasource
|
// Adds a row to the datasource
|
||||||
|
|
Loading…
Reference in New Issue