Merge pull request #1252 from mailpoet/templates
filtering unknown templates categories [MAILPOET-1281]
This commit is contained in:
@ -128,7 +128,7 @@ const NewsletterTemplates = React.createClass({
|
|||||||
getInitialState: function () {
|
getInitialState: function () {
|
||||||
return {
|
return {
|
||||||
loading: false,
|
loading: false,
|
||||||
templates: {},
|
templates: {}, // {category1: [template11, template12, ..], category2: [template21, ...]}
|
||||||
selectedCategory: '',
|
selectedCategory: '',
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
@ -159,18 +159,27 @@ const NewsletterTemplates = React.createClass({
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
let templates = templatesCategories.reduce((result, { name }) => {
|
const templates = {};
|
||||||
const obj = result;
|
const categoriesNames = templatesCategories.map(category => category.name);
|
||||||
obj[name] = [];
|
categoriesNames.forEach((name) => {
|
||||||
return obj;
|
templates[name] = [];
|
||||||
}, {});
|
});
|
||||||
|
|
||||||
templates = response.data.reduce((result, item) => {
|
response.data.forEach((template) => {
|
||||||
JSON.parse(item.categories).forEach((category) => {
|
let categories;
|
||||||
result[category].push(item);
|
try {
|
||||||
|
categories = JSON.parse(template.categories)
|
||||||
|
.filter(name => categoriesNames.indexOf(name) !== -1);
|
||||||
|
} catch (err) {
|
||||||
|
categories = [];
|
||||||
|
}
|
||||||
|
if (categories.length === 0) { // the template has no known category
|
||||||
|
categories = ['saved']; // we add it to "Your saved templates"
|
||||||
|
}
|
||||||
|
categories.forEach((category) => {
|
||||||
|
templates[category].push(template);
|
||||||
});
|
});
|
||||||
return result;
|
});
|
||||||
}, templates);
|
|
||||||
|
|
||||||
this.selectInitialCategory(templates);
|
this.selectInitialCategory(templates);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user