Created
August 2, 2018 18:03
-
-
Save fhdez/6b0a9525631699258580ecc56a73c960 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vm.downloadcsv = function() { | |
$http({ | |
method: 'GET', | |
url: '/api/v1/activities/record/'+vm.query.domain.id+'/csv', | |
params: { | |
'datesince': vm.query.datesince, | |
'dateto':vm.query.dateto, | |
'verbs':vm.query.verb | |
}, | |
responseType: 'arraybuffer' | |
}).success(function (data, status, headers, response) { | |
$localStorage.downloading_csv = { | |
status: true, | |
task_id : response.taskId | |
}; | |
updateCSVStatus(); | |
swal("La carga masiva de usuarios esta en curso...", "", "success"); | |
headers = headers(); | |
var filename = headers['x-filename']; | |
var contentType = headers['content-type']; | |
var linkElement = document.createElement('a'); | |
try { | |
var blob = new Blob([data], { type: contentType }); | |
var url = window.URL.createObjectURL(blob); | |
linkElement.setAttribute('href', url); | |
linkElement.setAttribute("download", filename); | |
var clickEvent = new MouseEvent("click", { | |
"view": window, | |
"bubbles": true, | |
"cancelable": false | |
}); | |
linkElement.dispatchEvent(clickEvent); | |
} catch (ex) { | |
console.log(ex); | |
} | |
}).error(function (data, response) { | |
console.log(data); | |
$localStorage.loading_csv = { | |
status: false | |
}; | |
var errorMessage = "Hubo un problema con la descarga."; | |
if(response && response.nonFieldErrors.length > 0){ | |
errorMessage = response.nonFieldErrors[0]; | |
} | |
vm.loading_csv = $localStorage.loading_csv.status; | |
swal(errorMessage, "", "error"); | |
}); | |
}; | |
var updateCSVStatus = function(){ | |
var downloading_csv = $localStorage.downloading_csv; | |
var taskId = downloading_csv ? downloading_csv.task_id : null; | |
if(taskId){ | |
var url = '/api/v1/tasks/' + taskId + '/status'; | |
$http.get(url) | |
.success(function(response){ | |
if (response.task.status == 'SUCCESS') { | |
$localStorage.downloading_csv = { | |
status: false | |
}; | |
vm.downloading_csv = false; | |
} else { | |
$timeout(updateCSVStatus, 1000); | |
vm.downloading_csv = $localStorage.downloading_csv.status; | |
} | |
}) | |
.error(function(response){ | |
$localStorage.downloading_csv = { | |
status: false | |
}; | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment