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

Fredhase/codeclimate refactor onboarding js 3739 #5767

Closed
wants to merge 7 commits into from
Closed

Fredhase/codeclimate refactor onboarding js 3739 #5767

wants to merge 7 commits into from

Conversation

fredhase
Copy link
Contributor

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update

Description

Made a function for repetitive blocks in render function for app/javascript/onborading/components/ClosingSlide.tsx

Added to documentation?

  • docs.dev.to
  • readme
  • no documentation needed

@pr-triage pr-triage bot added the PR: unreviewed bot applied label for PR's with no review label Jan 27, 2020
Copy link
Contributor

@nickytonline nickytonline left a comment

Choose a reason for hiding this comment

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

Thanks so much for the PR @fredhase! I've requested a few changes and also some suggestions.

drawerSliders.forEach(drawerSliders => {
const element = document.getElementById(drawerSlider.selector);
if (element) {
element.onclick = function() {
Copy link
Contributor

@nickytonline nickytonline Jan 27, 2020

Choose a reason for hiding this comment

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

Instead of having 4 click events, you can add one click event (via addEventListener) on the element with the ID on-page-nav-controls. From there you can compare the Event.target.id with the drawerSliders. To avoid looping through the array, I'd suggest making it an object instead, e.g.

const drawerSliders = {
  'sidebar-bg-left': { swipeState: 'middle', side: 'left', view: 'outOfView' },
  'sidebar-bg-right': {
    swipeState: 'middle',
    side: 'right',
    view: 'outOfView',
  },
  'on-page-nav-butt-left': {
    swipeState: 'left',
    side: 'left',
    view: 'intoView',
  },
  'on-page-nav-butt-right': {
    swipeState: 'right',
    side: 'right',
    view: 'intoView',
  },
};

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I get the ideia of the addEventListener, but I don't see where/how exactly to implement this. 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll post an example in a bit. Just in a meeting.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've made some testing here. I'll post it.
Using the array, it works fine:

`if (document.getElementById('on-page-nav-controls')) {
const drawerSliders = [
{ selector: 'sidebar-bg-left', swipeState: 'middle', side: 'left', view: 'outOfView' },
{ selector: 'sidebar-bg-right', swipeState: 'middle', side: 'right', view: 'outOfView' },
{ selector: 'on-page-nav-butt-left', swipeState: 'left', side: 'left', view: 'intoView' },
{ selector: 'on-page-nav-butt-right', swipeState: 'right', side: 'right', view: 'intoView' },
];

drawerSliders.forEach((drawerSlider) => {
  const element = document.getElementById(drawerSlider.selector);
  if (element) {
    element.onclick = function() {
      swipeState = drawerSlider.swipeState;
      slideSidebar(drawerSlider.side, drawerSlider.view);
    };
  }
});
body.addEventListener('click', () => {
  slideSidebar('right', 'outOfView');
  slideSidebar('left', 'outOfView');
});
listenForNarrowMenuClick();

}
}`

But using as an object, the side bar doesn't close when I click elsewhere:

` const element = document.getElementById('on-page-nav-controls')

if (element) {
const drawerSliders = {
'sidebar-bg-left': { swipeState: 'middle', side: 'left', view: 'outOfView' },
'sidebar-bg-right': { swipeState: 'middle', side: 'right', view: 'outOfView' },
'on-page-nav-butt-left': { swipeState: 'left', side: 'left', view: 'intoView' },
'on-page-nav-butt-right': { swipeState: 'right', side: 'right', view: 'intoView' },
};

element.addEventListener('click', (e) => {
  const drawerSlider = drawerSliders[e.target.id]
  swipeState = drawerSlider.swipeState;
  slideSidebar(drawerSlider.side, drawerSlider.view);
});

body.addEventListener('click', () => {
  slideSidebar('right', 'outOfView');
  slideSidebar('left', 'outOfView');
});
listenForNarrowMenuClick();

}
}`

Any suggestions or observations?
Also, meanwhile I'm working on the others changes

Copy link
Contributor

Choose a reason for hiding this comment

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

This is what I meant if using an object for drawer sliders, but the same could be done with the array. The only difference with the array is you'd need to loop through or do a .find to, get the right slider when comparing it to the clicked element's className. My assumption for this is there is only that one class name, which may not be correct. If there is more than one class name, looping through the array may make more sense to find the right drawer slider.

const navControls = document.getElementById("on-page-nav-controls");

const drawerSliders = {
  'sidebar-bg-left': { swipeState: 'middle', side: 'left', view: 'outOfView' },
  'sidebar-bg-right': {.
    swipeState: 'middle',
    side: 'right',
    view: 'outOfView',
  },
  'on-page-nav-butt-left': {
    swipeState: 'left',
    side: 'left',
    view: 'intoView',
  },
  'on-page-nav-butt-right': {
    swipeState: 'right',
    side: 'right',
    view: 'intoView',
  },
};

navControls.addEventListener('click', e => {
  // e.target.className would be for example 'sidebar-bg-left'
  const drawerSlider = drawerSliders[e.target.className]

  if (!drawerSlider) {
    // a drawer slider was not clicked.
    return;
  }

  const { side, view } = drawerSlider;

  slideSidebar(side, view);

  // This appears to be a global as it is not set in this file
  swipeState = drawerSlider.swipeState;
});

}
}
});

