Skip to content

Commit

Permalink
WIP: Make units configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
JanCBrammer committed Sep 3, 2023
1 parent 88ba833 commit 9369a0f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {
} from 'react-bootstrap';
import _ from 'lodash';
import { createVariationsRow } from 'src/apps/mydb/elements/details/reactions/variationsTab/ReactionVariationsUtils';
import {
temperatureUnits, durationUnits, amountUnits
} from 'src/apps/mydb/elements/details/reactions/variationsTab/ReactionVariationsUtils';

function RowToolsCellRenderer({ data, copyRow, removeRow }) {
return (
Expand Down Expand Up @@ -180,12 +183,12 @@ export default function ReactionVariations({ reaction, onEditVariations }) {
{
headerName: 'Temperature',
field: 'properties.temperature',
cellEditorParams: { unitOptions: ['°C', '°F', 'K'] },
cellEditorParams: { unitOptions: temperatureUnits },
},
{
headerName: 'Duration',
field: 'properties.duration',
cellEditorParams: { unitOptions: ['Second(s)', 'Minute(s)', 'Hour(s)', 'Day(s)', 'Week(s)'] },
cellEditorParams: { unitOptions: durationUnits },
},
]
},
Expand Down Expand Up @@ -267,7 +270,7 @@ export default function ReactionVariations({ reaction, onEditVariations }) {
comparator: cellComparator,
cellEditor: CellEditor,
cellEditorPopup: true,
cellEditorParams: { unitOptions: ['g', 'mg', 'μg'] },
cellEditorParams: { unitOptions: amountUnits },
cellRenderer: CellRenderer,
wrapHeaderText: true,
autoHeaderHeight: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
import cloneDeep from 'lodash/cloneDeep';
import { convertTemperature, convertDuration } from 'src/models/Reaction';
import { metPreConv as convertAmount } from 'src/utilities/metricPrefix';

const temperatureUnits = ['°C', 'K', '°F'];
const durationUnits = ['Second(s)', 'Minute(s)', 'Hour(s)', 'Day(s)', 'Week(s)'];
const amountUnits = ['g', 'mg', 'μg'];

function convertUnit(value, fromUnit, toUnit) {
if (temperatureUnits.includes(fromUnit) && temperatureUnits.includes(toUnit)) {
return convertTemperature(value, fromUnit, toUnit);
}
if (durationUnits.includes(fromUnit) && durationUnits.includes(toUnit)) {
return convertDuration(value, fromUnit, toUnit);
}
if (amountUnits.includes(fromUnit) && amountUnits.includes(toUnit)) {
const amountUnitPrefixes = { g: 'n', mg: 'm', μg: 'u' };
return convertAmount(value, amountUnitPrefixes[fromUnit], amountUnitPrefixes[toUnit]);
}
return value;
}

function getMaterialData(material) {
const value = material.amount_g ?? null;
const unit = 'g';
const unit = amountUnits[0];
const aux = {
coefficient: material.coefficient ?? null,
isReference: material.reference ?? false,
Expand All @@ -16,21 +36,8 @@ function getMaterialData(material) {
return { value, unit, aux };
}

function getAmountInGram(material) {
switch (material.unit) {
case 'g':
return material.value;
case 'mg':
return material.value / 1e4;
case 'μg':
return material.value / 1e6;
default:
return null;
}
}

function getMolFromGram(material) {
const gram = getAmountInGram(material);
const gram = convertUnit(material.value, material.unit, 'g');

if (material.aux.loading) {
return (material.aux.loading * gram) / 1e4;
Expand Down Expand Up @@ -127,5 +134,9 @@ export {
createVariationsRow,
removeObsoleteMaterialsFromVariations,
addMissingMaterialsToVariations,
computeYield
computeYield,
temperatureUnits,
durationUnits,
amountUnits,
convertUnit
};

0 comments on commit 9369a0f

Please sign in to comment.