Story 5.3: persistance localStorage

This commit is contained in:
2026-02-04 21:10:40 +01:00
parent fb95a39792
commit a4e0cb71e9
7 changed files with 222 additions and 42 deletions

57
assets/js/state.js Normal file
View File

@@ -0,0 +1,57 @@
/**
* Gestionnaire d'état pour le localStorage
*/
const AppState = {
STORAGE_KEY: 'portfolio_contact_form',
EXCLUDED_FIELDS: ['csrf_token', 'password', 'recaptcha_token'],
isStorageAvailable() {
try {
const test = '__storage_test__';
localStorage.setItem(test, test);
localStorage.removeItem(test);
return true;
} catch (e) {
return false;
}
},
saveFormData(data) {
if (!this.isStorageAvailable()) return;
try {
const filteredData = {};
Object.keys(data).forEach((key) => {
if (!this.EXCLUDED_FIELDS.includes(key)) {
filteredData[key] = data[key];
}
});
localStorage.setItem(this.STORAGE_KEY, JSON.stringify(filteredData));
} catch (e) {
console.warn('Impossible de sauvegarder dans localStorage:', e);
}
},
getFormData() {
if (!this.isStorageAvailable()) return null;
try {
const data = localStorage.getItem(this.STORAGE_KEY);
return data ? JSON.parse(data) : null;
} catch (e) {
console.warn('Impossible de charger depuis localStorage:', e);
return null;
}
},
clearFormData() {
if (!this.isStorageAvailable()) return;
try {
localStorage.removeItem(this.STORAGE_KEY);
} catch (e) {
// Silencieux
}
}
};