What's new
HTML Forums | An HTML and CSS Coding Community

Welcome to HTMLForums; home of web development discussion! Please sign in or register your free account to get involved. Once registered you will be able to connect with other members, send and receive private messages, reply to topics and create your very own. Our registration process is hassle-free and takes no time at all!

Label New Update appears after i upload new version

malveiro75

New member
Hello , i'm newbie in this forum and i need some help

I'm trying to insert a New Update Label in my html file , so , i have csv file in a format dd_mm_yyyy , in a shared folder that is uploaded in my page , and when i make an update , the label must appear . Example , In my page it appears the verison that is upload , 14_09_2024 , but if i have a 16_09_2024.csv , the label appears .
My problem , is the label disappear when i upload the new version , but i hit F5 , it appears .
Here is my code :
function checkForNewFile() {
var storedFileName = localStorage.getItem('latestFileName');
var fileNamePattern = /\d{2}_\d{2}_\d{4}/; // Regex for dd_mm_yyyy format
var currentDate = currentFileName.match(fileNamePattern)[0];

// Check if a new update flag exists
var newUpdateFlag = localStorage.getItem('newUpdateFlag');

if (storedFileName && storedFileName !== currentFileName) {
document.getElementById('newUpdateLabel').style.display = 'inline'; // Show new update label
localStorage.setItem('newUpdateFlag', 'true'); // Store that an update is available
} else if (storedFileName === currentFileName) {
document.getElementById('newUpdateLabel').style.display = 'none'; // Hide the label if no update
localStorage.removeItem('newUpdateFlag'); // Ensure the flag is cleared if up to date
}
}
// Load CSV data from localStorage on page load
window.onload = function() {
var storedCSV = localStorage.getItem('csvData');
var storedFileName = localStorage.getItem('csvFileName');
var latestFileName = localStorage.getItem('latestFileName'); // Latest uploaded file name

if (storedCSV && storedFileName) {
parseCSV(storedCSV);
currentFileName = getFileNameWithoutExtension(storedFileName);
displayVersion(currentFileName); // Display CSV version
document.getElementById('status').textContent = "Using stored CSV data.";
document.getElementById('searchSection').style.display = 'block';
document.getElementById('uploadSection').style.display = 'none';
document.getElementById('resetSection').style.display = 'block';

// If no latest file is stored yet, store the current uploaded file as latest
if (!latestFileName) {
localStorage.setItem('latestFileName', storedFileName);
}

// Check for new file
checkForNewFile();
}
};


// Upload CSV and Parse the Data
document.getElementById('uploadButton').addEventListener('click', function() {
var fileInput = document.getElementById('csvFile');
var file = fileInput.files[0];
if (file) {
var reader = new FileReader();
reader.onload = function(e) {
var csvData = e.target.result;
localStorage.setItem('csvData', csvData);
localStorage.setItem('csvFileName', file.name); // Store the filename
localStorage.setItem('latestFileName', file.name); // Update latest file in localStorage
currentFileName = getFileNameWithoutExtension(file.name);
displayVersion(currentFileName); // Display the CSV version
parseCSV(csvData);

// Clear the "New Update Available" label after upload
document.getElementById('newUpdateLabel').style.display = 'none';
localStorage.removeItem('newUpdateFlag'); // Clear the stored flag

document.getElementById('status').textContent = "File uploaded successfully!";
document.getElementById('searchSection').style.display = 'block';
document.getElementById('uploadSection').style.display = 'none';
document.getElementById('resetSection').style.display = 'block';
};
reader.readAsText(file);
} else {
document.getElementById('status').textContent = "Please choose a file.";
}
});

What i'm doing wrong ??
Thanks in advance
 
Back
Top