Change map logic and view

This commit is contained in:
Berkeley Martinez
2015-12-04 22:13:14 -08:00
parent 4c1d84cbf4
commit ab043d1c6d
2 changed files with 93 additions and 71 deletions

View File

@ -84,6 +84,26 @@ function updateUserProgress(user, challengeId, completedChallenge) {
return user; return user;
} }
// small helper function to determine whether to mark something as new
const dateFormat = 'MMM MMMM DD, YYYY';
function shouldShowNew(element, block) {
if (element) {
return typeof element.releasedOn !== 'undefined' &&
moment(element.releasedOn, dateFormat).diff(moment(), 'days') >= -30;
}
if (block) {
const newCount = block.reduce((sum, { markNew }) => {
if (markNew) {
return sum + 1;
}
return sum;
}, 0);
return newCount / block.length * 100 === 100;
}
}
module.exports = function(app) { module.exports = function(app) {
const router = app.loopback.Router(); const router = app.loopback.Router();
@ -113,23 +133,31 @@ module.exports = function(app) {
.shareReplay(); .shareReplay();
// create a stream of challenge blocks // create a stream of challenge blocks
const blocks$ = challenge$ const superBlocks$ = challenge$
.map(challenge => challenge.toJSON()) .map(challenge => challenge.toJSON())
// group challenges by block | returns a stream of observables // group challenges by block | returns a stream of observables
.groupBy(challenge => challenge.block) .groupBy(challenge => challenge.block)
// turn block group stream into an array // turn block group stream into an array
.flatMap(block$ => block$.toArray()) .flatMap(blocks$ => blocks$.toArray())
// turn array into stream of object // turn array into stream of object
.map(blockArray => ({ .map(blockArray => ({
name: blockArray[0].block, name: blockArray[0].block,
dashedName: dasherize(blockArray[0].block), dashedName: dasherize(blockArray[0].block),
challenges: blockArray challenges: blockArray,
superBlock: blockArray[0].superBlock
})) }))
.filter(({ name })=> { .filter(({ name })=> {
return name !== 'Hikes'; return name !== 'Hikes';
}) })
.groupBy(block => block.superBlock)
.flatMap(superBlocks$ => superBlocks$.toArray())
.shareReplay(); .shareReplay();
const blocks$ = superBlocks$
.flatMap(superBlock => Observable.just(superBlock))
.shareReplay();
superBlocks$.subscribe(() => {});
const User = app.models.User; const User = app.models.User;
const userCount$ = observeMethod(User, 'count'); const userCount$ = observeMethod(User, 'count');
@ -455,23 +483,6 @@ module.exports = function(app) {
function challengeMap({ user = {} }, res, next) { function challengeMap({ user = {} }, res, next) {
// small helper function to determine whether to mark something as new
function shouldShowNew(element, block) {
if (element) {
return (typeof element.releasedOn !== 'undefined' &&
moment(element.releasedOn, 'MMM MMMM DD, YYYY')
.diff(moment(), 'days') >= -30);
} else if (block) {
const newCount = block.reduce((sum, { markNew }) => {
if (markNew) {
return sum + 1;
}
return sum;
}, 0);
return newCount / block.length * 100 === 100;
}
}
let lastCompleted; let lastCompleted;
const daysRunning = moment().diff(new Date('10/15/2014'), 'days'); const daysRunning = moment().diff(new Date('10/15/2014'), 'days');
@ -485,7 +496,7 @@ module.exports = function(app) {
.map(camperCount => numberWithCommas(camperCount)); .map(camperCount => numberWithCommas(camperCount));
// create a stream of an array of all the challenge blocks // create a stream of an array of all the challenge blocks
const blocks$ = challenge$ const superBlocks$ = challenge$
// mark challenge completed // mark challenge completed
.map(challengeModel => { .map(challengeModel => {
const challenge = challengeModel.toJSON(); const challenge = challengeModel.toJSON();
@ -511,6 +522,7 @@ module.exports = function(app) {
return { return {
isBeta, isBeta,
name: blockArray[0].block, name: blockArray[0].block,
superBlock: blockArray[0].superBlock,
dashedName: dasherize(blockArray[0].block), dashedName: dasherize(blockArray[0].block),
markNew: shouldShowNew(null, blockArray), markNew: shouldShowNew(null, blockArray),
challenges: blockArray, challenges: blockArray,
@ -521,22 +533,31 @@ module.exports = function(app) {
.filter(({ name }) => name !== 'Hikes') .filter(({ name }) => name !== 'Hikes')
// turn stream of blocks into a stream of an array // turn stream of blocks into a stream of an array
.toArray() .toArray()
.doOnNext((blocks) => { .doOnNext(blocks => {
const lastCompletedBlock = _.findLast(blocks, (block) => { const lastCompletedBlock = _.findLast(blocks, (block) => {
return block.completed === 100; return block.completed === 100;
}); });
lastCompleted = lastCompletedBlock && lastCompletedBlock.name || null; lastCompleted = lastCompletedBlock && lastCompletedBlock.name || null;
}); })
.flatMap(blocks => Observable.from(blocks, null, null, Scheduler.default))
.groupBy(block => block.superBlock)
.flatMap(blocks$ => blocks$.toArray())
.map(superBlockArray => ({
name: superBlockArray[0].superBlock,
blocks: superBlockArray
}))
.toArray();
Observable.combineLatest( Observable.combineLatest(
camperCount$, camperCount$,
blocks$, superBlocks$,
(camperCount, blocks) => ({ camperCount, blocks }) (camperCount, superBlocks) => ({ camperCount, superBlocks })
) )
.subscribe( .subscribe(
({ camperCount, blocks }) => { ({ camperCount, superBlocks }) => {
console.log('sup', superBlocks);
res.render('challengeMap/show', { res.render('challengeMap/show', {
blocks, superBlocks,
daysRunning, daysRunning,
globalCompletedCount: numberWithCommas( globalCompletedCount: numberWithCommas(
5612952 + (Math.floor((Date.now() - 1446268581061) / 2000)) 5612952 + (Math.floor((Date.now() - 1446268581061) / 2000))

View File

@ -45,35 +45,38 @@ block content
.row .row
.col-xs-12.col-sm-8.col-sm-offset-2 .col-xs-12.col-sm-8.col-sm-offset-2
h3 800 Hours of Practice: h3 800 Hours of Practice:
- var i = 0 ol
for challengeBlock in blocks for superBlock in superBlocks
- i++ h1= superBlock.name
.row - var i = 0
if (user) for challengeBlock in superBlock.blocks
if (challengeBlock.completed === 100) - i++
.hidden-xs.col-sm-3.col-md-2.text-primary.ion-checkmark-circled.padded-ionic-icon.text-center.map-p.negative-10 .row
.col-xs-1.col-sm-1.col-md-1.map-row-numbers if (user)
span.map-p.negative-10 #{i}. if (challengeBlock.completed === 100)
.col-xs-11.col-sm-8.col-md-9 .hidden-xs.col-sm-3.col-md-2.text-primary.ion-checkmark-circled.padded-ionic-icon.text-center.map-p.negative-10
span.map-p.faded.negative-10 .col-xs-1.col-sm-1.col-md-1.map-row-numbers
a(href='#' + challengeBlock.dashedName)= challengeBlock.name span.map-p.negative-10 #{i}.
if challengeBlock.markNew .col-xs-11.col-sm-8.col-md-9
span.text-danger.small     li.map-p.faded.negative-10
strong a(href='#' + challengeBlock.dashedName)= challengeBlock.name
em NEW if challengeBlock.markNew
else span.text-danger.small    
.hidden-xs.col-sm-3.col-md-2 strong
.progress.progress-bar-padding.text-center.thin-progress-bar em NEW
.progress-bar(role='progressbar', aria-valuenow=(challengeBlock.completed), aria-valuemin='0', aria-valuemax='100', style='width: ' + challengeBlock.completed + '%;') else
.col-xs-1.col-sm-1.col-md-1.map-row-numbers .hidden-xs.col-sm-3.col-md-2
span.map-p.negative-10 #{i}. .progress.progress-bar-padding.text-center.thin-progress-bar
.col-xs-11.col-sm-8.col-md-9 .progress-bar(role='progressbar', aria-valuenow=(challengeBlock.completed), aria-valuemin='0', aria-valuemax='100', style='width: ' + challengeBlock.completed + '%;')
span.map-p.negative-10 .col-xs-1.col-sm-1.col-md-1.map-row-numbers
a(href='#' + challengeBlock.dashedName)= challengeBlock.name span.map-p.negative-10 #{i}.
if challengeBlock.markNew .col-xs-11.col-sm-8.col-md-9
span.text-danger.small     li.map-p.negative-10
strong a(href='#' + challengeBlock.dashedName)= challengeBlock.name
em NEW if challengeBlock.markNew
span.text-danger.small    
strong
em NEW
else else
.hidden-xs.col-sm-3.col-md-2 .hidden-xs.col-sm-3.col-md-2
.col-xs-1.col-sm-1.col-md-1.map-row-numbers .col-xs-1.col-sm-1.col-md-1.map-row-numbers
@ -117,21 +120,19 @@ block content
.col-xs-10.col-sm-8.col-md-9 .col-xs-10.col-sm-8.col-md-9
span.map-p.negative-10 300-hour Nonprofit Project span.map-p.negative-10 300-hour Nonprofit Project
hr hr
for superBlock in superBlocks
for challengeBlock in superBlock.blocks
.row
a(href='#' name=challengeBlock.dashedName)
.spacer.negative-55
for challengeBlock in blocks .row
.row .hidden-xs.col-sm-3.col-md-2
a(href='#' name=challengeBlock.dashedName) h3.text-primary.text-right.nowrap
.spacer.negative-55 i.fa.fa-clock-o
= challengeBlock.time
.row .col-xs-12.col-sm-9.col-md-10
.hidden-xs.col-sm-3.col-md-2 h3 #{challengeBlock.name}  
h3.text-primary.text-right.nowrap
i.fa.fa-clock-o
= challengeBlock.time
.col-xs-12.col-sm-9.col-md-10
h3 #{challengeBlock.name}  
- var i = 0 - var i = 0
for challenge in challengeBlock.challenges for challenge in challengeBlock.challenges
- i++ - i++