Commit 475d2841 authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Fixed browser units from hook and context.

parent d2627a64
Pipeline #121625 passed with stages
in 30 minutes and 29 seconds
......@@ -54,11 +54,10 @@ export default function ArchiveBrowser({data}) {
// the Browser (notably: Quantity, QuantityItemPreview). In order to pass the
// up-to-date unit information, we pass the hook value down the component
// hierarchy.
const units = useUnits()
data.resources = data.resources || {}
return (
<Browser
adaptor={archiveAdaptorFactory(data, undefined, units)}
adaptor={archiveAdaptorFactory(data, undefined)}
form={<ArchiveConfigForm searchOptions={searchOptions} data={data}/>}
/>
)
......@@ -160,8 +159,8 @@ ArchiveConfigForm.propTypes = ({
searchOptions: PropTypes.arrayOf(PropTypes.object).isRequired
})
function archiveAdaptorFactory(data, sectionDef, units) {
return new SectionAdaptor(data, sectionDef || rootSections.find(def => def.name === 'EntryArchive'), undefined, {archive: data}, units)
function archiveAdaptorFactory(data, sectionDef) {
return new SectionAdaptor(data, sectionDef || rootSections.find(def => def.name === 'EntryArchive'), undefined, {archive: data})
}
function archiveSearchOptions(data) {
......@@ -212,22 +211,21 @@ function archiveSearchOptions(data) {
}
class ArchiveAdaptor extends Adaptor {
constructor(obj, def, parent, context, units) {
constructor(obj, def, parent, context) {
super(obj)
this.def = def
this.parent = parent
this.context = context
this.units = units
}
adaptorFactory(obj, def, parent, context) {
if (def.m_def === 'Section') {
return new SectionAdaptor(obj, def, parent, context || this.context, this.units)
return new SectionAdaptor(obj, def, parent, context || this.context)
} else if (def.m_def === 'Quantity') {
if (def.type.type_kind === 'reference') {
return new ReferenceAdaptor(obj, def, parent, context || this.context, this.units)
return new ReferenceAdaptor(obj, def, parent, context || this.context)
} else {
return new QuantityAdaptor(obj, def, parent, context || this.context, this.units)
return new QuantityAdaptor(obj, def, parent, context || this.context)
}
}
}
......@@ -285,23 +283,24 @@ class SectionAdaptor extends ArchiveAdaptor {
}
}
render() {
return <Section section={this.e} def={this.def} parent={this.parent} units={this.units}/>
return <Section section={this.e} def={this.def} parent={this.parent} />
}
}
class ReferenceAdaptor extends ArchiveAdaptor {
render() {
return <Reference value={this.e} def={this.def} units={this.units}/>
return <Reference value={this.e} def={this.def} />
}
}
class QuantityAdaptor extends ArchiveAdaptor {
render() {
return <Quantity value={this.e} def={this.def} units={this.units}/>
return <Quantity value={this.e} def={this.def} />
}
}
function QuantityItemPreview({value, def, units}) {
function QuantityItemPreview({value, def}) {
const units = useUnits()
if (def.type.type_kind === 'reference') {
return <Box component="span" fontStyle="italic">
<Typography component="span">reference ...</Typography>
......@@ -348,11 +347,11 @@ function QuantityItemPreview({value, def, units}) {
}
QuantityItemPreview.propTypes = ({
value: PropTypes.any,
def: PropTypes.object.isRequired,
units: PropTypes.object
def: PropTypes.object.isRequired
})
function QuantityValue({value, def, units}) {
function QuantityValue({value, def}) {
const units = useUnits()
const val = (def.type.type_data === 'nomad.metainfo.metainfo._Datetime' ? new Date(value).toLocaleString() : value)
const [finalValue, finalUnit] = def.unit
? toUnitSystem(val, def.unit, units, true)
......@@ -392,11 +391,10 @@ function QuantityValue({value, def, units}) {
}
QuantityValue.propTypes = ({
value: PropTypes.any,
def: PropTypes.object.isRequired,
units: PropTypes.object
def: PropTypes.object.isRequired
})
function Section({section, def, parent, units}) {
function Section({section, def, parent}) {
const config = useRecoilValue(configState)
if (!section) {
......@@ -414,7 +412,7 @@ function Section({section, def, parent, units}) {
const quantities = def._allProperties.filter(prop => prop.m_def === 'Quantity')
return <Content>
<Title def={def} data={section} kindLabel="section" />
<Overview section={section} def={def} units={units}/>
<Overview section={section} def={def} />
<Compartment title="sub sections">
{sub_sections
.filter(subSectionDef => section[subSectionDef.name] || config.showAllDefined)
......@@ -462,7 +460,6 @@ function Section({section, def, parent, units}) {
<QuantityItemPreview
value={section[quantityDef.name]}
def={quantityDef}
units={units}
/>
</span>
}
......@@ -478,8 +475,7 @@ function Section({section, def, parent, units}) {
Section.propTypes = ({
section: PropTypes.object.isRequired,
def: PropTypes.object.isRequired,
parent: PropTypes.any,
units: PropTypes.object
parent: PropTypes.any
})
function SubSectionList({subSectionDef}) {
......@@ -571,14 +567,13 @@ PropertyValuesList.propTypes = ({
values: PropTypes.arrayOf(PropTypes.string).isRequired
})
function Quantity({value, def, units}) {
function Quantity({value, def}) {
return <Content>
<Title def={def} data={value} kindLabel="value" />
<Compartment title="value">
<QuantityValue
value={value}
def={def}
units={units}
/>
</Compartment>
<Meta def={def} />
......@@ -586,11 +581,10 @@ function Quantity({value, def, units}) {
}
Quantity.propTypes = ({
value: PropTypes.any,
def: PropTypes.object.isRequired,
units: PropTypes.object
def: PropTypes.object.isRequired
})
function Reference({value, def, units}) {
function Reference({value, def}) {
const {api} = useApi()
const {raiseError} = useErrors()
const [loading, setLoading] = useState(true)
......@@ -633,8 +627,7 @@ function Reference({value, def, units}) {
}
Reference.propTypes = ({
value: PropTypes.any,
def: PropTypes.object.isRequired,
units: PropTypes.object
def: PropTypes.object.isRequired
})
const useMetaStyles = makeStyles(theme => ({
......
......@@ -18,7 +18,6 @@
import React, { useContext, useRef, useLayoutEffect, useMemo, useState } from 'react'
import PropTypes from 'prop-types'
import { RecoilRoot } from 'recoil'
import { makeStyles, Card, CardContent, Box, Typography } from '@material-ui/core'
import grey from '@material-ui/core/colors/grey'
import ArrowRightIcon from '@material-ui/icons/ArrowRight'
......@@ -126,7 +125,7 @@ export const Browser = React.memo(function Browser({adaptor, form}) {
return lanes
}, [root, archivePath, memoedAdapters, render, setRender])
return <RecoilRoot>
return <React.Fragment>
{form}
<Card>
<CardContent>
......@@ -141,7 +140,7 @@ export const Browser = React.memo(function Browser({adaptor, form}) {
</div>
</CardContent>
</Card>
</RecoilRoot>
</React.Fragment>
})
Browser.propTypes = ({
adaptor: PropTypes.object.isRequired,
......
......@@ -12,7 +12,7 @@ import BrillouinZone from '../visualization/BrillouinZone'
import BandStructure from '../visualization/BandStructure'
import EELS from '../visualization/EELS'
import DOS from '../visualization/DOS'
import { toUnitSystem } from '../../units'
import { toUnitSystem, useUnits } from '../../units'
import { electronicRange } from '../../config'
import EnergyVolumeCurve from '../visualization/EnergyVolumeCurve'
......@@ -257,27 +257,27 @@ OverviewEquationOfState.propTypes = ({
export const Overview = React.memo((props) => {
const {def} = props
const units = useUnits()
let path = window.location.href.split('/').pop().split(':')[0]
if (def.name === 'BandStructure' && path === 'band_structure_electronic') {
return <OverviewBandstructureElectronic {...props}/>
return <OverviewBandstructureElectronic {...props} units={units}/>
} else if (def.name === 'BandStructure' && path === 'band_structure_phonon') {
return <OverviewBandstructurePhonon {...props}/>
return <OverviewBandstructurePhonon {...props} units={units}/>
} else if (def.name === 'Atoms' && path === 'atoms') {
return <OverviewAtoms {...props}/>
return <OverviewAtoms {...props} units={units} />
} else if (def.name === 'Dos' && path === 'dos_electronic') {
return <OverviewDOSElectronic {...props}/>
return <OverviewDOSElectronic {...props} units={units} />
} else if (def.name === 'Dos' && path === 'dos_phonon') {
return <OverviewDOSPhonon {...props}/>
return <OverviewDOSPhonon {...props} units={units}/>
} else if (def.name === 'Spectrum') {
return <OverviewEELS {...props}/>
return <OverviewEELS {...props} units={units}/>
} else if (def.name === 'EquationOfState') {
return <OverviewEquationOfState {...props}/>
return <OverviewEquationOfState {...props} units={units}/>
}
return null
})
Overview.propTypes = ({
def: PropTypes.object,
section: PropTypes.object,
units: PropTypes.object
section: PropTypes.object
})
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment