Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds error message for wrong input #59

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions src/Components/Control-Template/Control-Template.component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import React from 'react';
import './Control-Template.styles.css';
import { createStructuredSelector } from 'reselect';
import { connect } from 'react-redux';
import { selectLevel, selectSpellInput } from '../../redux/control/control.selectors';
import { selectLevel, selectSpellInput, selectErrorMessage } from '../../redux/control/control.selectors';
import { HandleSpellInputChange, OnSpellSubmit } from '../../redux/control/control.actions';

const ControlTemplate = ({ submitSpell, spell_input, hints, headerText, level, handleChange }) => {
const ControlTemplate = ({ submitSpell, spell_input, hints, headerText, level, handleChange, error_message }) => {
//they are the only unique things to each Control //level comes from reducer now
//the caveat of using controlTemplate is that ,now , we have to clear our spell_input every time a user submit the spell correctly
//to make classnames dynamic to the level value.
Expand All @@ -22,18 +22,24 @@ const ControlTemplate = ({ submitSpell, spell_input, hints, headerText, level, h
{hints}

<p className='hint'>Don't forget to finish your spell with semicolon &#59; </p>
<p>#fire_spell &#123;</p>
<p>display: flex;</p>
<section>
<textarea
className='control_input'
type='input'
value={spell_input} //to clear the textarea with redux
onChange={handleChange}
autoFocus={true}
required={true}
/>
<p>#board &#123;</p>
<div className="code_indent">
<p>display: flex;</p>
<textarea
className='control_input'
type='input'
value={spell_input} //to clear the textarea with redux
onChange={handleChange}
autoFocus={true}
required={true}
/>
</div>

<p>&#125;</p>
{
error_message && <span className='error_message'> {error_message} </span>
}
<div className='control_submit'>
<button className='record_btn' onClick={() => submitSpell(level)}>
Next
Expand All @@ -49,6 +55,7 @@ const ControlTemplate = ({ submitSpell, spell_input, hints, headerText, level, h
const mapStateToProps = createStructuredSelector({
level: selectLevel,
spell_input: selectSpellInput,
error_message: selectErrorMessage,
});

const mapDispatchToProps = dispatch => ({
Expand Down
8 changes: 8 additions & 0 deletions src/Components/Control-Template/Control-Template.styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@
.control_submit{
display: flex;
justify-content: flex-end;
}

.error_message {
color: red;
}

.code_indent {
padding-left: 2rem;
}
4 changes: 4 additions & 0 deletions src/redux/control/control.reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const INITIAL_STATE = {
fire_spell: {}, //this is input that users type on <input />
spell_input: '',
levels: new Levels(), //an object with all of the levels
error_message: ""
};

const ControlReducer = (state = INITIAL_STATE, action) => {
Expand All @@ -20,16 +21,19 @@ const ControlReducer = (state = INITIAL_STATE, action) => {
score: state.score + 100,
spell_input: '',
fire_spell: {},
error_message: "",
};
case ControlActionTypes.INCORRECT_SPELL_INPUT:
return {
...state, //we can decrease scores or show errors from here if we want
error_message: `Oops, your spell misfired!`,
};
case ControlActionTypes.SPELL_INPUT_CHANGE:
return {
...state,
spell_input: action.payload,
fire_spell: SpellParsing(state.spell_input),
error_message: "",
};
default:
return state;
Expand Down
2 changes: 2 additions & 0 deletions src/redux/control/control.selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ export const selectScore = createSelector([selectControl], control => control.sc
export const selectFireSpell = createSelector([selectControl], control => control.fire_spell);

export const selectSpellInput = createSelector([selectControl], control => control.spell_input);

export const selectErrorMessage = createSelector([selectControl], control => control.error_message);