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

Complete app's logic #1

Merged
merged 23 commits into from
Oct 16, 2017
Merged

Complete app's logic #1

merged 23 commits into from
Oct 16, 2017

Conversation

Fafruch
Copy link
Owner

@Fafruch Fafruch commented Oct 14, 2017

This PR contains following:

  • visual representation of all possible options to choose from (no CSS - TO DO)
  • add handling rrule.js converting library with users input from components
  • add rrule.js, moment.js, lodash, react-datetime, react-copy-to-clipboard dependencies

Copy link

@ar-mac ar-mac left a 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>
Copy link

Choose a reason for hiding this comment

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

Obsolete divs

@@ -0,0 +1,111 @@
import React, { Component } from 'react';
import _ from 'lodash';
Copy link

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

Copy link
Owner Author

@Fafruch Fafruch Oct 15, 2017

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?

Copy link

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'

constructor(props) {
super(props);

this.state = {
Copy link

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)

Copy link
Owner Author

@Fafruch Fafruch Oct 15, 2017

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.

Copy link

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.

Copy link

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 }

Copy link
Owner Author

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)

this.handleCopy = this.handleCopy.bind(this);
}

handleChange(event) {
Copy link

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);

Copy link
Owner Author

@Fafruch Fafruch Oct 15, 2017

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

handleChange(event) {
const newData = _.cloneDeep(this.state.data);
_.set(newData, event.target.name, event.target.value);
this.setState({ data: newData, isCopied: false });
Copy link

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 };
  })
}

Copy link
Owner Author

@Fafruch Fafruch Oct 15, 2017

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)?

Copy link

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.

handleChange(editedEvent);
}}
>
{[...new Array(31)].map((day, i) => <option key={i} value={i + 1}>{i + 1}</option>)}
Copy link

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];
Copy link

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?

Copy link
Owner Author

Choose a reason for hiding this comment

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

👍

className="form-control"
checked={mode === 'on'}
onChange={(event) => {
const editedEvent = { ...event, target: { ...event.target, value: 'on', name: event.target.name } };
Copy link

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

Copy link
Owner Author

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

};

const activeDays = [];
_.values(days).forEach((isDayActive, dayIndex) => {
Copy link

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

const computeYearlyOn = (on) => {
const repeat = {};

months.forEach((month, monthIndex) => {
Copy link

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

};
const computeYearlyOn = on => ({
bymonth: months.reduce(
(lastMonth, month, monthIndex) =>
Copy link
Owner Author

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

Copy link

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,

@Fafruch Fafruch merged commit ab00ff6 into master Oct 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants