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

Finished unitselectorSlice and store features.

parent aa19266e
No related branches found
No related tags found
No related merge requests found
import { configureStore } from '@reduxjs/toolkit'; import { configureStore } from '@reduxjs/toolkit';
import counterSlice from '../features/counter/counterSlice.js'; import unitSelectorSlice from '../features/unit-selector/unitSelectorSlice.js';
export const store = configureStore({ export const store = configureStore({
reducer: { reducer: {
[counterSlice.name]: counterSlice.reducer, [unitSelectorSlice.name]: unitSelectorSlice.reducer,
}, },
}); });
import { UnitSelector } from '../unit-selector/unitSelector'; import { store } from '../../app/store';
export function NumberInputField() { export function NumberInputField() {
const unitSelectorName = UnitSelector.name; const placeHolderString = `Enter # of "${store.getState().unitSelector.currentInputUnit}"`;
const inputUnits = document.getElementById('inputUnitSelector').UnitSelector;
const placeHolderString = `Enter # of ${inputUnits}`;
return ( return (
<div> <div>
<h1>NumberInputField</h1> <h1>NumberInputField</h1>
<p>{ unitSelectorName }</p>
<input required type="text" placeholder={ placeHolderString }></input> <input required type="text" placeholder={ placeHolderString }></input>
</div> </div>
); );
......
import { store } from '../../app/store';
export function UnitSelector(props) { export function UnitSelector(props) {
const unitSelectorOptions = [ const unitSelectorOptions = [
{ {
...@@ -12,10 +14,6 @@ export function UnitSelector(props) { ...@@ -12,10 +14,6 @@ export function UnitSelector(props) {
label: 'Cm', label: 'Cm',
value: 'cm', value: 'cm',
}, },
{
label: 'Cm',
value: 'cm',
},
{ {
label: 'Ft', label: 'Ft',
value: 'ft', value: 'ft',
...@@ -26,15 +24,35 @@ export function UnitSelector(props) { ...@@ -26,15 +24,35 @@ export function UnitSelector(props) {
}, },
]; ];
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,
});
unsubscribe();
}
function handleSubmit(e) {
e.preventDefault();
}
return ( return (
<div> <div>
<form onSubmit={handleSubmit}>
<h1>Unit Selector</h1> <h1>Unit Selector</h1>
<div className="select-container"> <div className="select-container">
<select name="units"> <select name="units" onChange={handleChange}>
{unitSelectorOptions.map((option) => {unitSelectorOptions.map((option) =>
<option value={option.value}>{option.label}</option>)} <option value={option.value}>{option.label}</option>)}
</select> </select>
</div> </div>
</form>
</div> </div>
); );
} }
...@@ -3,23 +3,37 @@ import { createSlice } from '@reduxjs/toolkit'; ...@@ -3,23 +3,37 @@ import { createSlice } from '@reduxjs/toolkit';
const unitSelectorSlice = createSlice({ const unitSelectorSlice = createSlice({
name: 'unitSelector', name: 'unitSelector',
initialState: { initialState: {
selectedInputUnit: '', currentInputUnit: 'in',
currentOutputUnit: 'in',
}, },
reducers: { reducers: {
setCurrentInputUnit: (state, action) => { // The action payload should have the unit to switch to with the in or out
const { unitSelectorReducer: (state, action) => {
value, console.log(state, action);
} = action.payload; const unitToSwitchTo = action.payload;
state.value = value; switch (action.type){
case 'setCurrentInputUnit':
return {
...state,
currentInputUnit: unitToSwitchTo,
};
case 'setCurrentOutputUnit':
return {
...state,
currentOutputUnit: unitToSwitchTo,
};
default:
return state;
}
}, },
}, },
}); });
export default unitSelectorSlice; export default unitSelectorSlice;
export const { export const {
setCurrentInputUnit, unitSelectorReducer,
} = unitSelectorSlice.actions; } = unitSelectorSlice.actions;
export function selectCurrentInputUnit(state) { export function getCurrentInputUnit(state) {
return state.state.value; 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