From 8ee755d9fa95be922ffc7a1c9a68af6a0f90bb4e Mon Sep 17 00:00:00 2001 From: Beate Quednau Date: Mon, 20 Jan 2025 18:58:25 +0100 Subject: [PATCH] Fix some fields and translations for device descriptions --- app/api/chemotion/device_description_api.rb | 1 + app/api/entities/device_description_entity.rb | 1 + .../DeviceDescriptionDetails.js | 4 +- .../details/deviceDescriptions/FormFields.js | 67 ++++++++++++++++++- .../propertiesTab/PropertiesForm.js | 20 ++---- ..._add_weight_unit_to_device_descriptions.rb | 5 ++ db/schema.rb | 3 +- 7 files changed, 83 insertions(+), 18 deletions(-) create mode 100644 db/migrate/20250120162008_add_weight_unit_to_device_descriptions.rb diff --git a/app/api/chemotion/device_description_api.rb b/app/api/chemotion/device_description_api.rb index eb02174e98..270e523a6d 100644 --- a/app/api/chemotion/device_description_api.rb +++ b/app/api/chemotion/device_description_api.rb @@ -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 diff --git a/app/api/entities/device_description_entity.rb b/app/api/entities/device_description_entity.rb index 56db7d65e4..9973cdae91 100644 --- a/app/api/entities/device_description_entity.rb +++ b/app/api/entities/device_description_entity.rb @@ -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 diff --git a/app/javascript/src/apps/mydb/elements/details/deviceDescriptions/DeviceDescriptionDetails.js b/app/javascript/src/apps/mydb/elements/details/deviceDescriptions/DeviceDescriptionDetails.js index 657c71546b..f28ff2b896 100644 --- a/app/javascript/src/apps/mydb/elements/details/deviceDescriptions/DeviceDescriptionDetails.js +++ b/app/javascript/src/apps/mydb/elements/details/deviceDescriptions/DeviceDescriptionDetails.js @@ -84,10 +84,8 @@ const DeviceDescriptionDetails = ({ toggleFullScreen }) => { } visibleTabs.forEach((key, i) => { - let title = key.charAt(0).toUpperCase() + key.slice(1); - tabContents.push( - + { !deviceDescription.isNew && diff --git a/app/javascript/src/apps/mydb/elements/details/deviceDescriptions/FormFields.js b/app/javascript/src/apps/mydb/elements/details/deviceDescriptions/FormFields.js index efb3aca982..09ef70c35a 100644 --- a/app/javascript/src/apps/mydb/elements/details/deviceDescriptions/FormFields.js +++ b/app/javascript/src/apps/mydb/elements/details/deviceDescriptions/FormFields.js @@ -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'; @@ -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); } @@ -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 ( + + {labelWithInfo(label, info)} + + + + + + ); +} + 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 = ( +
+ {element.version_doi_url} +
+ ); + } return ( @@ -509,6 +573,7 @@ const identifierMultipleInputGroups = (element, label, options, store, info) => onChange={handleFieldChanged(store, 'version_doi_url', 'text', element.type)} /> + {link} ); } @@ -682,5 +747,5 @@ export { selectInput, multiSelectInput, textInput, multipleInputGroups, textareaInput, dateTimePickerInput, headlineWithToggle, mulipleRowInput, annotationButton, checkboxInput, componentInput, - identifierMultipleInputGroups, toggleContent, + identifierMultipleInputGroups, toggleContent, inputGroupWithWeightUnit } diff --git a/app/javascript/src/apps/mydb/elements/details/deviceDescriptions/propertiesTab/PropertiesForm.js b/app/javascript/src/apps/mydb/elements/details/deviceDescriptions/propertiesTab/PropertiesForm.js index 02cf90f0b9..59ee319b9e 100644 --- a/app/javascript/src/apps/mydb/elements/details/deviceDescriptions/propertiesTab/PropertiesForm.js +++ b/app/javascript/src/apps/mydb/elements/details/deviceDescriptions/propertiesTab/PropertiesForm.js @@ -4,6 +4,7 @@ import { selectInput, multiSelectInput, textInput, multipleInputGroups, textareaInput, dateTimePickerInput, mulipleRowInput, toggleContent, checkboxInput, componentInput, identifierMultipleInputGroups, + inputGroupWithWeightUnit, } from '../FormFields'; import { observer } from 'mobx-react'; @@ -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' }, @@ -305,16 +306,6 @@ const PropertiesForm = () => { } - - -
- Previous versions of this device -
-
- Later versions of this device -
- -
{ @@ -454,7 +445,7 @@ const PropertiesForm = () => { > - Information for Publications + Physical descriptions @@ -462,7 +453,10 @@ const PropertiesForm = () => { {textInput(deviceDescription, deviceDescriptionsStore, 'size', 'Size')} - {textInput(deviceDescription, deviceDescriptionsStore, 'weight', 'Weight [kg]', 'Weight in kilogram')} + {inputGroupWithWeightUnit( + deviceDescription, deviceDescriptionsStore, + 'weight', 'weight_unit', 'Weight', 'Weight in kilogram' + )} diff --git a/db/migrate/20250120162008_add_weight_unit_to_device_descriptions.rb b/db/migrate/20250120162008_add_weight_unit_to_device_descriptions.rb new file mode 100644 index 0000000000..fdb15799ee --- /dev/null +++ b/db/migrate/20250120162008_add_weight_unit_to_device_descriptions.rb @@ -0,0 +1,5 @@ +class AddWeightUnitToDeviceDescriptions < ActiveRecord::Migration[6.1] + def change + add_column :device_descriptions, :weight_unit, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 568505a027..5a3fc3175d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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