Last active
September 26, 2017 17:54
-
-
Save mattduggan/11b018f2720cec0efcc4a664e8be809c to your computer and use it in GitHub Desktop.
Feature Tour API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
//::base_url:: | |
class ArgumentError extends Error {} | |
const states = { | |
DISMISSED: "dismissed", | |
FINISHED: "finished", | |
SHOW: "show", | |
SHOW_LATER: "show_later" | |
}; | |
// Service | |
const FeatureTourService = { | |
getFeatureTours() { | |
return Yesware | |
.get("Request") | |
.request({ | |
url: BASE + "api/v1/feature_tours/feature_tours", | |
type: "GET", | |
includeAuthToken: true | |
}) | |
.then(function(response) { | |
return response.feature_tours; | |
}) | |
.catch(function(reason) { | |
console.error(reason); | |
throw reason; | |
}); | |
}, | |
updateFeatureTour(feature_tour) { | |
return Yesware | |
.get("Request") | |
.request({ | |
url: BASE + "api/v1/feature_tours/feature_tours/" + feature_tour.id, | |
type: "PUT", | |
includeAuthToken: true, | |
data: { feature_tour } | |
}) | |
.catch(function(reason) { | |
console.error(reason); | |
throw reason; | |
}); | |
} | |
}; | |
// Controller | |
class FeatureTourController { | |
dismiss(featureTour) { | |
return this.updateFeatureTour( | |
Object.assign(featureTour, {state: states.DISMISSED}) | |
); | |
} | |
finish(featureTour) { | |
return this.updateFeatureTour( | |
Object.assign(featureTour, {state: states.FINISHED}) | |
); | |
} | |
show(featureTour) { | |
return this.updateFeatureTour( | |
Object.assign(featureTour, {state: states.SHOW}) | |
); | |
} | |
showLater(featureTour, showAfter) { | |
return this.updateFeatureTour( | |
Object.assign( | |
featureTour, | |
{state: states.SHOW_LATER, show_after: showAfter} | |
) | |
); | |
} | |
updateFeatureTour(featureTour) { | |
return FeatureTourService | |
.updateFeatureTour(featureTour); | |
} | |
} | |
// Module | |
const FeatureTourManager = (function() { | |
function getFeatureTours() { | |
return FeatureTourService | |
.getFeatureTours() | |
.then(function(featureTours) { | |
return featureTours | |
.filter(function(featureTour) { | |
return ( | |
tour.status === states.SHOW | |
|| tour.status === states.SHOW_LATER | |
); | |
}) | |
.sort(function(a, b) { | |
return (a.show_after < b.show_after); | |
}); | |
}); | |
} | |
function start() { | |
getFeatureTours() | |
.then((featureTours) => { | |
if (featureTours.length) { | |
const current = featureTours.shift(); | |
const view = featureTourViewFactory(current, new FeatureTourController()); | |
const delay = Math.max(new Date(current.show_after).getTime()-new Date(current.show_after).getTime(), 60000); | |
view.on("destroy", start); | |
setTimeout(view.show, delay); | |
} | |
}); | |
} | |
})(); | |
// View Factory | |
function featureTourViewFactory(featureTour, featureTourController) { | |
let view; | |
switch(featureTour.tour_key) { | |
case "20170920_crm": | |
view = new CRMFeatureTourView(featureTour, featureTourController); | |
break; | |
default: | |
throw new ArgumentError(`${tour.tour_key} is not a known FeatureTour`); | |
break; | |
} | |
return view; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment