-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: code refactored for likes count * fix: image fix and minor bookmark configuration added * feat: bookmark feature partial dependency * feat: bookmark feature created
- Loading branch information
1 parent
a05f7a6
commit 9d6ba92
Showing
23 changed files
with
526 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
"use client"; | ||
|
||
import { account, db, ID, Query, palettegramDB, bookmarksCollection } from "./appwrite.config"; | ||
|
||
/** | ||
* @abstract filtering bookmarks | ||
* @param {string[]} bookmarks | ||
* @param {string} postId | ||
* @returns {string[]} | ||
*/ | ||
const removePostIdFromBookmarks = function (bookmarks: string[], postId: string): string[] { | ||
let idx = bookmarks.indexOf(postId); | ||
|
||
let beforeIdx = bookmarks.slice(0, idx); | ||
|
||
let afterIdx = bookmarks.slice(idx + 1, bookmarks.length); | ||
|
||
return [...beforeIdx, ...afterIdx]; | ||
}; | ||
|
||
const getBookmarks = async (accountId: string) => { | ||
try { | ||
const getSavedBookmarkData = await db.listDocuments(palettegramDB, bookmarksCollection, [ | ||
Query.equal("accountId", accountId), | ||
]); | ||
|
||
return getSavedBookmarkData; | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}; | ||
|
||
const saveBookmark = async (accountId: string, postId: string) => { | ||
try { | ||
console.log(accountId, postId); | ||
const getSavedBookmarkData = await getBookmarks(accountId); | ||
|
||
if (!getSavedBookmarkData) { | ||
throw new Error("Account does not exist"); | ||
} | ||
|
||
const documentId = getSavedBookmarkData.documents[0].$id; | ||
let oldBookmarkArray = getSavedBookmarkData.documents[0].bookmark; | ||
|
||
oldBookmarkArray.push(postId); | ||
|
||
const updatedData = await db.updateDocument(palettegramDB, bookmarksCollection, documentId, { | ||
bookmark: oldBookmarkArray, | ||
}); | ||
|
||
if (!updatedData) { | ||
throw new Error(); | ||
} | ||
|
||
return updatedData; | ||
} catch (error) { | ||
throw new Error(); | ||
} | ||
}; | ||
|
||
const removeBookmark = async (accountId: string, postId: string) => { | ||
try { | ||
console.log(accountId, postId); | ||
|
||
const getSavedBookmarkData = await getBookmarks(accountId); | ||
|
||
if (!getSavedBookmarkData) { | ||
throw new Error("Account does not exist"); | ||
} | ||
|
||
const documentId = getSavedBookmarkData.documents[0].$id; | ||
let oldBookmarkArray = getSavedBookmarkData.documents[0].bookmark; | ||
|
||
let newBookmarkArray: string[] = removePostIdFromBookmarks(oldBookmarkArray, postId); | ||
|
||
const updatedData = await db.updateDocument(palettegramDB, bookmarksCollection, documentId, { | ||
bookmark: newBookmarkArray, | ||
}); | ||
|
||
if (!updatedData) { | ||
throw new Error(); | ||
} | ||
|
||
return updatedData; | ||
} catch (error) { | ||
throw new Error(); | ||
} | ||
}; | ||
|
||
const createBookmarkEntry = async (accountId: string, postId: string) => { | ||
try { | ||
console.log(accountId, postId); | ||
|
||
const bookmarkDocs = await db.createDocument(palettegramDB, bookmarksCollection, ID.unique(), { | ||
accountId: accountId, | ||
bookmark: [postId], | ||
}); | ||
|
||
if (!bookmarkDocs) { | ||
throw new Error("Account does not exist"); | ||
} | ||
|
||
return bookmarkDocs; | ||
} catch (error) { | ||
throw new Error(); | ||
} | ||
}; | ||
|
||
export { saveBookmark, removeBookmark, createBookmarkEntry, getBookmarks }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
9d6ba92
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
palettegram – ./
palettegram-git-master-sanchitbajaj02.vercel.app
palettegram-sanchitbajaj02.vercel.app
palettegram.vercel.app