Skip to content

Commit

Permalink
Fix some fields and translations for device descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
beque committed Jan 27, 2025
1 parent e63f0cf commit a0b387b
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 18 deletions.
1 change: 1 addition & 0 deletions app/api/chemotion/device_description_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class DeviceDescriptionAPI < Grape::API
optional :access_comments, type: String
optional :size, type: String
optional :weight, type: String
optional :weight_unit, type: String
optional :application_name, type: String
optional :application_version, type: String
optional :vendor_url, type: String
Expand Down
1 change: 1 addition & 0 deletions app/api/entities/device_description_entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class DeviceDescriptionEntity < ApplicationEntity
expose :access_comments
expose :size
expose :weight
expose :weight_unit
expose :application_name
expose :application_version
expose :vendor_url
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,8 @@ const DeviceDescriptionDetails = ({ toggleFullScreen }) => {
}

visibleTabs.forEach((key, i) => {
let title = key.charAt(0).toUpperCase() + key.slice(1);

tabContents.push(
<Tab eventKey={key} title={title} key={`${key}_${deviceDescription.id}`} disabled={disabled(i)}>
<Tab eventKey={key} title={tabTitles[key]} key={`${key}_${deviceDescription.id}`} disabled={disabled(i)}>
{
!deviceDescription.isNew &&
<CommentSection section={`device_description_${key}`} element={deviceDescription} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import DatePicker from 'react-datepicker';
import { useDrop } from 'react-dnd';
import { DragDropItemTypes } from 'src/utilities/DndConst';
import ChevronIcon from 'src/components/common/ChevronIcon';
import { handleFloatNumbers } from 'src/utilities/UnitsConversion';
import moment from 'moment';
import { v4 as uuid } from 'uuid';

Expand Down Expand Up @@ -79,6 +80,35 @@ const handleFieldChanged = (store, field, type, element_type) => (event) => {
}
}

const weightConversion = (value, multiplier) => value * multiplier;

const weightConversionMap = {
t: { convertedUnit: 'kg', conversionFactor: 1000 },
kg: { convertedUnit: 'g', conversionFactor: 1000 },
g: { convertedUnit: 't', conversionFactor: 0.000001 },
};

const convertByUnit = (valueToFormat, currentUnit) => {
const { convertedUnit, conversionFactor } = weightConversionMap[currentUnit];
const decimalPlaces = 7;
const formattedValue = weightConversion(valueToFormat, conversionFactor);
const convertedValue = handleFloatNumbers(formattedValue, decimalPlaces);
return [convertedValue, convertedUnit];
};

const changeWeightUnit = (element, store, field, field_unit, element_type) => {
const oldValue = element[field];
const oldUnitValue = element[field_unit] || 'kg';
const [newValue, newUnitValue] = convertByUnit(oldValue, oldUnitValue);

if (element_type == 'device_description') {
if (newValue !== 0) {
store.changeDeviceDescription(field, newValue);
}
store.changeDeviceDescription(field_unit, newUnitValue);
}
}

const toggleContent = (store, content) => {
store.toggleContent(content);
}
Expand Down Expand Up @@ -482,9 +512,43 @@ const checkboxInput = (element, label, field, store) => {
);
}

const inputGroupWithWeightUnit = (element, store, field, field_unit, label, info) => {
let value = elementFieldValue(element, store, field);

return (
<Form.Group>
{labelWithInfo(label, info)}
<InputGroup>
<Form.Control
name={field}
type="text"
key={`${store.key_prefix}-${field}`}
value={value}
onChange={handleFieldChanged(store, field, 'text', element.type)}
/>
<Button
variant="success"
onClick={() => changeWeightUnit(element, store, field, field_unit, element.type)}
>
{element[field_unit] || 'kg'}
</Button>
</InputGroup>
</Form.Group>
);
}

const identifierMultipleInputGroups = (element, label, options, store, info) => {
let formGroupKey = 'version_identifier_type_doi_url';
let idOrNew = element.id !== '' ? element.id : 'new';
let link = '';

if (element.version_doi_url) {
link = (
<div className="pt-2">
<a href={element.version_doi_url}>{element.version_doi_url}</a>
</div>
);
}

return (
<Form.Group key={`${store.key_prefix}-${idOrNew}-${formGroupKey}`}>
Expand All @@ -509,6 +573,7 @@ const identifierMultipleInputGroups = (element, label, options, store, info) =>
onChange={handleFieldChanged(store, 'version_doi_url', 'text', element.type)}
/>
</InputGroup>
{link}
</Form.Group>
);
}
Expand Down Expand Up @@ -682,5 +747,5 @@ export {
selectInput, multiSelectInput, textInput, multipleInputGroups,
textareaInput, dateTimePickerInput, headlineWithToggle,
mulipleRowInput, annotationButton, checkboxInput, componentInput,
identifierMultipleInputGroups, toggleContent,
identifierMultipleInputGroups, toggleContent, inputGroupWithWeightUnit
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
selectInput, multiSelectInput, textInput, multipleInputGroups,
textareaInput, dateTimePickerInput, mulipleRowInput, toggleContent,
checkboxInput, componentInput, identifierMultipleInputGroups,
inputGroupWithWeightUnit,
} from '../FormFields';

import { observer } from 'mobx-react';
Expand Down Expand Up @@ -135,7 +136,7 @@ const PropertiesForm = () => {
const versionDoiLabel = 'Persistent identifier';

const location = [
{ value: 'university_campus', label: 'University - Campus', type: 'text' },
{ value: 'university_campus', label: 'Institution', type: 'text' },
{ value: 'institute', label: 'Institute', type: 'text' },
{ value: 'building', label: 'Building', type: 'text' },
{ value: 'room', label: 'Room', type: 'text' },
Expand Down Expand Up @@ -305,16 +306,6 @@ const PropertiesForm = () => {
}
</Col>
</Row>
<Row className="mb-4">
<Col>
<div className="mb-2">
Previous versions of this device
</div>
<div>
Later versions of this device
</div>
</Col>
</Row>
<Row className="mb-3">
<Col>
{
Expand Down Expand Up @@ -454,15 +445,18 @@ const PropertiesForm = () => {
>
<Accordion.Item eventKey="physical_data">
<Accordion.Header>
Information for Publications
Physical descriptions
</Accordion.Header>
<Accordion.Body>
<Row className="mb-3">
<Col>
{textInput(deviceDescription, deviceDescriptionsStore, 'size', 'Size')}
</Col>
<Col>
{textInput(deviceDescription, deviceDescriptionsStore, 'weight', 'Weight [kg]', 'Weight in kilogram')}
{inputGroupWithWeightUnit(
deviceDescription, deviceDescriptionsStore,
'weight', 'weight_unit', 'Weight', 'Weight in kilogram'
)}
</Col>
</Row>
</Accordion.Body>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddWeightUnitToDeviceDescriptions < ActiveRecord::Migration[6.1]
def change
add_column :device_descriptions, :weight_unit, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2024_09_17_085816) do
ActiveRecord::Schema.define(version: 2025_01_20_162008) do

# These are extensions that must be enabled in order to support this database
enable_extension "hstore"
Expand Down Expand Up @@ -488,6 +488,7 @@
t.text "measures_after_short_shut_down"
t.text "measures_to_plan_offline_period"
t.text "restart_after_planned_offline_period"
t.string "weight_unit"
t.index ["ancestry"], name: "index_device_descriptions_on_ancestry"
t.index ["device_id"], name: "index_device_descriptions_on_device_id"
end
Expand Down

0 comments on commit a0b387b

Please sign in to comment.