#6 - Localstorage

Using localstorage to store last session information (todos, nb of todos finished, nb sessions, song, theme color, etc.).
This commit is contained in:
RemiSaurel
2022-08-29 14:19:57 +02:00
parent 3d142efc54
commit 98b759abcf
6 changed files with 65 additions and 17 deletions
+27 -9
View File
@@ -18,7 +18,7 @@
</div> </div>
<div id="btn-newword"> <div id="btn-newword">
<button @click="getNewWordEveryday" id="fetch" class="btn"> <button @click="getNewWordEveryday" id="fetch" class="btn">
Generate a word 📝 Generate a word
</button> </button>
</div> </div>
<hr> <hr>
@@ -26,16 +26,22 @@
<div id="stats"> <div id="stats">
<div class="center" v-if="displayWord"> <div class="center" v-if="displayWord">
<button @click="displayWord = false" class="btn"> <button @click="displayWord = false" class="btn">
Session stats 📊 Session stats
</button> </button>
</div> </div>
<div v-else> <div v-else>
Total todo: {{ this.nbItemsFinished }} Total todo: {{ this.nbItemsFinished }}
<br> <br>
Total sessions: {{ this.$store.getters.nbSessions }} Total sessions: {{ this.nbSessions }}
<br>
<span id="hint">(from last session)</span>
<div class="center"> <div class="center">
<button @click="resetStats" class="btn" id="reset">
🔁 Reset
</button>
<br>
<button class="btn" id="see-words" @click="displayWord = true"> <button class="btn" id="see-words" @click="displayWord = true">
English word 🇬🇧 English word
</button> </button>
</div> </div>
</div> </div>
@@ -134,6 +140,9 @@ export default {
console.log(error); console.log(error);
}); });
}, },
resetStats() {
this.$store.commit('resetStats');
},
}, },
mounted() { mounted() {
this.generateDateOfTheDay(); this.generateDateOfTheDay();
@@ -141,15 +150,15 @@ export default {
this.getNewWordEveryday(); this.getNewWordEveryday();
}, },
computed: { computed: {
itemsFinished() {
return this.$store.getters.itemsFinished;
},
nbItemsFinished() { nbItemsFinished() {
return this.$store.getters.itemsFinished.length; return parseInt(this.$store.getters.globalState.nbItemsFinished) + this.$store.getters.itemsFinished.length
}, },
colorSelected() { colorSelected() {
return this.$store.getters.colorSelected; return this.$store.getters.colorSelected;
}, },
nbSessions() {
return parseInt(this.$store.getters.globalState.nbSessions) + this.$store.getters.nbSessions
}
}, },
watch: { watch: {
colorSelected: function(color) { colorSelected: function(color) {
@@ -216,10 +225,19 @@ export default {
text-align: center; text-align: center;
} }
#see-words { #hint {
font-size: 18px;
font-weight: lighter;
}
#reset {
margin-top: 16px; margin-top: 16px;
} }
#see-words {
margin-top: 8px;
}
/* ********** */ /* ********** */
/* RESPONSIVE */ /* RESPONSIVE */
/* ********** */ /* ********** */
+1
View File
@@ -136,6 +136,7 @@ export default {
alert("SESSION TERMINÉE 👏"); alert("SESSION TERMINÉE 👏");
this.nbSessionsFinished++; this.nbSessionsFinished++;
this.$store.commit("updateNbSessionsFinished"); this.$store.commit("updateNbSessionsFinished");
localStorage.setItem("nbSessions", this.$store.getters.nbSessions);
if (this.nbSessionsFinished === 4) { if (this.nbSessionsFinished === 4) {
this.setupPauseTimer(LONG_PAUSE); this.setupPauseTimer(LONG_PAUSE);
this.nbSessionsFinished = 0; this.nbSessionsFinished = 0;
+5 -4
View File
@@ -2,7 +2,7 @@
<div id="settings-container"> <div id="settings-container">
<div id="color-palette" class="bubble-container"> <div id="color-palette" class="bubble-container">
<div v-for="color in colors" :key="color.id"> <div v-for="color in colors" :key="color.id">
<div class="bubble-item" :style="{backgroundColor: color, borderColor: activeColor}" @click="setBackgroundColor($event, color)"></div> <div class="bubble-item" :id="color" :style="{backgroundColor: color, borderColor: activeColor}" @click="setBackgroundColor($event, color)"></div>
</div> </div>
</div> </div>
</div> </div>
@@ -30,6 +30,7 @@ export default {
event.target.style.borderColor = this.activeColor; event.target.style.borderColor = this.activeColor;
document.body.style.backgroundColor = color; document.body.style.backgroundColor = color;
this.$store.commit("setColorSelected", color); this.$store.commit("setColorSelected", color);
localStorage.setItem("themeColor", this.$store.getters.colorSelected)
}, },
removeActiveColorFromPalette() { removeActiveColorFromPalette() {
const palette = document.querySelectorAll(".bubble-item"); const palette = document.querySelectorAll(".bubble-item");
@@ -38,9 +39,9 @@ export default {
} }
}, },
setFirstColor() { setFirstColor() {
// get first div of color-palette const color = this.$store.getters.globalState.themeColor;
const palette = document.querySelectorAll(".bubble-item"); document.getElementById(color).style.borderColor = this.activeColor;
palette[0].style.borderColor = this.activeColor; document.body.style.backgroundColor = color;
} }
}, },
mounted() { mounted() {
+8 -1
View File
@@ -32,7 +32,7 @@ export default {
data() { data() {
return { return {
item: "", item: "",
items: [] items: JSON.parse(this.$store.getters.globalState.currentItems)
} }
}, },
methods: { methods: {
@@ -47,6 +47,7 @@ export default {
} }
this.sortArrayPinnedElements() this.sortArrayPinnedElements()
this.clearItem() this.clearItem()
this.saveCurrentItems()
}, },
removeItem(item) { removeItem(item) {
this.items = this.items.filter(e => this.items = this.items.filter(e =>
@@ -54,6 +55,8 @@ export default {
) )
this.$store.commit("addItemFinished", item); this.$store.commit("addItemFinished", item);
this.clearItem() this.clearItem()
this.saveCurrentItems()
localStorage.setItem("nbItemsFinished", this.$store.getters.itemsFinished.length);
}, },
clearItem() { clearItem() {
this.item = "" this.item = ""
@@ -69,6 +72,7 @@ export default {
list.scrollTo({top: 0, behavior: 'smooth'}); list.scrollTo({top: 0, behavior: 'smooth'});
} }
this.sortArrayPinnedElements() this.sortArrayPinnedElements()
this.saveCurrentItems()
} }
}, },
sortArrayPinnedElements() { sortArrayPinnedElements() {
@@ -81,6 +85,9 @@ export default {
return 0 return 0
} }
}) })
},
saveCurrentItems(){
localStorage.setItem("currentItems", JSON.stringify(this.items));
} }
} }
} }
+8 -2
View File
@@ -30,7 +30,7 @@ export default {
livesId: [], livesId: [],
easterEggs: [], easterEggs: [],
easterEggsSongsId: -1, easterEggsSongsId: -1,
songId: 0, songId: this.$store.getters.globalState.currentSongIndex,
muted: true muted: true
} }
}, },
@@ -42,11 +42,13 @@ export default {
}, },
nextPlaylist() { nextPlaylist() {
this.songId++ === this.livesId.length - 1 ? this.songId = 0 : this.songId; this.songId++ === this.livesId.length - 1 ? this.songId = 0 : this.songId;
localStorage.setItem("currentSongIndex", this.songId);
let id = this.livesId[this.songId]; let id = this.livesId[this.songId];
this.loadVideoFromApi(id) this.loadVideoFromApi(id)
}, },
prevPlaylist() { prevPlaylist() {
--this.songId === -1 ? this.songId = this.livesId.length - 1 : this.songId; --this.songId === -1 ? this.songId = this.livesId.length - 1 : this.songId;
localStorage.setItem("currentSongIndex", this.songId);
let id = this.livesId[this.songId]; let id = this.livesId[this.songId];
this.loadVideoFromApi(id) this.loadVideoFromApi(id)
}, },
@@ -73,13 +75,17 @@ export default {
this.muted = !this.muted; this.muted = !this.muted;
}, },
loadVideoFromApi(id) { loadVideoFromApi(id) {
this.saveLocalStorage(id);
player.loadVideoById(id); player.loadVideoById(id);
}, },
saveLocalStorage(id) {
localStorage.setItem("lastSong", id);
},
loadApi() { loadApi() {
window.onYouTubeIframeAPIReady = () => { window.onYouTubeIframeAPIReady = () => {
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
player = new window.YT.Player("player", { player = new window.YT.Player("player", {
videoId: "XDh0JcxrbPM", videoId: this.$store.getters.globalState.lastSong,
playerVars: { playerVars: {
'autoplay': 1, 'autoplay': 1,
'mute': 1, 'mute': 1,
+16 -1
View File
@@ -8,7 +8,15 @@ export default new Vuex.Store({
state: { state: {
itemsFinished: [], itemsFinished: [],
colorSelected: "", colorSelected: "",
nbSessions: 0 nbSessions: 0,
globalState: {
currentItems: localStorage.getItem("currentItems") ? localStorage.getItem("currentItems") : JSON.stringify([]),
nbItemsFinished: localStorage.getItem("nbItemsFinished") ? localStorage.getItem("nbItemsFinished") : 0,
nbSessions: localStorage.getItem("nbSessions") ? localStorage.getItem("nbSessions") : 0,
lastSong: localStorage.getItem("lastSong") ? localStorage.getItem("lastSong") : "XDh0JcxrbPM",
currentSongIndex: localStorage.getItem("currentSongIndex") ? localStorage.getItem("currentSongIndex") : 0,
themeColor: localStorage.getItem("themeColor") ? localStorage.getItem("themeColor") : "#fdde95",
}
}, },
getters: { getters: {
itemsFinished(state) { itemsFinished(state) {
@@ -19,6 +27,9 @@ export default new Vuex.Store({
}, },
nbSessions(state) { nbSessions(state) {
return state.nbSessions; return state.nbSessions;
},
globalState(state) {
return state.globalState;
} }
}, },
mutations: { mutations: {
@@ -30,6 +41,10 @@ export default new Vuex.Store({
}, },
updateNbSessionsFinished(state) { updateNbSessionsFinished(state) {
state.nbSessions++; state.nbSessions++;
},
resetStats(state) {
state.globalState.nbItemsFinished = 0;
state.globalState.nbSessions = 0;
} }
}, },
actions: { actions: {