Skip to content

Commit

Permalink
Fix date selector for browsers that aren't Chrome
Browse files Browse the repository at this point in the history
  • Loading branch information
jayvarner committed Sep 20, 2024
1 parent 3275ed1 commit 6819aa7
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions app/views/admin/letters/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,18 @@ It renders the `_table` partial to display details about the resources.
const minField = document.getElementById('start_date');
const maxField = document.getElementById('end_date');
try {
minField.value = moment('<%= start_date %>'.replaceAll('-', '/')).format('DD-MM-YYYY');
maxField.value = moment('<%= end_date %>'.replaceAll('-', '/')).format('DD-MM-YYYY');
minField.value = moment('<%= start_date %>'.replaceAll('-', '/').split(" ")[0], "YYYY/MMM/DD").format('DD-MM-YYYY');
maxField.value = moment('<%= end_date %>'.replaceAll('-', '/').split(" ")[0], "YYYY/MMM/DD").format('DD-MM-YYYY');
const startPicker = new datepicker("#start_date", {
formatter: (input, date, instance) => {
const value = moment(date).format('DD-MM-YYYY');
input.value = value;
},
minDate: new Date('<%= min_date %>'),
maxDate: new Date('<%= max_date %>'),
minDate: new Date('<%= min_date %>'.split(" ")[0]),
maxDate: new Date('<%= max_date %>'.split(" ")[0]),
onSelect: (instance) => {
const newMin = moment(instance.dateSelected).format('YYYY-MM-DD');
const newMax = moment('<%= end_date %>'.replaceAll('-', '/')).format('YYYY-MM-DD');
const newMax = moment('<%= end_date %>'.replaceAll('-', '/').split(" ")[0], "YYYY/MMM/DD").format('YYYY-MM-DD');
const newPath = `/admin/letters?start_date=${newMin}&end_date=${newMax}&search=<%= search_term %>`;
window.open(newPath, '_self');
}
Expand All @@ -104,10 +104,10 @@ It renders the `_table` partial to display details about the resources.
const value = moment(date).format('DD-MM-YYYY');
input.value = value;
},
minDate: new Date('<%= min_date %>'),
maxDate: new Date('<%= max_date %>'),
minDate: new Date('<%= min_date %>'.split(" ")[0]),
maxDate: new Date('<%= max_date %>'.split(" ")[0]),
onSelect: (instance) => {
const newMin = moment('<%= start_date %>'.replaceAll('-', '/')).format('YYYY-MM-DD');
const newMin = moment('<%= start_date %>'.replaceAll('-', '/').split(" ")[0], "YYYY/MMM/DD").format('YYYY-MM-DD');
const newMax = moment(instance.dateSelected).format('YYYY-MM-DD');
const newPath = `/admin/letters?start_date=${newMin}&end_date=${newMax}&search=<%= search_term %>`;
window.open(newPath, '_self');
Expand All @@ -117,17 +117,18 @@ It renders the `_table` partial to display details about the resources.
startPicker.navigate('<%= start_date %>', true);
endPicker.navigate('<%= end_date %>', true);
} catch (error) {
setTimeout(() => {
initDateFilter();
}, 300);
console.error(error)
// setTimeout(() => {
// initDateFilter();
// }, 300);
}
}

defer(() => {
initDateFilter();
const urlParams = new URLSearchParams(window.location.search);
urlParams.set('start_date', moment('<%= start_date %>'.replaceAll('-', '/')).format('YYYY-MM-DD'));
urlParams.set('end_date', moment('<%= end_date %>'.replaceAll('-', '/')).format('YYYY-MM-DD'));
urlParams.set('start_date', moment('<%= start_date %>'.replaceAll('-', '/').split(" ")[0], "YYYY/MMM/DD").format('YYYY-MM-DD'));
urlParams.set('end_date', moment('<%= end_date %>'.replaceAll('-', '/').split(" ")[0], "YYYY/MMM/DD").format('YYYY-MM-DD'));
urlParams.set('search', '<%= search_term %>');
history.replaceState({}, null, `/admin/letters?${urlParams.toString()}`);
})
Expand Down

0 comments on commit 6819aa7

Please sign in to comment.