Skip to content

Commit

Permalink
Better way of recalling parameters and making sure the local storage …
Browse files Browse the repository at this point in the history
…is cleared between samples
  • Loading branch information
marcus-oscarsson committed Sep 1, 2023
1 parent d15eabe commit b371a0a
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 21 deletions.
2 changes: 2 additions & 0 deletions ui/src/actions/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { loadSample } from './sampleChanger';
import { sendAbortCentring, sendUpdateShapes } from './sampleview';
import { selectSamplesAction, clearSampleGrid } from './sampleGrid';
import { TASK_UNCOLLECTED } from '../constants';
import { clearAllLastUserParameters } from '../components/Tasks/fields';

export function queueLoading(loading) {
return { type: 'QUEUE_LOADING', loading };
Expand All @@ -22,6 +23,7 @@ export function setQueueAction(queue) {
}

export function setCurrentSample(sampleID) {
clearAllLastUserParameters();
return {
type: 'SET_CURRENT_SAMPLE',
sampleID,
Expand Down
6 changes: 5 additions & 1 deletion ui/src/components/SampleView/ContextMenu.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { Dropdown } from 'react-bootstrap';
import { getLastUsedParameters } from '../Tasks/fields';

export default class ContextMenu extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -324,8 +325,11 @@ export default class ContextMenu extends React.Component {
modalName === 'Generic'
? defaultParameters[type].name
: modalName.toLowerCase();
const params =
let params =
type in defaultParameters ? defaultParameters[type].acq_parameters : {};

params = getLastUsedParameters(type, params);

this.props.showForm(
modalName,
[sampleID],
Expand Down
2 changes: 1 addition & 1 deletion ui/src/components/SampleView/shapes.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ export function makeDistanceLine(p1, p2, iR, ppMm, color, width) {
const length = Number.parseInt(Math.sqrt(a * a + b * b) * 1000, 10);
return [
makeLine(p1.x * iR, p1.y * iR, p2.x * iR, p2.y * iR, color, width, false),
makeText(p2.x * iR, p2.y * iR, 12, color, `${length} µm`),
makeText(p2.x * iR, p2.y * iR, 14, color, `${length} µm`),
];
}

Expand Down
4 changes: 1 addition & 3 deletions ui/src/components/Tasks/Characterisation.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
FieldsRow,
CollapsableRows,
toFixed,
getLastUsedParameters,
saveToLastUsedParameters,
resetLastUsedParameters,
} from './fields';
Expand Down Expand Up @@ -491,7 +490,6 @@ Characterisation = connect((state) => {

const { type } = state.taskForm.taskData;
const { limits } = state.taskForm.defaultParameters[type.toLowerCase()];
const parameters = getLastUsedParameters(type, state);

return {
path: `${state.login.rootPath}/${subdir}`,
Expand All @@ -506,7 +504,7 @@ Characterisation = connect((state) => {
use_min_dose: selector(state, 'use_min_dose'),
use_min_time: selector(state, 'use_min_time'),
initialValues: {
...parameters,
...state.taskForm.taskData.parameters,
beam_size: state.sampleview.currentAperture,
resolution:
state.taskForm.sampleIds.constructor !== Array
Expand Down
4 changes: 1 addition & 3 deletions ui/src/components/Tasks/DataCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
FieldsRow,
CollapsableRows,
toFixed,
getLastUsedParameters,
saveToLastUsedParameters,
resetLastUsedParameters,
} from './fields';
Expand Down Expand Up @@ -366,15 +365,14 @@ DataCollection = connect((state) => {

const { type } = state.taskForm.taskData;
const { limits } = state.taskForm.defaultParameters[type.toLowerCase()];
const parameters = getLastUsedParameters(type, state);

return {
path: `${state.login.rootPath}/${subdir}`,
filename: fname,
acqParametersLimits: limits,
beamline: state.beamline,
initialValues: {
...parameters,
...state.taskForm.taskData.parameters,
beam_size: state.sampleview.currentAperture,
resolution:
state.taskForm.sampleIds.constructor !== Array
Expand Down
4 changes: 1 addition & 3 deletions ui/src/components/Tasks/Helical.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
FieldsRow,
CollapsableRows,
toFixed,
getLastUsedParameters,
saveToLastUsedParameters,
resetLastUsedParameters,
} from './fields';
Expand Down Expand Up @@ -240,15 +239,14 @@ Helical = connect((state) => {

const { type } = state.taskForm.taskData;
const { limits } = state.taskForm.defaultParameters[type.toLowerCase()];
const parameters = getLastUsedParameters(type, state);

return {
path: `${state.login.rootPath}/${subdir}`,
filename: fname,
acqParametersLimits: limits,
beamline: state.beamline,
initialValues: {
...parameters,
...state.taskForm.taskData.parameters,
beam_size: state.sampleview.currentAperture,
resolution:
state.taskForm.sampleIds.constructor !== Array
Expand Down
4 changes: 1 addition & 3 deletions ui/src/components/Tasks/Mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
FieldsRow,
CollapsableRows,
DisplayField,
getLastUsedParameters,
saveToLastUsedParameters,
resetLastUsedParameters,
} from './fields';
Expand Down Expand Up @@ -261,15 +260,14 @@ Mesh = connect((state) => {

const { type } = state.taskForm.taskData;
const { limits } = state.taskForm.defaultParameters[type.toLowerCase()];
const parameters = getLastUsedParameters(type, state);

return {
path: `${state.login.rootPath}/${subdir}`,
filename: fname,
acqParametersLimits: limits,
beamline: state.beamline,
initialValues: {
...parameters,
...state.taskForm.taskData.parameters,
beam_size: state.sampleview.currentAperture,
resolution: state.taskForm.taskData.sampleID
? state.taskForm.taskData.parameters.resolution
Expand Down
26 changes: 19 additions & 7 deletions ui/src/components/Tasks/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import { TiWarning, TiTimes } from 'react-icons/ti';

import './style.css';

export function getLastUsedParameters(formName, state) {
const lastParameters = localStorage.getItem(`last${formName}Parameters`);
let parameters = state.taskForm.taskData.parameters;
export function getLastUsedParameters(type, parameters) {
const lastParameters = localStorage.getItem(`last${type}Parameters`);

if (lastParameters !== null) {
parameters = JSON.parse(lastParameters);
Expand All @@ -16,7 +15,7 @@ export function getLastUsedParameters(formName, state) {
if (parseFloat(parameters.osc_range) === 0) {
parameters.osc_range =
state.taskForm.defaultParameters[
formName.toLowerCase()
type.toLowerCase()
].acq_parameters.osc_range;
}

Expand All @@ -27,6 +26,19 @@ export function saveToLastUsedParameters(formName, parameters) {
localStorage.setItem(`last${formName}Parameters`, JSON.stringify(parameters));
}

export function clearAllLastUserParameters() {
const collectionNames = [
'datacollection',
'characterisation',
'mesh',
'helical',
];

collectionNames.forEach((type) => {
localStorage.removeItem(`last${type}Parameters`);
});
}

export function resetLastUsedParameters(formObj) {
const { type } = formObj.props.taskData;
localStorage.removeItem(`last${type}Parameters`);
Expand All @@ -36,16 +48,16 @@ export function resetLastUsedParameters(formObj) {
formObj.props.defaultParameters[type.toLowerCase()].acq_parameters,
);

const ignore = [
const ignore = new Set([
'osc_start',
'resolution',
'energy',
'transmission',
'beam_size',
];
]);

fieldNames.forEach((field) => {
if (!ignore.includes(field)) {
if (!ignore.has(field)) {
formObj.props.autofill(
field,
formObj.props.defaultParameters[type.toLowerCase()].acq_parameters[
Expand Down

0 comments on commit b371a0a

Please sign in to comment.