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

Feature/open one form and clear #7

Merged
merged 3 commits into from
Apr 12, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
28 changes: 20 additions & 8 deletions components/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,26 @@ import PropTypes from 'prop-types';
import firebase from 'lib/firebase';

class Form extends Component {
constructor() {
super();
static getDerivedStateFromProps(nextProps, prevState) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bortojac I would refactor this to only have one return, I would have an object variable and then change just the hand property based on your if statement.

if (nextProps.name !== prevState.hand) {
return {
hand: nextProps.name,
origin: '',
email: '',
comments: '',
};
}

return {
hand: prevState.hand,
origin: '',
email: '',
comments: '',
};
}

constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.state = {
Expand All @@ -15,12 +33,6 @@ class Form extends Component {
};
}

componentWillMount() {
this.setState({
hand: this.props.name,
});
}

componentDidMount() {
this.submissionsRef = firebase.database().ref('submissions');
}
Expand Down
8 changes: 5 additions & 3 deletions components/hand.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ class Hand extends React.Component {
super(props);
this.state = {
handActive: false,
form: false,
};
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.setState({
handActive: !this.state.handActive,
form: !this.state.form,
});
this.props.resetFormState(this.props.name);
}
render() {
const isActive = this.state.handActive ? 'active' : '';
Expand All @@ -32,7 +31,7 @@ class Hand extends React.Component {
<img src={this.props.bwHand} className="black-white" alt={this.props.name} />
</figure>
</div>
<Form name={this.props.name} active={this.state.form} color={this.props.color} />
<Form name={this.props.name} active={this.props.formActive} color={this.props.color} />
<style jsx>
{`
@import './styles/variables.css';
Expand Down Expand Up @@ -106,13 +105,16 @@ Hand.defaultProps = {
color: '#000000',
colorHand: '',
bwHand: '',
formActive: false,
};

Hand.propTypes = {
name: PropTypes.string,
color: PropTypes.string,
colorHand: PropTypes.string,
bwHand: PropTypes.string,
formActive: PropTypes.bool,
resetFormState: PropTypes.func.isRequired,
};

export default Hand;
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@
"babel-plugin-transform-assets-import-to-string": "^1.0.1",
"babel-plugin-transform-inline-environment-variables": "^0.3.0",
"firebase": "^4.10.1",
"next": "^5.0.0",
"next": "^5.1.0",
"postcss-cssnext": "^3.1.0",
"postcss-easy-import": "^3.0.0",
"postcss-import": "^11.1.0",
"prettier": "^1.8.2",
"prop-types": "^15.6.0",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react": "^16.3.0",
"react-dom": "^16.3.0",
"styled-jsx-plugin-postcss": "^0.1.2"
},
"devDependencies": {
Expand Down
36 changes: 36 additions & 0 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,51 +28,85 @@ class Index extends React.Component {
color: '#ffc74f',
colorHand: bangladesh,
bwHand: bangladeshBW,
formActive: false,
},
{
order: 1,
name: 'central europe',
color: '#d09afc',
colorHand: centralEurope,
bwHand: centralEuropeBW,
formActive: false,
},
{
order: 2,
name: 'china',
color: '#475aeb',
colorHand: china,
bwHand: chinaBW,
formactive: false,
},
{
order: 3,
name: 'japan',
color: '#6ad4fc',
colorHand: japan,
bwHand: japanBW,
formActive: false,
},
{
order: 4,
name: 'north america',
color: '#fc92e7',
colorHand: northAmerica,
bwHand: northAmericaBW,
formActive: false,
},
{
order: 5,
name: 'taiwan',
color: '#5ae079',
colorHand: taiwan,
bwHand: taiwanBW,
formActive: false,
},
{
order: 6,
name: 'other',
color: '#f21c1c',
colorHand: other,
bwHand: otherBW,
formActive: false,
},
],
};
this.resetFormState = this.resetFormState.bind(this);
}

resetFormState(activeFormName) {
// we want to update the formActive state for all hands except activeFormName
const newRegions = this.state.regions.map(obj => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue as above. You're ending up writing the same thing twice, want to keep the code as DRY as possible

if (obj.name === activeFormName) {
return {
order: obj.order,
name: obj.name,
color: obj.color,
colorHand: obj.colorHand,
bwHand: obj.bwHand,
formActive: true,
};
}
return {
order: obj.order,
name: obj.name,
color: obj.color,
colorHand: obj.colorHand,
bwHand: obj.bwHand,
formActive: false,
};
});

this.setState({ regions: newRegions });
}

render() {
Expand Down Expand Up @@ -104,6 +138,8 @@ class Index extends React.Component {
color={hand.color}
colorHand={hand.colorHand}
bwHand={hand.bwHand}
formActive={hand.formActive}
resetFormState={this.resetFormState}
/>
))}
</section>
Expand Down