-
Notifications
You must be signed in to change notification settings - Fork 112
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
Complete app's logic #1
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not all changes are required to be applied.
It's just opening of discussion
src/App.js
Outdated
<p className="App-intro"> | ||
To get started, edit <code>src/App.js</code> and save to reload. | ||
</p> | ||
<div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Obsolete divs
src/components/AppContainer.js
Outdated
@@ -0,0 +1,111 @@ | |||
import React, { Component } from 'react'; | |||
import _ from 'lodash'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Webpack 2 provides tree shaking so unused lodash methods are cut out from the bundle.
However that's not always the case on other projects, so we tent to only import methods we need from lodash.
Please use named imports. This also removes the need of having _.
in your code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to emphasize using lodash'es function (e.g. isNaN(), not the original one). So should I use import isNaN from 'lodash/isNaN';
rather than this approach?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the common practice is
import { isNan, isEmpty, otherMethod } from 'lodash'
src/components/AppContainer.js
Outdated
constructor(props) { | ||
super(props); | ||
|
||
this.state = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(minor) as state does not use props, then it can be defined outside constructor.
Also binding can be omitted (next comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, when trying to move it outside the constructor I got the following error when I hover 'state':
Conflicting inherited declaration React.Component.state was found in namespace public
.
I see I got in emoji_roulette state in the way you described and it wasn't treated as an error. What's interesting, { Component } imported from react has there purple color while in rrule project it's white.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is and eslint error (unexpected token =) but this is actually working. And might be the issue with eslint itselft babel/babel-eslint#312
This is stylistic suggestion so there is no need to change that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In emoji_roulette we have React 15.6.1 while here you have 16.0.0. This might be the case that WebStorm does not know where to find import { Component }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to state - it's webstorm related (not ESLint error)
src/components/AppContainer.js
Outdated
this.handleCopy = this.handleCopy.bind(this); | ||
} | ||
|
||
handleChange(event) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about using handleChange = (event) => {
that way you do not need to this.handleChange = this.handleChange.bind(this);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What to heck, again I got an error. This time is ESLint: Parsing error: Unexpected token =
on the first '='. It looks like more global Component problem??
EDIT. But it do work
EDIT 2. Adding "parser": "babel-eslint"
to .eslintrc fixed that
src/components/AppContainer.js
Outdated
handleChange(event) { | ||
const newData = _.cloneDeep(this.state.data); | ||
_.set(newData, event.target.name, event.target.value); | ||
this.setState({ data: newData, isCopied: false }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
During coder dojo we found bug in this solution when handleChange
gets called faster than new state value is accessible in state.
the fix was:
handleChange = (event) => {
this.setState((currentState)=> {
const newData = cloneDeep(currentState.data);
set(newData, event.target.name, event.target.value);
return { data: newData, isCopied: false };
})
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change seems to require event.persist();
in all onChange functions. Ugh :(
Is it critical? Or how often did it happen to you (or in which situations)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This issue happened when we had two fields dependent on each other and when one was changed, we immediately called the change handle for the second field.
Regarding this event.persist();
You can call event.persist on the beginning of this handleChange method, and not in every onChange function.
src/components/Repeat/Monthly/On.js
Outdated
handleChange(editedEvent); | ||
}} | ||
> | ||
{[...new Array(31)].map((day, i) => <option key={i} value={i + 1}>{i + 1}</option>)} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good one 👍
|
||
<div className="btn-group" data-toggle="buttons"> | ||
{_.toPairs(days).map((day) => { | ||
const dayName = day[0]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about using destructuring for that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
src/components/Repeat/Yearly/On.js
Outdated
className="form-control" | ||
checked={mode === 'on'} | ||
onChange={(event) => { | ||
const editedEvent = { ...event, target: { ...event.target, value: 'on', name: event.target.name } }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about adding value="on"
to the input field?
That way you will not have to edit event.
Here and in other cases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, didn't know I can do it like that. Thanks
src/utils/computeWeekly.js
Outdated
}; | ||
|
||
const activeDays = []; | ||
_.values(days).forEach((isDayActive, dayIndex) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is perfect use-case for reduce
method
src/utils/computeYearlyOn.js
Outdated
const computeYearlyOn = (on) => { | ||
const repeat = {}; | ||
|
||
months.forEach((month, monthIndex) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
another use-case for reduce
src/utils/computeYearlyOn.js
Outdated
}; | ||
const computeYearlyOn = on => ({ | ||
bymonth: months.reduce( | ||
(lastMonth, month, monthIndex) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't know how to name first argument
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahhh sorry I did not understood the intention of what this code was supposed to accomplish.
Using reduce is not the correct tool here, as you want to get the index of on.month
from array of months and add 1 to it.
This can be done much easier by bymonth: months.indexOf(on.month) + 1,
This PR contains following: