Skip to content

Commit

Permalink
feat: run projection for each scenario and handle altitudinal zone ho…
Browse files Browse the repository at this point in the history
…chmontan
  • Loading branch information
friedjoff committed Mar 18, 2020
1 parent 4e71d21 commit 4284c40
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/components/ProjectionForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function ProjectionForm() {
mapLocation,
formLocation,
projectionMode,
projectionResult: { options },
projectionResult: [{ options }],
} = useSelector(state => ({
location: state.location,
mapLocation: state.mapLocation,
Expand Down
35 changes: 8 additions & 27 deletions src/components/ProjectionResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,21 @@ import { ReactComponent as EarthModerateIcon } from '../icons/earthModerate.svg'
import { ReactComponent as EarthTodayIcon } from '../icons/earthToday.svg';

function ProjectionResult() {
const {
location,
options,
projectionMode,
projections,
targetAltitudinalZone,
} = useSelector(state => ({
projectionMode: state.projectionMode,
const { location, projectionMode, projectionResult } = useSelector(state => ({
location: state.location,
options: state.projectionResult.options,
projections: [...state.projectionResult.projections],
targetAltitudinalZone: state.targetAltitudinalZone,
projectionMode: state.projectionMode,
projectionResult: state.projectionResult,
}));
const { i18n, t } = useTranslation();

const { altitudinalZone, forestType } = location;
if (
altitudinalZone &&
(altitudinalZone === targetAltitudinalZone ||
projections.findIndex(p => p.altitudinalZone === altitudinalZone) === -1)
) {
projections.unshift(location);
}

const panes = [];
panes.push({
menuItem: t('recommendation.header'),
render: () => <Recommendation />,
});

projections.forEach(p => {
projectionResult.forEach(r => {
const p = r.projections[r.projections.length - 1];
const icons = [];
const scenarios = [];
if (projectionMode === 'f') {
Expand All @@ -53,7 +37,7 @@ function ProjectionResult() {
} else {
scenarios.push(t('projectionScenario.manual'));
}
} else {
} else if (p) {
if (location.altitudinalZone === p.altitudinalZone) {
icons.push(<EarthTodayIcon key="today" className={styles.iconToday} />);
scenarios.push(t('projectionScenario.today'));
Expand Down Expand Up @@ -97,10 +81,7 @@ function ProjectionResult() {
}
});

return forestType &&
options.forestType &&
options.forestType.includes(forestType) &&
(location.coordinate || targetAltitudinalZone) ? (
return (
<div className={styles.container}>
{panes.length > 0 ? (
<Tab
Expand All @@ -120,7 +101,7 @@ function ProjectionResult() {
</Header>
)}
</div>
) : null;
);
}

export default ProjectionResult;
2 changes: 1 addition & 1 deletion src/components/Recommendation.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function Recommendation() {
const [future, setFuture] = useState(true);
const { location, projections } = useSelector(state => ({
location: state.location,
projections: state.projectionResult.projections,
projections: state.projectionResult[0].projections,
}));

const recommendations = useMemo(() => {
Expand Down
49 changes: 42 additions & 7 deletions src/store/enhancers/projection.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
setLocation,
setLocateResult,
setProjectionResult,
setTargetAltitudinalZone,
} from '../actions';

const projectionActionTypes = [
Expand All @@ -18,6 +17,24 @@ const projectionActionTypes = [
SET_PROJECTION_MODE,
];

const hochmontanAltitudinalZones = ['81', '82', '83'];

const getProjectionConfig = (location, targetAltitudinalZone) => {
let newTargetAltitudinalZone = targetAltitudinalZone;
let { altitudinalZone, silverFirArea } = location;
if (hochmontanAltitudinalZones.includes(targetAltitudinalZone)) {
silverFirArea = targetAltitudinalZone.slice(1);
newTargetAltitudinalZone = '80';
}
if (hochmontanAltitudinalZones.includes(altitudinalZone)) {
altitudinalZone = '80';
}
return {
location: { ...location, altitudinalZone, silverFirArea },
targetAltitudinalZone: newTargetAltitudinalZone,
};
};

const projection = store => next => action => {
const result = next(action);
if (projectionActionTypes.includes(action.type)) {
Expand All @@ -31,20 +48,38 @@ const projection = store => next => action => {
delete location.transitionAltitudinalZone;
}
store.dispatch(setLocation(location));
const targetAltitudinalZone =
projectionMode === 'm'
? mapLocation.targetAltitudinalZoneExtreme
: formLocation.targetAltitudinalZone;
store.dispatch(setTargetAltitudinalZone(targetAltitudinalZone));

try {
const locateResult = locate(location);
console.log(locateResult, location);
store.dispatch(setLocateResult(locateResult));
} catch (error) {
console.log('Locate error: ', error);
}

const projectionConfig = [];
if (projectionMode === 'm') {
const {
targetAltitudinalZoneModerate,
targetAltitudinalZoneExtreme,
} = mapLocation;
projectionConfig.push(
getProjectionConfig(location, targetAltitudinalZoneModerate),
);
projectionConfig.push(
getProjectionConfig(location, targetAltitudinalZoneExtreme),
);
} else {
projectionConfig.push(
getProjectionConfig(location, formLocation.targetAltitudinalZone),
);
}

try {
const projectionResult = project(location, targetAltitudinalZone);
const projectionResult = projectionConfig.map(config =>
project(config.location, config.targetAltitudinalZone),
);
console.log({ projectionConfig, projectionResult });
store.dispatch(setProjectionResult(projectionResult));
} catch (error) {
console.log('Projection error: ', error);
Expand Down

0 comments on commit 4284c40

Please sign in to comment.