Skip to content

Commit

Permalink
Add column for solvents
Browse files Browse the repository at this point in the history
  • Loading branch information
JanCBrammer committed Sep 4, 2023
1 parent c23b32f commit c4763bf
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 15 deletions.
5 changes: 5 additions & 0 deletions app/api/entities/reaction_variation_entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class ReactionVariationEntity < ApplicationEntity
:properties,
:reactants,
:products,
:solvents,
)
expose :starting_materials, as: :startingMaterials

Expand Down Expand Up @@ -36,6 +37,10 @@ def reactants
def products
materials(:products)
end

def solvents
materials(:solvents)
end
end

class ReactionVariationPropertyEntity < ApplicationEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from 'react-bootstrap';
import _ from 'lodash';
import {
createVariationsRow, temperatureUnits, durationUnits, amountUnits, convertUnit
createVariationsRow, temperatureUnits, durationUnits, massUnits, volumeUnits, convertUnit
} from 'src/apps/mydb/elements/details/reactions/variationsTab/ReactionVariationsUtils';

function RowToolsCellRenderer({ data, copyRow, removeRow }) {
Expand Down Expand Up @@ -223,7 +223,19 @@ export default function ReactionVariations({ reaction, onEditVariations }) {
headerName: getMaterialHeaderIdentifier(material, materialHeaderIdentifier)
})
)
}
},
{
headerName: 'Solvents',
groupId: 'Solvents',
marryChildren: true,
children: reaction.solvents.map(
(material) => ({
field: `solvents.${material.id}`,
headerName: getMaterialHeaderIdentifier(material, materialHeaderIdentifier),
cellEditorParams: { unitOptions: volumeUnits }
})
)
},
];

