Skip to content

Commit

Permalink
Added restriction to the like button. A user can now only like a sess…
Browse files Browse the repository at this point in the history
…ion one time
  • Loading branch information
Didiloy committed Jan 13, 2025
1 parent 8c5e088 commit 7512f73
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "GameClock",
"version": "2.2.0",
"version": "2.3.0",
"description": "Track your play time with your friends",
"main": "./out/main/index.js",
"author": "github.com/Didiloy",
Expand Down
27 changes: 27 additions & 0 deletions src/renderer/src/common/likes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const preferences_name = "liked_sessions";

export function isLiked(id) {
const liked_sessions = JSON.parse(localStorage.getItem(preferences_name));
if (liked_sessions === null) {
return false;
}
return liked_sessions.includes(id);
}

export function addSession(id) {
let liked_sessions = JSON.parse(localStorage.getItem(preferences_name));
if (liked_sessions === null) {
liked_sessions = [id];
}
liked_sessions.push(id);
localStorage.setItem(preferences_name, JSON.stringify(liked_sessions));
}

export function removeSession(id) {
let liked_sessions = JSON.parse(localStorage.getItem(preferences_name));
if (liked_sessions === null) {
return;
}
liked_sessions = liked_sessions.filter((session) => session !== id);
localStorage.setItem(preferences_name, JSON.stringify(liked_sessions));
}
49 changes: 37 additions & 12 deletions src/renderer/src/components/SessionsHistory.vue
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,11 @@
).toLocaleDateString()
}}
<Button
icon="pi pi-heart"
:icon="
isLiked(slotProps.data.id)
? 'pi pi-heart-fill'
: 'pi pi-heart'
"
severity="danger"
text
rounded
Expand All @@ -181,9 +185,14 @@ import { onMounted, ref, watch } from "vue";
import { useStore } from "../store/store";
import { storeToRefs } from "pinia";
import { convertMinuteToHoursMinute } from "../common/main";
import { getIdsOfTeam, addLikeToSession } from "../database/database";
import {
getIdsOfTeam,
addLikeToSession,
removeLikeToSession,
} from "../database/database";
import { getPreferences } from "../preferences/preferences";
import { useToast } from "primevue/usetoast";
import { isLiked, addSession, removeSession } from "../common/likes";
import { useI18n } from "vue-i18n";
const i18n = useI18n();
Expand Down Expand Up @@ -386,17 +395,33 @@ function getGameNameAndLogoById(id) {
}
async function addLike(session) {
let added_like = await addLikeToSession(session.id);
if (added_like) {
if (session.likes !== undefined) session.likes++;
else session.likes = 1;
if (!isLiked(session.id)) {
let added_like = await addLikeToSession(session.id);
if (added_like) {
if (session.likes !== undefined) session.likes++;
else session.likes = 1;
addSession(session.id);
} else {
toast.add({
severity: "error",
summary: "",
detail: i18n.t("SessionsHistory.error_like"),
life: 3000,
});
}
} else {
toast.add({
severity: "error",
summary: "",
detail: i18n.t("SessionsHistory.error_like"),
life: 3000,
});
let removed_like = removeLikeToSession(session.id);
if (removed_like) {
removeSession(session.id);
session.likes--;
} else {
toast.add({
severity: "error",
summary: "",
detail: i18n.t("SessionsHistory.error_remove_like"),
life: 3000,
});
}
}
}
</script>
Expand Down
24 changes: 24 additions & 0 deletions src/renderer/src/database/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,30 @@ export async function addLikeToSession(id) {
}
}

export async function removeLikeToSession(id) {
const docRef = doc(db, "sessions", id);
const docSnap = await getDoc(docRef);

if (docSnap.exists()) {
try {
await setDoc(
doc(db, "sessions", id),
{
likes:
docSnap.data().likes !== undefined ? docSnap.data().likes - 1 : 0,
},
{ merge: true },
);
return true;
} catch (error) {
console.log(error);
return false;
}
} else {
return false;
}
}

export async function addSession(
teamsId,
gameName,
Expand Down
6 changes: 4 additions & 2 deletions src/renderer/src/i18n/i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@
"bad": "Nul",
"date": "Date",
"deleted_game": "Jeu supprimé ou introuvable",
"error_like": "Impossible de mettre un like sur cette session"
"error_like": "Impossible de mettre un like sur cette session",
"error_remove_like": "Impossible de retirer un like sur cette session"
},
"DashboardTeam": {
"team_time": "passées à jouer. Soit <b> {percentage_total_time} </b> du temps total de jeu",
Expand Down Expand Up @@ -552,7 +553,8 @@
"bad": "Bad",
"date": "Date",
"deleted_game": "Deleted or unavailable game",
"error_like": "Impossible to add a like to this session"
"error_like": "Impossible to add a like to this session",
"error_remove_like": "Impossible to remove a like of this session"
},
"DashboardTeam": {
"team_time": "spent playing. This is <b>{percentage_total_time}</b> of the total playtime",
Expand Down

0 comments on commit 7512f73

Please sign in to comment.