-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Fredhase/codeclimate refactor onboarding js 3739 #5767
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.
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() { |
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.
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',
},
};
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 get the ideia of the addEventListener
, but I don't see where/how exactly to implement 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.
I'll post an example in a bit. Just in a meeting.
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'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
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 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'); |
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.
Since we're doing cleanup.
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'); |
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.
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() { |
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 exists in other parts of the codebase as well, but we should be defaulting to using addEventListener
.
narrowFeedButt.onclick = function() { | |
narrowFeedButt.addEventListener('click', function() { |
@@ -6,3 +6,16 @@ export const jsonToForm = data => { | |||
|
|||
export const getContentOfToken = token => |
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 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.
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.
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 }> |
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.
+1 for remembering the key
😎
touchcancel:function(e){nm=false} | ||
}; | ||
for(var a in touch){d.addEventListener(a,touch[a],false);} | ||
var nm = true; |
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 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.
hey @nickytonline |
Cool cool @fredhase . As mentioned, if you need some help with any of this, don’t hesitate to @ me. |
Closing this PR for some branche mistakes that caused conflict |
What type of PR is this? (check all applicable)
Description
Made a function for repetitive blocks in render function for app/javascript/onborading/components/ClosingSlide.tsx
Added to documentation?