Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

js: users module refactoring #1233

Merged
merged 1 commit into from
Mar 14, 2017
Merged

Conversation

vitoravelino
Copy link
Contributor

@vitoravelino vitoravelino commented Mar 4, 2017

So, this is something I've been thinking about for a while since I started contributing to Portus. After #1238 were merged, we are now able to write js using the new ES2015 syntax. This will improve the code readability and maintainability.

One of the coolest feature on ES2015 is the module support. The folder structure I'm proposing is something similar to:

javascripts/
    modules/
        (...)      
        namespaces/
        teams/
            components/
                form.js
                list.js
            pages/
                show.js
                list.js
            index.js
    application.js
modules

The index.js in the teamsfolder is simple and will be responsiable to know which which page component should be used on the loaded page. Example:

import TeamsShow from './pages/show';
import TeamsIndex from './pages/index';

// we can move this to a separated file 
// and import as `import { TEAMS_SHOW...
const TEAMS_SHOW_ROUTE = 'teams/show';
const TEAMS_INDEX_ROUTE = 'teams/index';

$(() => {
  const $body = $('body');
  const route = $body.data('route');

  if (route === TEAMS_SHOW_ROUTE) {
    TeamShow.render($body);
  }

  if (route === TEAMS_INDEX_ROUTE) {
    TeamIndex.render($body);
  }

  (...)

I know that it's quite a poor technique but since we have the module system on our side, it guarantees that only the code involved on the page component will be executed.

A BaseComponent was created as an artifact to avoid code duplication and create some convention.

Suggestions are more than welcome. 😃

@vitoravelino vitoravelino changed the title ES2015 support [wip] ES2015 support Mar 4, 2017
@vitoravelino
Copy link
Contributor Author

Tests are not passing because coverage got reduced. Any chance that we could expose the coverage report on coveralls or anything similar?

I have problem testing portusctl and couldn't reproduce the same coverage problem.


if ($('body section.sign-up, body section.login').length) {
rndNum = Math.floor((Math.random() * 2) + 1);
$('body').addClass('massive-background-' + rndNum).hide().fadeIn(1000);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Any idea what this massive-background-n is/was used for? I didn't find anything.

@vitoravelino
Copy link
Contributor Author

Most of the work is done. I'd appreciate some feedback.

@vitoravelino vitoravelino changed the title [wip] ES2015 support ES2015 support Mar 5, 2017
@mssola
Copy link
Collaborator

mssola commented Mar 6, 2017

@vitoravelino I'll review this PR later, but in principle it looks like a nice idea. In the meantime take a look at #1238. This is still WIP and it's something I've done during the weekend, and I think that it clashes a bit with your PR (not too much). My idea would be to get that one merged before yours, because the importing of modules will change (and also note that using webpack allows us to ignore the dependency on the execjs gem). After that we can review this one.

@vitoravelino
Copy link
Contributor Author

@mssola Oh, that looks great. I didn't consider webpack because I thought it would add another thing to the build process and that would be an issue.

I'll take a look at your work. As soon as you merge your PR I'll rebase my branch and you can review it. Feeling excited for both changes. Woohooo! 😄

@mssola
Copy link
Collaborator

mssola commented Mar 9, 2017

@vitoravelino the PRs regarding webpack and ruby 2.4 have been merged. You can go ahead 👍

@vitoravelino vitoravelino changed the title ES2015 support users: ES2015 refactoring Mar 9, 2017
@vitoravelino
Copy link
Contributor Author

@mssola I've rebased and fixed the issues but there are some feature tests failing in travis but passing on localhost. It doesn't make sense. Any idea what could be? =/

@vitoravelino
Copy link
Contributor Author

Enabled js_errors and I got a clue. Working on it.

@vitoravelino
Copy link
Contributor Author

Finally figured out the problem. For some unknown (at least for me) reason this.onKeyup.bind(this) was throwing an error on travis but working in localhost. First, I tried to change it to use $.proxy(this.onKeyup, this)and it worked. Since we have ES2015 syntax support, I preferred to use e => this.onKeyup(e) instead because it's cleaner.

I know that the project is moving forward to use a library to manage the view layer but the structure is not gonna change much from what I've written here. 😃

@vitoravelino vitoravelino force-pushed the es2015-support branch 3 times, most recently from 66a0bbe to 0b93aac Compare March 11, 2017 11:26
Copy link
Collaborator

@mssola mssola left a comment

Choose a reason for hiding this comment

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

In general it looks great to me. Just fix my comments (the documentation comment applies to all classes btw, I didn't want to repeat myself after some point :D) 😉

const APP_TOKEN_FORM = '#add_application_token_form';
const APP_TOKEN_FIELD = '#application_token_application';

class ApplicationTokenPanel extends BaseComponent {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you add documentation ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there any format/rule that I should follow?

const NEW_CONFIRMATION_PASSWORD_FIELD = '#user_password_confirmation';
const SUBMIT_BUTTON = 'input[type=submit]';

class UsersPasswordForm extends BaseComponent {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here

const DISPLAY_NAME_FIELD = '#user_display_name';
const SUBMIT_BUTTON = 'input[type=submit]';

class UsersProfileForm extends BaseComponent {
Copy link
Collaborator

Choose a reason for hiding this comment

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

ditto

.form-group
.col-md-offset-2.col-md-7
= f.submit('Create', class: 'btn btn-primary')
.app-token-wrapper
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of wrapping the form, why don't you simply add this class into the form itself ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The idea is to have an element that contains both the link and the form. I know I could just add classes/ids to the elements and access from the global scope but restricting the component to access only the this.$el give us more control of what's going on and also a good practice.

Ideally, panel and form could be separated components and when user clicked on the link an event is triggered and the component that contains both panel and form will tell the form to show itself. What do you think?

But yeah, wrapping them in a component to restrict dom element scope would still be needed.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Got it. Thanks

@@ -1,130 +1,130 @@
// to render the layout correctly in every browser/screen
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure I understand what changed here 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Me neither. Maybe because of the rebase? LOL. 😅

Copy link
Collaborator

Choose a reason for hiding this comment

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

Then, rebase again and checkout -- this file 😉

@vitoravelino
Copy link
Contributor Author

Added some documentation. I would appreciate some guidance since I don't know what you are expecting.

@vitoravelino vitoravelino changed the title users: ES2015 refactoring js: users module refactoring Mar 13, 2017
@mssola
Copy link
Collaborator

mssola commented Mar 14, 2017

@vitoravelino you overdid it! 😄 So, the documentation should be one the same lines as the ruby code: human explanation of what it does, and when there is a parameter explain what is it expecting and all that. You can take the LDAP class as reference 😉 So, avoid all the @ references and such, it should be simpler.

@mssola
Copy link
Collaborator

mssola commented Mar 14, 2017

Also, for comments I'd only use // instead of /**

@vitoravelino
Copy link
Contributor Author

KKKKK. Will do. 😅

@vitoravelino vitoravelino force-pushed the es2015-support branch 2 times, most recently from c1ec5fa to 72d2839 Compare March 14, 2017 13:05
@vitoravelino
Copy link
Contributor Author

@mssola Done. Let me know if it still needs improvement, please.

.babelrc Outdated
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm ok with this, but I think that we already provide the babel config through the package.json file no ? Why would we need this ? (asking just in case we can avoid duplication)

Copy link
Contributor Author

@vitoravelino vitoravelino Mar 14, 2017

Choose a reason for hiding this comment

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

I didn't see the package.json config. I prefer having babel config on their own file. This is actually from my work of adding ES2015 support that I forgot to delete it.

I just realized that we might not need both of them since you already set

query: {
    presets: ['es2015'],
},

on the babel-loader config. Should I remove both or just .babelrc?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe just the babelrc file.

@@ -0,0 +1,53 @@
/* eslint-disable class-methods-use-this */

//
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can remove this line and the one in the end. So, all comments can be like this:

// BaseComponent class to avoid boilerplate code
// into component classes and add some convention.
//
// The subclasses are not obligated to implement all
// of these methods but if they need to perform an action
// that fits one of the descriptions below, it's highly
// recommended to use the proper method.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This same thing applies to the other classes, functions, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

.form-group
.col-md-offset-2.col-md-7
= f.submit('Create', class: 'btn btn-primary')
.app-token-wrapper
Copy link
Collaborator

Choose a reason for hiding this comment

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

Got it. Thanks

@@ -1,130 +1,130 @@
// to render the layout correctly in every browser/screen
Copy link
Collaborator

Choose a reason for hiding this comment

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

Then, rebase again and checkout -- this file 😉

Copy link
Collaborator

@mssola mssola left a comment

Choose a reason for hiding this comment

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

Just remove the .babelrc file and squash your commits and I'll merge it. Thanks for the patience! 👍

Signed-off-by: Vítor Avelino <[email protected]>
@vitoravelino
Copy link
Contributor Author

No worries, @mssola. 😄

@mssola mssola merged commit f524db1 into SUSE:master Mar 14, 2017
@mssola
Copy link
Collaborator

mssola commented Mar 14, 2017

Thanks 👏

@vitoravelino vitoravelino deleted the es2015-support branch April 25, 2018 12:01
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants