Skip to content

Commit

Permalink
[TaskDialog] - Fix for JSForm getting cleard when store update
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-oscarsson committed Jun 21, 2024
1 parent afcc165 commit f1bf940
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
42 changes: 39 additions & 3 deletions ui/src/components/Tasks/GenericTaskForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ class GenericTaskForm extends React.Component {
this.props.handleSubmit(this.addToQueue.bind(this, true))();
}

get jsFormStorageKey() {
return `current${this.props.taskData.type}Parameters`;
}

clearCurrentJSFormParameters() {
localStorage.removeItem(this.jsFormStorageKey);
}

saveCurrentJSFormParameters(formData) {
localStorage.setItem(this.jsFormStorageKey, JSON.stringify(formData));
}

addToQueue(runNow, params) {
const parameters = {
...params,
Expand Down Expand Up @@ -71,12 +83,16 @@ class GenericTaskForm extends React.Component {
'chip_type',
];

saveToLastUsedParameters(this.props.taskData.type, parameters);
saveToLastUsedParameters(this.props.taskData.type, parameters, [
'selection',
]);
this.clearCurrentJSFormParameters();
this.props.addTask(parameters, stringFields, runNow);
this.props.hide();
}

defaultParameters() {
this.clearCurrentJSFormParameters();
resetLastUsedParameters(this);
}

Expand Down Expand Up @@ -167,6 +183,18 @@ class GenericTaskForm extends React.Component {
}
}

const currentFormData = JSON.parse(
localStorage.getItem(this.jsFormStorageKey),
);

if (currentFormData) {
for (const key in currentFormData) {
if (s.properties[key]) {
s.properties[key].default = currentFormData[key];
}
}
}

for (const key in this.props.taskData.Arraylimits) {
if (s.properties[key]) {
s.properties[key].exclusiveMinimum = this.props.taskData.limits[key][0];
Expand Down Expand Up @@ -244,10 +272,17 @@ class GenericTaskForm extends React.Component {
const schema = this.setConstraintsFromDefualts(
this.props.schema.user_collection_parameters,
);

return (
<DraggableModal show={this.props.show} onHide={this.props.hide}>
<DraggableModal
show={this.props.show}
onHide={() => {
this.clearCurrentJSFormParameters();
this.props.hide();
}}
>
<Modal.Header closeButton>
<Modal.Title>{this.props.taskData.name}</Modal.Title>
<Modal.Title>{this.props.taskData.parameters.name}</Modal.Title>
</Modal.Header>
<Modal.Body>
<Form>
Expand Down Expand Up @@ -306,6 +341,7 @@ class GenericTaskForm extends React.Component {
uiSchema={uiSchema}
onChange={({ formData }) => {
this.updateFromRemoteValidation(formData);
this.saveCurrentJSFormParameters(formData);
this.jsformData = formData;
}}
templates={{
Expand Down
10 changes: 8 additions & 2 deletions ui/src/components/Tasks/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ export function getLastUsedParameters(type, newParams) {
return lastParameters === null ? newParams : JSON.parse(lastParameters);
}

export function saveToLastUsedParameters(formName, parameters) {
localStorage.setItem(`last${formName}Parameters`, JSON.stringify(parameters));
export function saveToLastUsedParameters(formName, parameters, exclude = []) {
const params = { ...parameters };

exclude.forEach((paramName) => {
delete params[paramName];
});

localStorage.setItem(`last${formName}Parameters`, JSON.stringify(params));
}

export function clearAllLastUsedParameters() {
Expand Down
1 change: 1 addition & 0 deletions ui/src/containers/TaskContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ class TaskContainer extends React.Component {
apertureList={this.props.apertureList}
availableElements={this.props.beamline.energyScanElements}
rootPath={this.props.path}
defaultParameters={this.props.defaultParameters}
/>
);
}
Expand Down

0 comments on commit f1bf940

Please sign in to comment.