load previous uploads from localStorage

This commit is contained in:
Daniela Arcese 2017-06-23 15:07:02 -04:00
parent 0b4301be9e
commit b673fad703

View File

@ -17,7 +17,7 @@ $(document).ready(function() {
for(let i=0; i<localStorage.length; i++) { for(let i=0; i<localStorage.length; i++) {
let id = localStorage.key(i); let id = localStorage.key(i);
checkUploads(id); populateFileList(localStorage.getItem(id));
} }
// copy link to clipboard // copy link to clipboard
@ -73,15 +73,21 @@ $(document).ready(function() {
fileSender.upload().then(info => { fileSender.upload().then(info => {
const url = info.url.trim() + `#${info.secretKey}`.trim(); const url = info.url.trim() + `#${info.secretKey}`.trim();
$('#link').attr('value', url); $('#link').attr('value', url);
const fileData = {
localStorage.setItem(info.fileId, info.deleteToken); name: file.name,
fileId: info.fileId,
url: info.url,
secretKey: info.secretKey,
deleteToken: info.deleteToken
};
localStorage.setItem(info.fileId, JSON.stringify(fileData));
$('#page-one').hide(); $('#page-one').hide();
$('#file-list').hide(); $('#file-list').hide();
$('#upload-progress').hide(); $('#upload-progress').hide();
$('#share-link').show(); $('#share-link').show();
checkUploads(info.fileId, url); populateFileList(JSON.stringify(fileData));
}); });
}; };
@ -90,7 +96,7 @@ $(document).ready(function() {
}; };
//load previous uploads //load previous uploads
function checkUploads(id, url='') { function checkUploads(file) {
return new Promise ((resolve, reject) => { return new Promise ((resolve, reject) => {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.responseType = 'json'; xhr.responseType = 'json';
@ -115,8 +121,9 @@ $(document).ready(function() {
} }
//update file table with current files in localStorage //update file table with current files in localStorage
function populateFileList(file, url) { function populateFileList(file) {
console.log(file); file = JSON.parse(file);
const $fileList = $('#uploaded-files'); const $fileList = $('#uploaded-files');
const row = document.createElement('tr'); const row = document.createElement('tr');
const name = document.createElement('td'); const name = document.createElement('td');
@ -135,7 +142,7 @@ $(document).ready(function() {
// create delete button // create delete button
btn.innerHTML = 'x'; btn.innerHTML = 'x';
btn.classList.add('delete-btn'); btn.classList.add('delete-btn');
link.innerHTML = url; link.innerHTML = file.url.trim() + `#${file.secretKey}`.trim();;
// create popup // create popup
popupDiv.classList.add('popup'); popupDiv.classList.add('popup');
@ -147,7 +154,7 @@ $(document).ready(function() {
$popupText.find('.del-file').click(e => { $popupText.find('.del-file').click(e => {
FileSender.delete( FileSender.delete(
file.fileId, file.fileId,
localStorage.getItem(file.fileId) file.deleteToken
).then(() => { ).then(() => {
$(e.target).parents('tr').remove(); $(e.target).parents('tr').remove();
localStorage.removeItem(file.fileId); localStorage.removeItem(file.fileId);