Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 83 additions & 1 deletion src/filter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,87 @@
const sub = require('date-fns/sub');
const format = require('date-fns/format');
const startOfYear = require('date-fns/startOfYear');
const endOfYear = require('date-fns/endOfYear');
const startOfMonth = require('date-fns/startOfMonth');
const endOfMonth = require('date-fns/endOfMonth');

// spanish
const spanish = require('date-fns/locale/es');

// today
const today = new Date(2020, 0, 1);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

La fecha la estás fijando, esta debe de ser la que llega como parámetro de la función


// Last years
const lastYear = sub(today, {years:1});
const lastTwoYear = sub(today, {years:2});
const lastThreeYear = sub(today, {years:3});
Comment on lines +15 to +17
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cuando repites código similar es señal que debes implementar una función o una clase


// Last months
const lastMonth = sub(today, {months:1});
const lastTwoMonth = sub(today, {months:2});
const lastThreeMonth = sub(today, {months:3});
Comment on lines +20 to +22
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cuando repites código similar es señal que debes implementar una función o una clase


const makeFilter = (date) => {
let filters = [];

let filters = [
// Filters per days
{
label: 'Últimos 7 días',
startAt: format(sub(date, {days: 7}), 'yyyy/MM/dd'),
endAt: format(date, 'yyyy/MM/dd')
},
{
label: 'Últimos 28 días',
startAt: format(sub(date, {days: 28}), 'yyyy/MM/dd'),
endAt: format(date, 'yyyy/MM/dd')
},
{
label: 'Últimos 90 días',
startAt: format(sub(date, {days: 90}), 'yyyy/MM/dd'),
endAt: format(date, 'yyyy/MM/dd')
},
{
label: 'Últimos 365 días',
startAt: format(sub(date, {days: 365}), 'yyyy/MM/dd'),
endAt: format(date, 'yyyy/MM/dd')
},

// Filters per years
{
label: format(lastYear, 'yyyy'),
startAt: format(startOfYear(lastYear), 'yyyy/MM/dd'),
endAt: format(endOfYear(lastYear), 'yyyy/MM/dd')
},
{
label: format(lastTwoYear, 'yyyy'),
startAt: format(startOfYear(lastTwoYear), 'yyyy/MM/dd'),
endAt: format(endOfYear(lastTwoYear), 'yyyy/MM/dd')
},
{
label: format(lastThreeYear, 'yyyy'),
startAt: format(startOfYear(lastThreeYear), 'yyyy/MM/dd'),
endAt: format(endOfYear(lastThreeYear), 'yyyy/MM/dd')
},

// Filters per months
{
label: format(lastMonth, 'MMMM', {locale: spanish}),
startAt: format(startOfMonth(lastMonth), 'yyyy/MM/dd'),
endAt: format(endOfMonth(lastMonth), 'yyyy/MM/dd')
},
{
label: format(lastTwoMonth, 'MMMM', {locale: spanish}),
startAt: format(startOfMonth(lastTwoMonth), 'yyyy/MM/dd'),
endAt: format(endOfMonth(lastTwoMonth), 'yyyy/MM/dd')
},
{
label: format(lastThreeMonth, 'MMMM', {locale: spanish}),
startAt: format(startOfMonth(lastThreeMonth), 'yyyy/MM/dd'),
endAt: format(endOfMonth(lastThreeMonth), 'yyyy/MM/dd')
},
Comment on lines +50 to +81
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Estos datos no se actualizan con el parámetro date, porque ya los fijaste en la linea 12


];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cuando repites código similar es señal que debes implementar una función o una clase


return filters;
}

Expand Down