diff --git a/apiserver/plane/app/views/workspace/favorite.py b/apiserver/plane/app/views/workspace/favorite.py index 055f4d6787..ad2f24883e 100644 --- a/apiserver/plane/app/views/workspace/favorite.py +++ b/apiserver/plane/app/views/workspace/favorite.py @@ -34,6 +34,22 @@ class WorkspaceFavoriteEndpoint(BaseAPIView): def post(self, request, slug): try: workspace = Workspace.objects.get(slug=slug) + + # If the favorite exists return + if request.data.get("entity_identifier"): + user_favorites = UserFavorite.objects.filter( + workspace=workspace, + user_id=request.user.id, + entity_type=request.data.get("entity_type"), + entity_identifier=request.data.get("entity_identifier"), + ).first() + + # If the favorite exists return + if user_favorites: + serializer = UserFavoriteSerializer(user_favorites) + return Response(serializer.data, status=status.HTTP_200_OK) + + # else create a new favorite serializer = UserFavoriteSerializer(data=request.data) if serializer.is_valid(): serializer.save( diff --git a/web/core/store/favorite.store.ts b/web/core/store/favorite.store.ts index a588f2af3b..d210b723e7 100644 --- a/web/core/store/favorite.store.ts +++ b/web/core/store/favorite.store.ts @@ -111,11 +111,12 @@ export class FavoriteStore implements IFavoriteStore { * @returns Promise */ addFavorite = async (workspaceSlug: string, data: Partial) => { - const id = uuidv4(); data = { ...data, parent: null, is_folder: data.entity_type === "folder" }; + if (data.entity_identifier && this.entityMap[data.entity_identifier]) return this.entityMap[data.entity_identifier]; try { // optimistic addition + const id = uuidv4(); runInAction(() => { set(this.favoriteMap, [id], data); data.entity_identifier && set(this.entityMap, [data.entity_identifier], data); @@ -271,6 +272,7 @@ export class FavoriteStore implements IFavoriteStore { * @returns Promise */ deleteFavorite = async (workspaceSlug: string, favoriteId: string) => { + if (!this.favoriteMap[favoriteId]) return; const parent = this.favoriteMap[favoriteId].parent; const children = this.groupedFavorites[favoriteId].children; const entity_identifier = this.favoriteMap[favoriteId].entity_identifier;