-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: Add a document created by other to one's bookshelf (closes #150
- Loading branch information
1 parent
aeadb15
commit 8a73581
Showing
6 changed files
with
85 additions
and
4 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
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,5 @@ | ||
function (doc) { | ||
if (doc.bookmark) { | ||
emit([doc.editors[0], doc.bookmark]); | ||
} | ||
} |
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,52 @@ | ||
import { useState, useEffect } from 'react'; | ||
import { BookmarkFill } from 'react-bootstrap-icons'; | ||
import { v4 as uuid } from 'uuid'; | ||
|
||
function Bookmark({backend, id}) { | ||
const [isBookmarked, setIsBookmarked] = useState(false); | ||
let user = backend.credentials.name; | ||
|
||
const getBookmark = (id, user) => | ||
backend.getView({view: 'bookmark', id: user, options: ['include_docs']}) | ||
.then(rows => rows.find(row => row.doc.bookmark === id)); | ||
|
||
useEffect(() => { | ||
if (user) { | ||
getBookmark(id, user) | ||
.then(bookmark => setIsBookmarked(!!bookmark)) | ||
.catch(console.error); | ||
} | ||
}, [user, id, backend]); | ||
|
||
const createBookmark = () => | ||
backend.putDocument({ | ||
_id: uuid(), | ||
editors: [user], | ||
bookmark: id | ||
}); | ||
|
||
const removeBookmark = () => | ||
getBookmark(id, user) | ||
.then(bookmark => bookmark.doc) | ||
.then(backend.deleteDocument); | ||
|
||
const onBookmarkToggle = () => { | ||
if (!isBookmarked) { | ||
createBookmark(user, id) | ||
.then(() => setIsBookmarked(true)) | ||
.catch(console.error); | ||
} else { | ||
removeBookmark(id, user) | ||
.then(() => setIsBookmarked(false)) | ||
.catch(console.error); | ||
} | ||
}; | ||
|
||
return ( | ||
<BookmarkFill className={`icon ${isBookmarked && 'bookmarked'}`} | ||
onClick={onBookmarkToggle} | ||
/> | ||
); | ||
} | ||
|
||
export default Bookmark; |
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