1
0
Fork 0
mirror of synced 2024-09-20 19:33:10 +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 = () => { 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) {
const result = { ...$schemaMutations } subSchemaMutations.update($subSchemaMutations => {
result[fromNestedField] ??= { schema: {} } return {
result[fromNestedField].schema[field] = { ...$subSchemaMutations,
...result[fromNestedField].schema[field], [fromNestedField]: {
...$subSchemaMutations[fromNestedField],
[field]: {
...($subSchemaMutations[fromNestedField] || {})[field],
...mutation, ...mutation,
},
},
} }
return result })
} else { } else {
schemaMutations.update($schemaMutations => {
return { return {
...$schemaMutations, ...$schemaMutations,
[field]: { [field]: {
@ -168,9 +194,9 @@ export const createActions = context => {
...mutation, ...mutation,
}, },
} }
}
}) })
} }
}
// Adds schema mutations for multiple fields at once // Adds schema mutations for multiple fields at once
const addSchemaMutations = mutations => { const addSchemaMutations = mutations => {
@ -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