InstantClick.on('change', function() {
document.getElementsByTagName('body')[0].classList.remove('modal-open');
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we're doing cleanup.

Suggested change
document.getElementsByTagName('body')[0].classList.remove('modal-open');
document.body.classList.remove('modal-open');

var narrowFeedButt = document.getElementById("narrow-feed-butt");
for (var i = 0; i < navLinks.length; i++) {
document.getElementById("narrow-nav-menu").classList.remove("showing");
var navLinks = document.getElementsByClassName('narrow-nav-menu');
Copy link
Contributor

Choose a reason for hiding this comment

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

If you have time, it'd be nice to migrate var to const and let where necessary. I use const as a default, but there are no strong opinions on this in the codebase.

narrowFeedButt.onclick = function(){
document.getElementById("narrow-nav-menu").classList.add("showing");
}
narrowFeedButt.onclick = function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

This exists in other parts of the codebase as well, but we should be defaulting to using addEventListener.

Suggested change
narrowFeedButt.onclick = function() {
narrowFeedButt.addEventListener('click', function() {

@@ -6,3 +6,16 @@ export const jsonToForm = data => {

export const getContentOfToken = token =>
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't believe we use this anywhere else but for the CSRF token, so consider just naming this getCsrfToken and omit the token parameter and hard-code csrf-token in the function. If in the future we need to get the content of different meta tags, then we should consider promoting this to a more generic function like you currently have.

Copy link
Contributor

@nickytonline nickytonline Jan 27, 2020

Choose a reason for hiding this comment

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

Also, consider adding some unit tests to these functions as well. Happy to help on this front.


return(
properties.map((element, index) =>
<a key={ index } href={ element.href } data-no-instant={(index === 0) ? true : null }>
Copy link
Contributor

Choose a reason for hiding this comment

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

+1 for remembering the key 😎

touchcancel:function(e){nm=false}
};
for(var a in touch){d.addEventListener(a,touch[a],false);}
var nm = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

I see prettier in action here, and it looks like this code was probably pulled from somewhere minified. It looks like it's just registering events, but if you have more insight, consider adding a comment about this.

@pr-triage pr-triage bot added PR: reviewed-changes-requested bot applied label for PR's where reviewer requests changes and removed PR: unreviewed bot applied label for PR's with no review labels Jan 27, 2020
@fredhase
Copy link
Contributor Author

hey @nickytonline
Thanks for the reply and review :)
I'll see what I can do, but it's very nice, not only for the project, but also for me, as an intern and begginer, to grow and learn more. I'll try my best to give the best result

@nickytonline
Copy link
Contributor

hey @nickytonline
Thanks for the reply and review :)
I'll see what I can do, but it's very nice, not only for the project, but also for me, as an intern and begginer, to grow and learn more. I'll try my best to give the best result

Cool cool @fredhase . As mentioned, if you need some help with any of this, don’t hesitate to @ me.

@fredhase
Copy link
Contributor Author

Closing this PR for some branche mistakes that caused conflict

@fredhase fredhase closed this Feb 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: reviewed-changes-requested bot applied label for PR's where reviewer requests changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants