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

Changes in input units are reflected in inputField.

parent 992062b0
No related branches found
No related tags found
No related merge requests found
import { store } from '../../app/store';
import { useSelector } from 'react-redux';
import { selectCurrentInputUnit } from '../unit-selector/unitSelectorSlice';
export function NumberInputField() {
const placeHolderString = `Enter # of "${store.getState().unitSelector.currentInputUnit}"`;
const currentInputUnit = useSelector(selectCurrentInputUnit);
const placeHolderString = `Enter # of "${currentInputUnit}"`;
return (
<div>
<h1>NumberInputField</h1>
......
import { store } from '../../app/store';
import { useDispatch } from 'react-redux';
import {
setCurrentInputUnit,
} from './unitSelectorSlice';
export function UnitSelector(props) {
const unitSelectorOptions = [
......@@ -24,17 +28,14 @@ export function UnitSelector(props) {
},
];
const disbatch = useDispatch();
function handleChange(e) {
const unsubscribe = store.subscribe(() =>
console.log('State after dispatch: ', store.getState()),
);
const currentInputUnit = e.target.value;
console.log(currentInputUnit);
store.dispatch({
type: 'setCurrentInputUnit',
payload: currentInputUnit,
});
disbatch(setCurrentInputUnit(currentInputUnit));
unsubscribe();
}
......
......@@ -8,32 +8,29 @@ const unitSelectorSlice = createSlice({
},
reducers: {
// The action payload should have the unit to switch to with the in or out
unitSelectorReducer: (state, action) => {
console.log(state, action);
const unitToSwitchTo = action.payload;
switch (action.type){
case 'setCurrentInputUnit':
setCurrentInputUnit: (state, action) => {
const newUnit = action.payload;
return {
...state,
currentInputUnit: unitToSwitchTo,
currentInputUnit: newUnit,
};
case 'setCurrentOutputUnit':
},
setCurrentOutputUnit: (state, action) => {
const newUnit = action.payload;
return {
...state,
currentOutputUnit: unitToSwitchTo,
currentOutputUnit: newUnit,
};
default:
return state;
}
},
},
});
export default unitSelectorSlice;
export const {
unitSelectorReducer,
setCurrentInputUnit,
setCurrentOutputUnit,
} = unitSelectorSlice.actions;
export function getCurrentInputUnit(state) {
export function selectCurrentInputUnit(state) {
return state.unitSelector.currentInputUnit;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment