clear last tickets
This commit is contained in:
+46
-3
@@ -1,4 +1,10 @@
|
||||
import { arrayRemove, arrayUnion, projectsRef } from "../config/firebase";
|
||||
import {
|
||||
arrayRemove,
|
||||
arrayUnion,
|
||||
deleteField,
|
||||
projectsRef,
|
||||
serverTimestamp,
|
||||
} from "../config/firebase";
|
||||
import { ensureAuthenticated } from "./authRedirect";
|
||||
|
||||
export const LIKE_TARGET = {
|
||||
@@ -10,6 +16,16 @@ export const getLikeFieldPath = (target = LIKE_TARGET.SONG) => {
|
||||
return target === LIKE_TARGET.PLAYBACK ? "likes.playback" : "likes.song";
|
||||
};
|
||||
|
||||
const getLikeTimestampPath = (target = LIKE_TARGET.SONG) => {
|
||||
if (target === LIKE_TARGET.PLAYBACK) {
|
||||
return "likes.playbackLikedAt";
|
||||
}
|
||||
if (target === LIKE_TARGET.SONG) {
|
||||
return "likes.songLikedAt";
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
export const getProjectLikes = (project, target = LIKE_TARGET.SONG) => {
|
||||
const path = target === LIKE_TARGET.PLAYBACK ? "playback" : "song";
|
||||
const likes = project?.likes;
|
||||
@@ -46,17 +62,44 @@ export const toggleProjectLike = async ({
|
||||
}) => {
|
||||
if (!projectId) return;
|
||||
if (!ensureAuthenticated(currentUID)) return;
|
||||
const fieldPath = getLikeFieldPath(target);
|
||||
const likeFieldKey =
|
||||
target === LIKE_TARGET.PLAYBACK ? "playback" : "song";
|
||||
const likeOperation = next ? arrayUnion(currentUID) : arrayRemove(currentUID);
|
||||
const likedAtKey =
|
||||
target === LIKE_TARGET.PLAYBACK
|
||||
? "playbackLikedAt"
|
||||
: target === LIKE_TARGET.SONG
|
||||
? "songLikedAt"
|
||||
: null;
|
||||
const likesPayload = {
|
||||
likes: {
|
||||
[likeFieldKey]: likeOperation,
|
||||
...(likedAtKey && currentUID
|
||||
? {
|
||||
[likedAtKey]: {
|
||||
[currentUID]: next ? serverTimestamp() : deleteField(),
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
};
|
||||
const cleanupPayload =
|
||||
target === LIKE_TARGET.PLAYBACK
|
||||
? {
|
||||
// Remove any malformed top-level "likes.playback" key if it exists
|
||||
["likes.playback"]: deleteField(),
|
||||
}
|
||||
: {};
|
||||
await projectsRef.doc(projectId).set(
|
||||
{
|
||||
[fieldPath]: likeOperation,
|
||||
...(target === LIKE_TARGET.SONG
|
||||
? {
|
||||
// Keep legacy likedBy in sync for clients still reading this field
|
||||
likedBy: next ? arrayUnion(currentUID) : arrayRemove(currentUID),
|
||||
}
|
||||
: {}),
|
||||
...likesPayload,
|
||||
...cleanupPayload,
|
||||
},
|
||||
{ merge: true }
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user