I’m trying to store all the likedUserIds
s and dislikedUserIds
s in local storage but for some reason, it’s not even hitting the if()
statement. The likedPhotoUserId
function parameter indeed has a value, so why’s it not hitting the first if()
?
Any feedback would be appreciated!
const handleLikesBasedOnUserId = (likedPhotoUserId) => {
console.log(likedPhotoUserId); // value is present
// dislike
if(likedPhotoUserId) {
// if the user id exists
if(localStorage.getItem('dislikedUserIds')) {
console.log("inside if");
// split the existing values into an array
let vals = localStorage.getItem('dislikedUserIds').split(',');
// if the value has not already been added
if (!vals.includes(likedPhotoUserId)) {
// add the value to the array
vals.push(likedPhotoUserId);
// sort the array
vals.sort();
// join the values into a delimeted string and store it
localStorage.setItem('dislikedUserIds', vals.join(','));
} else {
console.log("inside else");
// the key doesn't exist yet, add it and the new value
localStorage.setItem('dislikedUserIds', likedPhotoUserId);
}
}
} else {
// like
if(likedPhotoUserId) {
// if the user id exists
if(localStorage.getItem('likedUserIds')) {
console.log("inside if");
// split the existing values into an array
let vals = localStorage.getItem('likedUserIds').split(',');
// if the value has not already been added
if (!vals.includes(likedPhotoUserId)) {
// add the value to the array
vals.push(likedPhotoUserId);
// sort the array
vals.sort();
// join the values into a delimeted string and store it
localStorage.setItem('likedUserIds', vals.join(','));
} else {
console.log("inside else");
// the key doesn't exist yet, add it and the new value
localStorage.setItem('likedUserIds', likedPhotoUserId);
}
}
}
}
};