You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.3 KiB
83 lines
2.3 KiB
4 years ago
|
import * as Pikaday from 'pikaday'
|
||
|
import moment from 'moment'
|
||
|
import $ from 'jquery'
|
||
2 years ago
|
import Cookies from 'js-cookie'
|
||
4 years ago
|
|
||
|
const DATE_FORMAT = 'YYYY-MM-DD'
|
||
|
|
||
|
const $button = $('#export-csv-button')
|
||
|
|
||
|
// eslint-disable-next-line
|
||
|
const _instance1 = new Pikaday({
|
||
|
field: $('.js-datepicker-from')[0],
|
||
|
onSelect: (date) => onSelect(date, 'from_period'),
|
||
|
defaultDate: moment().add(-1, 'months').toDate(),
|
||
|
setDefaultDate: true,
|
||
|
maxDate: new Date(),
|
||
|
format: DATE_FORMAT
|
||
|
})
|
||
|
|
||
|
// eslint-disable-next-line
|
||
|
const _instance2 = new Pikaday({
|
||
|
field: $('.js-datepicker-to')[0],
|
||
|
onSelect: (date) => onSelect(date, 'to_period'),
|
||
|
defaultDate: new Date(),
|
||
|
setDefaultDate: true,
|
||
|
maxDate: new Date(),
|
||
|
format: DATE_FORMAT
|
||
|
})
|
||
|
|
||
|
$button.on('click', () => {
|
||
|
// eslint-disable-next-line
|
||
2 years ago
|
const recaptchaResponse = grecaptcha.getResponse()
|
||
|
if (recaptchaResponse) {
|
||
|
$button.addClass('spinner')
|
||
|
$button.prop('disabled', true)
|
||
|
const downloadUrl = `${$button.data('link')}&recaptcha_response=${recaptchaResponse}`
|
||
|
|
||
|
$('body').append($('<iframe id="csv-iframe" style="display: none;"></iframe>'))
|
||
|
$('#csv-iframe').attr('src', downloadUrl)
|
||
|
|
||
|
const interval = setInterval(handleCSVDownloaded, 1000)
|
||
|
setTimeout(resetDownload, 60000)
|
||
|
|
||
|
function handleCSVDownloaded () {
|
||
|
if (Cookies.get('csv-downloaded') === 'true') {
|
||
|
resetDownload()
|
||
4 years ago
|
}
|
||
2 years ago
|
}
|
||
|
|
||
|
function resetDownload () {
|
||
|
$button.removeClass('spinner')
|
||
|
$button.prop('disabled', false)
|
||
|
clearInterval(interval)
|
||
|
Cookies.remove('csv-downloaded')
|
||
|
// eslint-disable-next-line
|
||
|
grecaptcha.reset()
|
||
|
}
|
||
4 years ago
|
}
|
||
|
})
|
||
|
|
||
|
function onSelect (date, paramToReplace) {
|
||
|
const formattedDate = moment(date).format(DATE_FORMAT)
|
||
|
|
||
|
if (date) {
|
||
3 years ago
|
const csvExportPath = $button.data('link')
|
||
4 years ago
|
|
||
3 years ago
|
const updatedCsvExportUrl = replaceUrlParam(csvExportPath, paramToReplace, formattedDate)
|
||
4 years ago
|
$button.data('link', updatedCsvExportUrl)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function replaceUrlParam (url, paramName, paramValue) {
|
||
|
if (paramValue == null) {
|
||
|
paramValue = ''
|
||
|
}
|
||
3 years ago
|
const pattern = new RegExp('\\b(' + paramName + '=).*?(&|#|$)')
|
||
4 years ago
|
if (url.search(pattern) >= 0) {
|
||
|
return url.replace(pattern, '$1' + paramValue + '$2')
|
||
|
}
|
||
|
url = url.replace(/[?#]$/, '')
|
||
|
return url + (url.indexOf('?') > 0 ? '&' : '?') + paramName + '=' + paramValue
|
||
|
}
|