Skip to content

Commit

Permalink
Date Filter Pre Selects
Browse files Browse the repository at this point in the history
  • Loading branch information
calebtocco committed Jun 27, 2024
1 parent 49ea19c commit a53f69d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/radCommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { DateSetup } from './dateSetup';
import { RadSearch } from './radSearch';
import { BatchActions } from './batchActions';
import { Duplicates } from './duplicates';
import { RadSearchDateFilter } from './radSearchDateFilter';

export class RadCommon {
static setup() {
Expand All @@ -36,6 +37,7 @@ export class RadCommon {
RadSearch.setup();
Duplicates.setup();
BatchActions.setup();
RadSearchDateFilter.setup();
});
}

Expand Down
59 changes: 59 additions & 0 deletions src/radSearchDateFilter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import moment from 'moment';
import $ from 'jquery';

export class RadSearchDateFilter {
static setup() {
const setDateRange = (target, range) => {
let startDate, endDate;
switch (range) {
case 'today':
startDate = moment().format('YYYY-MM-DD');
endDate = moment().format('YYYY-MM-DD');
break;
case 'this_week':
startDate = moment().startOf('week').format('YYYY-MM-DD');
endDate = moment().endOf('week').format('YYYY-MM-DD');
break;
case 'this_month':
startDate = moment().startOf('month').format('YYYY-MM-DD');
endDate = moment().endOf('month').format('YYYY-MM-DD');
break;
case 'this_year':
startDate = moment().startOf('year').format('YYYY-MM-DD');
endDate = moment().endOf('year').format('YYYY-MM-DD');
break;
case 'yesterday':
startDate = moment().subtract(1, 'days').format('YYYY-MM-DD');
endDate = moment().subtract(1, 'days').format('YYYY-MM-DD');
break;
case 'last_week':
startDate = moment().subtract(1, 'weeks').startOf('week').format('YYYY-MM-DD');
endDate = moment().subtract(1, 'weeks').endOf('week').format('YYYY-MM-DD');
break;
case 'last_month':
startDate = moment().subtract(1, 'months').startOf('month').format('YYYY-MM-DD');
endDate = moment().subtract(1, 'months').endOf('month').format('YYYY-MM-DD');
break;
case 'last_year':
startDate = moment().subtract(1, 'years').startOf('year').format('YYYY-MM-DD');
endDate = moment().subtract(1, 'years').endOf('year').format('YYYY-MM-DD');
break;
}

const parentContainer = $(target.parentElement);
if (parentContainer.length) {
const filter = parentContainer.data('filter-target');
const startInput = $(`[name="search[${filter}_start]"]`);
const endInput = $(`[name="search[${filter}_end]"]`);
startInput.val(startDate);
endInput.val(endDate);
}
}

$('.search-date-filter .dropdown-item').on('click', function(event) {
event.preventDefault();
const range = $(this).data('range');
setDateRange(this, range);
});
}
}

0 comments on commit a53f69d

Please sign in to comment.