Skip to content

Instantly share code, notes, and snippets.

@najibghadri
Created August 24, 2020 13:25
Show Gist options
  • Save najibghadri/f46a39f02fcece637a16b775651ab188 to your computer and use it in GitHub Desktop.
Save najibghadri/f46a39f02fcece637a16b775651ab188 to your computer and use it in GitHub Desktop.
// Here at Prezi we work a lot with templates across all our products
// Consider a backend system called template-service. This service has the following API:
//
// /api/v1/templates/:
// params:
// - product__in - products of content we would like to read
// - type__in - type of template we would like to read
// response:
// [{
// key: string,
// popularity: number,
// product: 'presentation' | 'design' | 'video',
// type?: 'dashboard' | 'infographic' | 'chart',
// }, ...]
//
// Please provide a method that will return the number of templates for each product. For designs,
// this should be broken down to types.
// Please provide a method that will return the 5 most popular templates as well, regardless of types.
function loadTemplates(products, types) {
return fetch(`/api/v1/templates/?product__in=${products.join(',')}&type__in=${types.join(',')}`);
}
function templateCount() {
const result = {
presentation: 0,
design: {
dashboard: 0,
infographic: 0,
chart: 0
},
video: 0
};
const response = await loadTemplates(['presentation','design','video'], ['dashboard','infographic','chart']);
response.forEach(template => {
if(template.product === "presentation"){
result.presentation++;
} else if(template.product === "video") {
result.video++;
} else if(template.product=== "design"){
result.design[template.type]++;
}
});
return result;
}
function fiveMostPopularTemplates() {
const response = await loadTemplates(['presentation','design','video'], ['dashboard','infographic','chart']);
response.sort((t1,t2)=> {
return t2.popularity - t1.popularity;
});
return response.slice(0,5);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment