- Simplified date matching logic by using Moment.js

This commit is contained in:
Vlad
2016-05-25 17:04:41 -04:00
parent 603b6749de
commit 3888241cbd

View File

@@ -6,9 +6,10 @@ define(
'mailpoet', 'mailpoet',
'handlebars', 'handlebars',
'papaparse', 'papaparse',
'select2',
'asyncqueue', 'asyncqueue',
'xss' 'xss',
'moment',
'select2'
], ],
function ( function (
Backbone, Backbone,
@@ -18,7 +19,8 @@ define(
Handlebars, Handlebars,
Papa, Papa,
AsyncQueue, AsyncQueue,
xss xss,
Moment
) { ) {
if (!jQuery('#mailpoet_subscribers_import').length) { if (!jQuery('#mailpoet_subscribers_import').length) {
return; return;
@@ -924,31 +926,9 @@ define(
// DATE filter: if column type is date, check if we can recognize it // DATE filter: if column type is date, check if we can recognize it
if (column.type === 'date' && matchedColumn !== -1) { if (column.type === 'date' && matchedColumn !== -1) {
jQuery.map(subscribersClone.subscribers, function (data, position) { jQuery.map(subscribersClone.subscribers, function (data, position) {
var rowData = data[matchedColumn], var rowData = data[matchedColumn];
date = new Date(rowData.replace(/-/g, '/')), // IE doesn't like
// dashes as date separators
month_name = [
MailPoet.I18n.t('january'),
MailPoet.I18n.t('february'),
MailPoet.I18n.t('march'),
MailPoet.I18n.t('april'),
MailPoet.I18n.t('may'),
MailPoet.I18n.t('june'),
MailPoet.I18n.t('july'),
MailPoet.I18n.t('august'),
MailPoet.I18n.t('september'),
MailPoet.I18n.t('october'),
MailPoet.I18n.t('november'),
MailPoet.I18n.t('december')
];
if (position !== fillterPosition) { if (position !== fillterPosition) {
// check for valid date: // check if date exists
// * invalid date object returns NaN for getTime() and NaN
// is the only object not strictly equal to itself
// * date must have period/dash/slash OR be at least 4
// characters long (e.g., year)
// * must be before now
if (rowData.trim() === '') { if (rowData.trim() === '') {
data[matchedColumn] = data[matchedColumn] =
'<span class="mailpoet_data_match mailpoet_import_error" title="' '<span class="mailpoet_data_match mailpoet_import_error" title="'
@@ -958,25 +938,12 @@ define(
preventNextStep = true; preventNextStep = true;
return; return;
} }
else if (date.getTime() === date.getTime() && // check if date is valid and is before today
(/[.-\/]/.test(rowData) || rowData.length >= 4) && if (Moment(rowData).isValid() && Moment(rowData).isBefore(Moment())) {
date.getTime() < (new Date()).getTime()
) {
date = '/ '
+ month_name[date.getMonth()]
+ ' ' + date.getDate() + ', '
+ date.getFullYear() + ' '
+ date.getHours() + ':'
+ ((date.getMinutes() < 10 ? '0' : '')
+ date.getMinutes()) + ' '
+ ((date.getHours() >= 12)
? MailPoet.I18n.t('pm')
: MailPoet.I18n.t('am')
);
data[matchedColumn] += data[matchedColumn] +=
'<span class="mailpoet_data_match" title="' '<span class="mailpoet_data_match" title="'
+ MailPoet.I18n.t('verifyDateMatch') + '">' + MailPoet.I18n.t('verifyDateMatch') + '">'
+ MailPoet.Date.format(date) + '</span>'; + MailPoet.Date.format(rowData) + '</span>';
} }
else { else {
data[matchedColumn] += data[matchedColumn] +=