Give and receive constructive feedback from your fellow campers on Algorithm Challenge solutions, Frontend and Backend projects
function handleRevealerAnim({...args}, cb){
return setTimeout(function() {
args.target.classList.add(args.anim)
cb && cb();
}, args.ms);
}
function createLi(pages){
const paginationContainer = document.querySelector('.pagination');
for(var i=0; i < pages.length; i++){
const targetText = pages[i].childNodes[3].childNodes[1].innerText;
const LI = document.createElement('LI');
const NUM = document.createElement('SPAN');
const TEXTCONTAINER = document.createElement('SPAN');
const textNode = document.createTextNode(targetText);
const textNodeIndex = document.createTextNode(i);
LI.appendChild(NUM);
i === 0 ? LI.classList.add('pagination-li', 'active-pagination') : LI.classList.add('pagination-li');
i === 0 ? TEXTCONTAINER.classList.add('pagination-title', 'active-text') : TEXTCONTAINER.classList.add('pagination-title', 'none')
LI.appendChild(TEXTCONTAINER);
TEXTCONTAINER.appendChild(textNode)
NUM.appendChild(textNodeIndex)
paginationContainer.appendChild(LI);
}
}
(function() {
const section = document.querySelector('.section')
const tab = document.querySelector('body')
const li = document.getElementsByClassName('pagination-li');
let pages = document.getElementsByClassName('work'),
currentPage = 0,
prev;
createLi(pages)
handleRevealerAnim({
target: section.childNodes[3].children[0],
ms: 300,
anim: 'scale-out-hor-right'
});
handleRevealerAnim({
target: section.childNodes[5].children[0],
ms: 400,
anim: 'scale-out-ver-top'
});
handleRevealerAnim({
target: section.childNodes[1],
ms: 400,
anim: 'slide-out-left'
});
handleRevealerAnim({
target: section,
ms: 2000,
anim: 'slide-out-left'
}, () => pages[currentPage].style.display = 'block');
window.addEventListener('wheel', function(e) {
console.log(e.deltaY)
handleSections(e.deltaY)
});
const links = document.querySelectorAll('a');
for(let a = 0; a < links.length; a++){
links[a].addEventListener('click', function(e) {
console.log(10)
});
}
function handleSections(delta){
var direction = (delta < 0) ? 'up' : 'down';
if(direction == 'down' && currentPage <= pages.length - 2){
prev = currentPage;
currentPage = currentPage + 1;
scrollIntoSection({
prev,
currentPage,
pages
})
} else if(direction == 'up' && currentPage > 0) {
prev = currentPage;
currentPage--;
scrollIntoSection({
prev,
currentPage,
pages
})
}
}
function scrollIntoSection({...opts}){
opts.pages[opts.prev].childNodes[1].classList.add('scale-out-hor-right');
setTimeout(function(){
opts.pages[opts.prev].childNodes[1].classList.remove('scale-out-hor-right');
li[opts.prev].classList.remove('active-pagination');
li[opts.prev].childNodes[1].classList.remove('active-text')
opts.pages[opts.prev].style.display = 'none';
opts.pages[opts.currentPage].style.display = 'block';
li[opts.currentPage].classList.add('active-pagination');
li[opts.currentPage].childNodes[1].classList.add('active-text');
}, 500)
opts.pages[opts.currentPage].scrollIntoView({behavior: "smooth", block: "start", inline: "nearest"});
}
})();
// service
export const getUserByUsername = username => new Promise(async function(resolve, reject) {
const userData = await dbgetUserByUsername(username);
const user = userModelFactory(userData);
if(validateUser(user)) {
resolve(user);
} else {
reject();
}
});
//model
export const dbgetUserByUsername = username => getdb().collection('users').find({ username }).toArray((err, result) => (err || result));
//route
router.get('/user/:username', handleasyncreq(req => getUserByUsername(req.params.username)
.then(result => ({ body: result }))
.catch(result => ({ body: result, status: 400 }))
));
//handleasyncreq code
export function handleasyncreq(f) {
async function asynchandler(req, res) {
const { status = 200, headers = {}, body = {} } = await f(req);
res.status(status).set(headers).json(body);
}
return asynchandler;
}
@abeledovictor I personally feel like the code you posted it too complex for its own good. Your mixing async/await
with normal promises, your own async handler function, and new Promise
, even tho you have async/await
code.
For such a simple case, you should be fine with just the route, service and maybe the model. I also don't believe in using .then
and .catch
if your using async/await
. It's more important to stay consistent than to do "one-liners" for the same of one-liners.
Great code is dirt easy to read and understand. Don't be clever/shorter just for the sake of it.
without getting to verbose (I'm to lazy to write out all your code hehe)
I would have this:
// route
router.get('/user/:username', (req, res) => {
try {
const {username} = req.params;
const user = await getUserByUsername(username);
res.send({status: 200, body: user});
} catch (err) {
res.send({body: err, status: 400});
}
});
// getUserByUsername service
// NOTE: if your using async await, you automatically return promises from this function
export const getUserByUsername =(username) => {
const user = userModelFactory(dbGetUserByUsername(username)); // this function is the same
if (validateUser(user) {
return user;
}
throw Error();
}
The dbGetUserByUsername
function is the same