1
0
Fork 0
mirror of synced 2024-09-20 11:27:56 +12:00

Mutate properly

This commit is contained in:
Adria Navarro 2024-08-21 11:46:36 +02:00
parent 82c0f6f076
commit 6c70d09fc2

View file

@ -5,16 +5,24 @@ import { memo } from "../../../utils"
export const createStores = () => {
const definition = memo(null)
const schemaMutations = memo({})
const subSchemaMutations = memo({})
return {
definition,
schemaMutations,
subSchemaMutations,
}
}
export const deriveStores = context => {
const { API, definition, schemaOverrides, datasource, schemaMutations } =
context
const {
API,
definition,
schemaOverrides,
datasource,
schemaMutations,
subSchemaMutations,
} = context
const schema = derived(definition, $definition => {
let schema = getDatasourceSchema({
@ -40,8 +48,8 @@ export const deriveStores = context => {
// Derives the total enriched schema, made up of the saved schema and any
// prop and user overrides
const enrichedSchema = derived(
[schema, schemaOverrides, schemaMutations],
([$schema, $schemaOverrides, $schemaMutations]) => {
[schema, schemaOverrides, schemaMutations, subSchemaMutations],
([$schema, $schemaOverrides, $schemaMutations, $subSchemaMutations]) => {
if (!$schema) {
return null
}
@ -52,6 +60,18 @@ export const deriveStores = context => {
...$schemaOverrides?.[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
}
@ -83,6 +103,7 @@ export const createActions = context => {
viewV2,
nonPlus,
schemaMutations,
subSchemaMutations,
schema,
notifications,
} = context
@ -151,16 +172,21 @@ export const createActions = context => {
if (!field || !mutation) {
return
}
schemaMutations.update($schemaMutations => {
if (fromNestedField) {
const result = { ...$schemaMutations }
result[fromNestedField] ??= { schema: {} }
result[fromNestedField].schema[field] = {
...result[fromNestedField].schema[field],
...mutation,
if (fromNestedField) {
subSchemaMutations.update($subSchemaMutations => {
return {
...$subSchemaMutations,
[fromNestedField]: {
...$subSchemaMutations[fromNestedField],
[field]: {
...($subSchemaMutations[fromNestedField] || {})[field],
...mutation,
},
},
}
return result
} else {
})
} else {
schemaMutations.update($schemaMutations => {
return {
...$schemaMutations,
[field]: {
@ -168,8 +194,8 @@ export const createActions = context => {
...mutation,
},
}
}
})
})
}
}
// Adds schema mutations for multiple fields at once
@ -198,6 +224,7 @@ export const createActions = context => {
}
const $definition = get(definition)
const $schemaMutations = get(schemaMutations)
const $subSchemaMutations = get(subSchemaMutations)
const $schema = get(schema)
let newSchema = {}
@ -207,6 +234,17 @@ export const createActions = context => {
...$schema[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
@ -219,6 +257,7 @@ export const createActions = context => {
const resetSchemaMutations = () => {
schemaMutations.set({})
subSchemaMutations.set({})
}
// Adds a row to the datasource