return (
Expand Down Expand Up @@ -269,7 +281,7 @@ export default function ReactionVariations({ reaction, onEditVariations }) {
comparator: cellComparator,
cellEditor: CellEditor,
cellEditorPopup: true,
cellEditorParams: { unitOptions: amountUnits },
cellEditorParams: { unitOptions: massUnits },
cellRenderer: CellRenderer,
wrapHeaderText: true,
autoHeaderHeight: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ 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'];
const massUnits = ['mg', 'g', 'μg'];
const volumeUnits = ['ml', 'l', 'μl'];

function convertUnit(value, fromUnit, toUnit) {
if (temperatureUnits.includes(fromUnit) && temperatureUnits.includes(toUnit)) {
Expand All @@ -13,16 +14,21 @@ function convertUnit(value, fromUnit, toUnit) {
if (durationUnits.includes(fromUnit) && durationUnits.includes(toUnit)) {
return convertDuration(value, fromUnit, toUnit);
}
if (amountUnits.includes(fromUnit) && amountUnits.includes(toUnit)) {
if (massUnits.includes(fromUnit) && massUnits.includes(toUnit)) {
const amountUnitPrefixes = { g: 'n', mg: 'm', μg: 'u' };
return convertAmount(value, amountUnitPrefixes[fromUnit], amountUnitPrefixes[toUnit]);
}
if (volumeUnits.includes(fromUnit) && volumeUnits.includes(toUnit)) {
const amountUnitPrefixes = { l: 'n', ml: 'm', μl: 'u' };
return convertAmount(value, amountUnitPrefixes[fromUnit], amountUnitPrefixes[toUnit]);
}

return value;
}

function getMaterialData(material) {
const value = material.amount_g ?? null;
const unit = amountUnits[0];
const value = material.amount_value ?? null;
const unit = material.amount_unit ?? null;
const aux = {
coefficient: material.coefficient ?? null,
isReference: material.reference ?? false,
Expand Down Expand Up @@ -70,6 +76,8 @@ function createVariationsRow(reaction, id) {
reactants: reaction.reactants.reduce((a, v) => (
{ ...a, [v.id]: getMaterialData(v) }), {}),
products: reaction.products.reduce((a, v) => (
{ ...a, [v.id]: getMaterialData(v) }), {}),
solvents: reaction.solvents.reduce((a, v) => (
{ ...a, [v.id]: getMaterialData(v) }), {})
};
return row;
Expand All @@ -78,7 +86,7 @@ function createVariationsRow(reaction, id) {
function removeObsoleteMaterialsFromVariations(variations, currentMaterials) {
const updatedVariations = cloneDeep(variations);
updatedVariations.forEach((row) => {
['startingMaterials', 'reactants', 'products'].forEach((materialType) => {
['startingMaterials', 'reactants', 'products', 'solvents'].forEach((materialType) => {
Object.keys(row[materialType]).forEach((materialName) => {
if (!currentMaterials[materialType].map((material) => material.id.toString()).includes(materialName)) {
delete row[materialType][materialName];
Expand All @@ -92,7 +100,7 @@ function removeObsoleteMaterialsFromVariations(variations, currentMaterials) {
function addMissingMaterialsToVariations(variations, currentMaterials) {
const updatedVariations = cloneDeep(variations);
updatedVariations.forEach((row) => {
['startingMaterials', 'reactants', 'products'].forEach((materialType) => {
['startingMaterials', 'reactants', 'products', 'solvents'].forEach((materialType) => {
currentMaterials[materialType].forEach((material) => {
if (!(material.id in row[materialType])) {
row[materialType][material.id] = getMaterialData(material);
Expand Down Expand Up @@ -137,6 +145,7 @@ export {
computeYield,
temperatureUnits,
durationUnits,
amountUnits,
massUnits,
volumeUnits,
convertUnit
};
10 changes: 8 additions & 2 deletions app/packs/src/models/Reaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,12 @@ export default class Reaction extends Element {
// <material_id: {"value": <number>, "unit": <string>, "aux": {...}},
// <material_id>: {"value": <number>, "unit": <string>, "aux": {...}},
// ...
// }
// },
// "solvents": {
// <material_id: {"value": <number>, "unit": <string>, "aux": {...}},
// <material_id>: {"value": <number>, "unit": <string>, "aux": {...}},
// ...
// },
// },
// {
// "id": "<number>",
Expand All @@ -239,7 +244,8 @@ export default class Reaction extends Element {
const currentMaterials = {
startingMaterials: this.starting_materials,
reactants: this.reactants,
products: this.products
products: this.products,
solvents: this.solvents
};
// Keep materials up-to-date. Materials could have been added or removed in the scheme tab
// (of the reaction detail modal); these changes need to be reflected in the variations.
Expand Down
1 change: 1 addition & 0 deletions lib/reporter/docx/detail_reaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def variations
'duration' => "#{var[:properties][:duration][:value]} #{var[:properties][:duration][:unit]}",
'startingMaterials' => variation_materials(var, :startingMaterials),
'reactants' => variation_materials(var, :reactants),
'solvents' => variation_materials(var, :solvents),
'products' => variation_products(var),
}
end
Expand Down
Binary file modified lib/template/Standard.docx
Binary file not shown.
6 changes: 4 additions & 2 deletions spec/api/entities/reaction_entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@
isReference: false,
molecularWeight: 700.9154799999998 },
unit: 'g',
value: nil } } },
value: nil } },
solvents: {} },
{ id: 'd0a7087f-bf1b-479a-b684-783b004ff588',
products: { '47': { aux: { yield: '0',
purity: 0.29,
Expand Down Expand Up @@ -149,7 +150,8 @@
isReference: false,
molecularWeight: 700.9154799999998 },
unit: 'g',
value: nil } } }],
value: nil } },
solvents: {} }],
)
end

Expand Down
3 changes: 2 additions & 1 deletion spec/javascripts/packs/src/models/Reaction.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ function getReactionMaterials(reaction) {
return {
startingMaterials: reaction.starting_materials,
reactants: reaction.reactants,
products: reaction.products
products: reaction.products,
solvents: reaction.solvents
};
}

Expand Down

0 comments on commit c4763bf

Please sign in to comment.