fixing code quality

This commit is contained in:
Amine Ben hammou
2018-02-15 16:32:09 +00:00
committed by pavel-mailpoet
parent cfc425dfd7
commit 86aff26d5f

View File

@@ -2,7 +2,6 @@ import React from 'react';
import _ from 'underscore';
import MailPoet from 'mailpoet';
import { confirmAlert } from 'react-confirm-alert';
import classNames from 'classnames';
import Breadcrumb from 'newsletters/breadcrumb.jsx';
import HelpTooltip from 'help-tooltip.jsx';
@@ -270,7 +269,6 @@ class ImportTemplate extends React.Component {
'MailPoet Free version': window.mailpoet_version,
});
} catch (err) {
console.error(err)
MailPoet.Notice.error(MailPoet.I18n.t('templateFileMalformedError'));
}
};
@@ -312,11 +310,10 @@ class NewsletterTemplates extends React.Component {
templates: {}, // {category1: [template11, template12, ..], category2: [template21, ...]}
selectedTab: '',
};
this.templates = {};
}
componentWillMount() {
let templates = {};
MailPoet.Ajax.post({
api_version: window.mailpoet_api_version,
endpoint: 'newsletterTemplates',
@@ -334,15 +331,8 @@ class NewsletterTemplates extends React.Component {
},
];
}
templates = response.data.reduce(this.addTemplate, {});
for(const category in templates) {
templates[category].sort((a, b) =>
parseInt(a.id) < parseInt(b.id) ? 1 : -1
);
}
response.data.forEach(this.addTemplate.bind(this));
this.sortTemplates();
}).fail((response) => {
if (response.errors.length > 0) {
MailPoet.Notice.error(
@@ -351,11 +341,11 @@ class NewsletterTemplates extends React.Component {
);
}
}).always(() => {
this.selectInitialCategory(templates);
this.selectInitialTab();
});
}
addTemplate(templates, template) {
addTemplate(template) {
const categoriesNames = templatesCategories.map(category => category.name);
let categories;
@@ -372,16 +362,26 @@ class NewsletterTemplates extends React.Component {
categories.push('saved');
}
return categories.reduce((templates, category) => {
if (templates[category] === undefined) {
templates[category] = [];
categories.forEach((category) => {
if (this.templates[category] === undefined) {
this.templates[category] = [];
}
templates[category].unshift(template);
return templates;
}, templates);
this.templates[category].unshift(template);
});
}
selectInitialCategory(templates) {
sortTemplates() {
Object.keys(this.templates).forEach((category) => {
this.templates[category].sort((a, b) => {
if (parseInt(a.id, 10) < parseInt(b.id, 10)) {
return 1;
}
return -1;
});
});
}
selectInitialTab() {
let selectedTab = 'standard';
MailPoet.Ajax.post({
api_version: window.mailpoet_api_version,
@@ -401,7 +401,7 @@ class NewsletterTemplates extends React.Component {
}
}).always(() => {
this.setState({
templates: templates,
templates: this.templates,
selectedTab: selectedTab,
loading: false,
});
@@ -409,28 +409,26 @@ class NewsletterTemplates extends React.Component {
}
afterTemplateDelete(success, id) {
let templates = this.state.templates;
if (success) {
for (const category in templates) {
templates[category] = templates[category].filter(template => template.id != id);
}
Object.keys(this.templates).forEach((category) => {
this.templates[category] = this.templates[category].filter(template => template.id !== id);
});
}
this.setState({
templates: templates,
templates: this.templates,
loading: false,
});
}
afterTemplateImport(success, importedTemplate) {
let templates = this.state.templates;
afterTemplateImport(success, template) {
if (success) {
templates = this.addTemplate(templates, importedTemplate, true);
this.addTemplate(template);
}
this.setState({
templates: templates,
templates: this.templates,
selectedTab: success ? 'saved' : 'import',
loading: false,
})
});
}
render() {
@@ -445,16 +443,18 @@ class NewsletterTemplates extends React.Component {
const selectedTab = this.state.selectedTab;
let content = null;
if (selectedTab === 'import') {
content = <ImportTemplate
content = (
<ImportTemplate
afterImport={afterTemplateImport}
setLoading={value => this.setState({ loading: value })}
/>;
/>
);
} else {
let templates = this.state.templates[this.state.selectedTab] || [];
if (templates.length === 0) {
templates = <p>{MailPoet.I18n.t('noTemplates')}</p>;
} else {
templates = templates.map((template, index) =>
templates = templates.map((template, index) => (
<TemplateBox
key={index}
index={index}
@@ -463,7 +463,7 @@ class NewsletterTemplates extends React.Component {
setLoading={value => this.setState({ loading: value })}
{...template}
/>
)
));
}
content = <ul className="mailpoet_boxes clearfix">{templates}</ul>;
}