Skip to content
Snippets Groups Projects
Commit f7a7cb8f authored by Gabriel Clark's avatar Gabriel Clark
Browse files

Seperated input and output unit selectors by type prop.

parent 32feda16
Branches
No related tags found
No related merge requests found
...@@ -8,10 +8,9 @@ export function App() { ...@@ -8,10 +8,9 @@ export function App() {
<> <>
<Route exact path={'/'}> <Route exact path={'/'}>
</Route> </Route>
<UnitSelector id="inputUnitSelector"/> <UnitSelector type="Input"/>
<NumberInputField /> <NumberInputField />
<h1>Ouput Section</h1> <UnitSelector type="Output"/>
<UnitSelector id="outputUnitSelector"/>
<EndingNumberOutput /> <EndingNumberOutput />
</> </>
); );
......
import { useSelector } from 'react-redux';
import { selectCurrentOutputUnit } from '../unit-selector/unitSelectorSlice';
export function EndingNumberOutput() { export function EndingNumberOutput() {
const currentOutputUnit = useSelector(selectCurrentOutputUnit);
return ( return (
<div> <div>
<h1>Number Output Text</h1> <h1>{} {currentOutputUnit}</h1>
</div> </div>
); );
} }
...@@ -2,9 +2,16 @@ import { store } from '../../app/store'; ...@@ -2,9 +2,16 @@ import { store } from '../../app/store';
import { useDispatch } from 'react-redux'; import { useDispatch } from 'react-redux';
import { import {
setCurrentInputUnit, setCurrentInputUnit,
setCurrentOutputUnit,
} from './unitSelectorSlice'; } from './unitSelectorSlice';
import PropTypes from 'prop-types';
export function UnitSelector(props) { export function UnitSelector(props) {
// type will either be 'Input' or 'Output'
UnitSelector.propTypes = {
type: PropTypes.string.isRequired,
};
const unitSelectorOptions = [ const unitSelectorOptions = [
{ {
label: 'Km', label: 'Km',
...@@ -33,9 +40,15 @@ export function UnitSelector(props) { ...@@ -33,9 +40,15 @@ export function UnitSelector(props) {
const unsubscribe = store.subscribe(() => const unsubscribe = store.subscribe(() =>
console.log('State after dispatch: ', store.getState()), console.log('State after dispatch: ', store.getState()),
); );
const currentInputUnit = e.target.value; const currentUnit = e.target.value;
console.log(currentInputUnit); console.log(currentUnit);
disbatch(setCurrentInputUnit(currentInputUnit));
if (props.type === 'Input'){
disbatch(setCurrentInputUnit(currentUnit));
}
if (props.type === 'Output'){
disbatch(setCurrentOutputUnit(currentUnit));
}
unsubscribe(); unsubscribe();
} }
...@@ -46,7 +59,7 @@ export function UnitSelector(props) { ...@@ -46,7 +59,7 @@ export function UnitSelector(props) {
return ( return (
<div> <div>
<form onSubmit={handleSubmit}> <form onSubmit={handleSubmit}>
<h1>Unit Selector</h1> <h1>{props.type} Unit Selector</h1>
<div className="select-container"> <div className="select-container">
<select name="units" onChange={handleChange}> <select name="units" onChange={handleChange}>
{unitSelectorOptions.map((option) => {unitSelectorOptions.map((option) =>
......
...@@ -3,8 +3,8 @@ import { createSlice } from '@reduxjs/toolkit'; ...@@ -3,8 +3,8 @@ import { createSlice } from '@reduxjs/toolkit';
const unitSelectorSlice = createSlice({ const unitSelectorSlice = createSlice({
name: 'unitSelector', name: 'unitSelector',
initialState: { initialState: {
currentInputUnit: 'in', currentInputUnit: 'km',
currentOutputUnit: 'in', currentOutputUnit: 'km',
}, },
reducers: { reducers: {
// The action payload should have the unit to switch to with the in or out // The action payload should have the unit to switch to with the in or out
...@@ -34,3 +34,7 @@ export const { ...@@ -34,3 +34,7 @@ export const {
export function selectCurrentInputUnit(state) { export function selectCurrentInputUnit(state) {
return state.unitSelector.currentInputUnit; return state.unitSelector.currentInputUnit;
} }
export function selectCurrentOutputUnit(state) {
return state.unitSelector.currentOutputUnit;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment