Skip to content

Commit

Permalink
Started adding app-wide unit selector logic
Browse files Browse the repository at this point in the history
  • Loading branch information
pierotofy committed May 2, 2024
1 parent d735582 commit bf24be7
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
69 changes: 69 additions & 0 deletions app/static/app/js/classes/Units.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { _ } from '../classes/gettext';

const units = {
acres: {
factor: 0.00024711,
label: _('Acres'),
abbr: 'ac'
},
feet: {
factor: 3.2808,
label: _('Feet'),
abbr: 'ft'
},
hectares: {
factor: 0.0001,
label: _('Hectares'),
abbr: 'ha'
},
meters: {
factor: 1,
label: _('Meters'),
abbr: 'm'
},
kilometers: {
factor: 0.001,
label: _('Kilometers'),
abbr: 'km'
},
centimeters: {
factor: 100,
label: _('Centimeters'),
abbr: 'cm'
},
miles: {
factor: 3.2808 / 5280,
label: _('Miles'),
abbr: 'mi'
},
sqfeet: {
factor: 10.7639,
label: _('Square Feet'),
abbr: 'ft²'
},
sqmeters: {
factor: 1,
label: _('Square Meters'),
abbr: 'm²'
},
sqmiles: {
factor: 0.000000386102,
label: _('Square Miles'),
abbr: 'mi²'
}
};

const systems = {
metric: {
length: [units.kilometers, units.meters, units.centimeters],
area: [units.sqmeters]
}

// TODO
}

export default {
// to be used on individual strings

};

30 changes: 30 additions & 0 deletions app/static/app/js/components/UnitSelector.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';
import PropTypes from 'prop-types';

class UnitSelector extends React.Component {
static propTypes = {
}

constructor(props){
super(props);

this.state = {
system: window.getPreferredUnitSystem()
}
}

handleChange = e => {
this.setState({system: e.target.value});
window.setPreferredUnitSystem(e.target.value);
};

render() {
return (
<select value={this.state.system} onChange={this.handleChange}>
<option value="metric" key={}></option>
</select>
);
}
}

export default UnitSelector;
10 changes: 10 additions & 0 deletions app/static/app/js/components/tests/UnitSelector.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';
import { shallow } from 'enzyme';
import UnitSelector from '../UnitSelector';

describe('<UnitSelector />', () => {
it('renders without exploding', () => {
const wrapper = shallow(<UnitSelector />);
expect(wrapper.exists()).toBe(true);
})
});
8 changes: 8 additions & 0 deletions app/static/app/js/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ window.React = React;
// Expose set locale function globally
window.setLocale = setLocale;

// Expose to allow every part of the app to access this information
window.getPreferredUnitSystem = () => {
return localStorage.getItem("preferred_unit_system") || "metric";
};
window.setPreferredUnitSystem = (system) => {
localStorage.setItem("preferred_unit_system", system);
};

$(function(){
PluginsAPI.App.triggerReady();
});
Expand Down

0 comments on commit bf24be7

Please sign in to comment.