Skip to content

Commit

Permalink
FEATURE: Add a document created by other to one's bookshelf (closes #150
Browse files Browse the repository at this point in the history
).
  • Loading branch information
Nizreenana authored and benel committed Jul 16, 2024
1 parent aeadb15 commit 8a73581
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 4 deletions.
5 changes: 5 additions & 0 deletions backend/src/views/all_documents/map.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
function (metadata) {

if (metadata.bookmark) {
emit([metadata.editors[0], metadata.bookmark], {_id: metadata.bookmark});
return;
}

if (!metadata.dc_title) return;

const editors = metadata.editors || ['PUBLIC'];
Expand Down
5 changes: 5 additions & 0 deletions backend/src/views/bookmark/map.js
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]);
}
}
52 changes: 52 additions & 0 deletions frontend/src/components/Bookmark.js
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;
9 changes: 5 additions & 4 deletions frontend/src/components/OpenedDocuments.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import Row from 'react-bootstrap/Row';
import Col from 'react-bootstrap/Col';
import { BookmarkFill } from 'react-bootstrap-icons';
import BrowseTools from './BrowseTools';
import Metadata from './Metadata';
import Type, { TypeBadge } from './Type';
import Passage from './Passage';
import License from './License';
import More from './More';
import Bookmark from './Bookmark';

function OpenedDocuments({backend, lectern, metadata, sourceMetadata, margin, hasSources, id, setLastUpdate}) {
return (
<Col className="lectern">
<Row className ="runningHead">
<RunningHeadSource metadata={ sourceMetadata } hasSources={hasSources} />
<RunningHeadSource metadata={ sourceMetadata } {...{hasSources, backend}} />
<RunningHeadMargin {...{backend}}
metadata={ metadata.find(x => (x._id === margin)) }
/>
Expand All @@ -36,10 +36,11 @@ function OpenedDocuments({backend, lectern, metadata, sourceMetadata, margin, ha
);
}

function RunningHeadSource({metadata, hasSources}) {
function RunningHeadSource({metadata, hasSources, backend}) {
let id = metadata?._id;
return (
<Col className="main">
<BookmarkFill className="icon" />
<Bookmark {...{backend, id}} />
<BrowseTools id={metadata?._id} editable={!hasSources} focusable={false} />
<Metadata {...{metadata}} />
<TypeBadge type={metadata?.type} />
Expand Down
14 changes: 14 additions & 0 deletions frontend/src/hyperglosae.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ function Hyperglosae(logger) {
return x;
});

this.deleteDocument = ({_id, _rev}) =>
fetch(`${service}/${_id}?rev=${_rev}`, {
method: 'DELETE',
headers: basicAuthentication({ force: false })
})
.then(x => x.json())
.then(x => {
if (x.reason) {
logger(x.reason);
throw new Error(x.reason);
}
return x;
});

this.getDocumentMetadata = (id) =>
fetch(`${service}/${id}`, {
method: 'HEAD',
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/styles/Lectern.css
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,7 @@ dd {
padding-left: 30px;
}

.icon.bookmarked {
color: gold; /* or any color that indicates an active bookmark */
}

0 comments on commit 8a73581

Please sign in to comment.