Skip to content

Commit

Permalink
Merge pull request #376 from NeurodataWithoutBorders/fix-json-update
Browse files Browse the repository at this point in the history
Allow direct updates to deep results when no nested forms exist
  • Loading branch information
CodyCBakerPhD authored Sep 25, 2023
2 parents f86da5d + 5e9a94f commit 7a82dd3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
6 changes: 1 addition & 5 deletions src/renderer/src/stories/FileSystemSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,7 @@ export class FilesystemSelector extends LitElement {
isUpdated = resolved !== this.value;
}

if (isUpdated) {
this.value = resolved;
this.#handleFiles(this.value); // Notify of the change to the separators
return;
}
if (isUpdated) this.#handleFiles(resolved); // Notify of the change to the separators

const resolvedValueDisplay = isArray
? len > 1
Expand Down
14 changes: 7 additions & 7 deletions src/renderer/src/stories/JSONSchemaForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export class JSONSchemaForm extends LitElement {
this.identifier = props.identifier;
this.mode = props.mode ?? "default";
this.schema = props.schema ?? {};
this.results = props.results ?? {};
this.results = (props.base ? structuredClone(props.results) : props.results) ?? {}; // Deep clone results in nested forms
this.globals = props.globals ?? {};

this.ignore = props.ignore ?? [];
Expand Down Expand Up @@ -254,10 +254,10 @@ export class JSONSchemaForm extends LitElement {

// NOTE: Forms with nested forms will handle their own state updates
if (!value) {
if (fullPath.length === 1) delete resultParent[name];
delete resultParent[name];
delete resolvedParent[name];
} else {
if (fullPath.length === 1) resultParent[name] = value;
resultParent[name] = value;
resolvedParent[name] = value;
}

Expand Down Expand Up @@ -297,9 +297,9 @@ export class JSONSchemaForm extends LitElement {
throw new Error(message);
};

validate = async () => {
validate = async (resolved) => {
// Check if any required inputs are missing
const invalidInputs = await this.#validateRequirements(); // get missing required paths
const invalidInputs = await this.#validateRequirements(resolved); // get missing required paths
const isValid = !invalidInputs.length;

// Print out a detailed error message if any inputs are missing
Expand All @@ -316,9 +316,9 @@ export class JSONSchemaForm extends LitElement {

if (message) this.throw(message);

for (let key in this.#nestedForms) await this.#nestedForms[key].validate(); // Validate nested forms too
for (let key in this.#nestedForms) await this.#nestedForms[key].validate(resolved ? resolved[key] : undefined); // Validate nested forms too
try {
for (let key in this.tables) await this.tables[key].validate(); // Validate nested tables too
for (let key in this.tables) await this.tables[key].validate(resolved ? resolved[key] : undefined); // Validate nested tables too
} catch (e) {
this.throw(e.message);
}
Expand Down
7 changes: 6 additions & 1 deletion src/renderer/src/stories/pages/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ export class Page extends LitElement {
this.beforeTransition();

// Otherwise note unsaved updates if present
if (this.unsavedUpdates || ("states" in this.info && !this.info.states.saved)) {
if (
this.unsavedUpdates ||
("states" in this.info &&
transition === 1 && // Only ensure save for standard forward progression
!this.info.states.saved)
) {
if (transition === 1) await this.save(); // Save before a single forward transition
else {
Swal.fire({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { html } from "lit";

import Swal from "sweetalert2";
import { isStorybook } from "../../../../dependencies/globals.js";
import { JSONSchemaForm } from "../../../JSONSchemaForm.js";
Expand Down
18 changes: 11 additions & 7 deletions tests/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ test('inter-table updates are triggered', async () => {
test('changes are resolved correctly', async () => {

const results = {}

const schema = {
properties: {
v0: {
Expand All @@ -148,11 +147,16 @@ test('changes are resolved correctly', async () => {
l2: {
type: "object",
properties: {
v2: {
type: 'string'
}
l3: {
type: "object",
properties: {
v2: {
type: 'string'
}
},
required: ['v2']
},
},
required: ['v2']
},
v1: {
type: 'string'
Expand Down Expand Up @@ -181,13 +185,13 @@ test('changes are resolved correctly', async () => {

const input1 = form.getInput(['v0'])
const input2 = form.getInput(['l1', 'v1'])
const input3 = form.getInput(['l1', 'l2', 'v2'])
const input3 = form.getInput(['l1', 'l2', 'l3', 'v2'])

input1.updateData('test')
input2.updateData('test')
input3.updateData('test')

// Validate that the new structure is correct
await form.validate().then(res => errors = false).catch(e => errors = true)
await form.validate(form.results).then(res => errors = false).catch(e => errors = true)
expect(errors).toBe(false) // Is valid
})

0 comments on commit 7a82dd3

Please sign in to comment.