Skip to content

Commit

Permalink
FIX: The user bookshelf should be refreshed after signing out.
Browse files Browse the repository at this point in the history
Regression caused by 0b45d67.
  • Loading branch information
benel committed Mar 1, 2025
1 parent 0b45d67 commit 8899207
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 59 deletions.
23 changes: 6 additions & 17 deletions frontend/src/components/Bookmark.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { useState, useEffect, useCallback } from 'react';
import { BookmarkFill } from 'react-bootstrap-icons';
import { v4 as uuid } from 'uuid';

function Bookmark({backend, id}) {
function Bookmark({backend, user, id}) {
const [isBookmarked, setIsBookmarked] = useState(false);
let user = backend.user;

const getBookmark = useCallback((id, user) =>
backend.getView({view: 'bookmark', id: user, options: ['include_docs']})
Expand All @@ -17,27 +16,17 @@ function Bookmark({backend, id}) {
.then(bookmark => setIsBookmarked(!!bookmark))
.catch(console.error);
}
}, [user, id, backend, getBookmark]);

const createBookmark = () =>
backend.putDocument({
_id: uuid(),
editors: [user],
bookmark: id
});

const removeBookmark = () =>
getBookmark(id, user)
.then(bookmark => bookmark.doc)
.then(backend.deleteDocument);
}, [user, id, getBookmark]);

const onBookmarkToggle = () => {
if (!isBookmarked) {
createBookmark(user, id)
backend.putDocument({ _id: uuid(), editors: [user], bookmark: id })
.then(() => setIsBookmarked(true))
.catch(console.error);
} else {
removeBookmark(id, user)
getBookmark(id, user)
.then(x => x.doc)
.then(backend.deleteDocument)
.then(() => setIsBookmarked(false))
.catch(console.error);
}
Expand Down
11 changes: 6 additions & 5 deletions frontend/src/components/DocumentList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ function DocumentList({ relatedTo, verb, setSelectedDocument, setShowDocumentLis
);

useEffect(() => {
backend.refreshDocuments(
x => setUserDocuments(
x.filter(y => y && y._id !== relatedTo[0])
)
);
backend.getAllDocuments(user)
.then(x =>
setUserDocuments(
x.filter(y => y && y._id !== relatedTo[0])
)
);
}, [user, backend, relatedTo]);

return (
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/components/FutureDocument.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function FutureDocument({relatedTo, setLastUpdate, backend, user}) {
)}
<FutureDocumentIcon
relatedTo={selectedDocument ? [selectedDocument._id] : relatedTo}
{...{verb, setLastUpdate, backend}}
{...{verb, setLastUpdate, backend, user}}
/>
{!fixedType && (
<Link
Expand All @@ -50,14 +50,14 @@ function FutureDocument({relatedTo, setLastUpdate, backend, user}) {
);
}

function FutureDocumentIcon({relatedTo, verb, setLastUpdate, backend}) {
function FutureDocumentIcon({relatedTo, verb, setLastUpdate, backend, user}) {
const navigate = useNavigate();

let handleClick = async () => {
let _id = uuid().replace(/-/g, '');
let doc = {
_id,
editors: [backend.user],
editors: [user],
dc_creator: '<CREATOR>',
dc_title: '<TITLE>',
dc_issued: new Date(),
Expand Down
6 changes: 1 addition & 5 deletions frontend/src/components/Menu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ function Authentication({backend, user, setUser}) {
e.preventDefault();
let credentials = Object.fromEntries(new FormData(e.target).entries());
backend.postSession(credentials)
.then((x) => {
if (x) {
setUser(x);
}
});
.then(setUser);
};

if (user) return (
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/components/OpenedDocuments.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import DeleteDocumentAction from './DeleteDocumentAction';
import Bookmark from './Bookmark';
import LicenseCompatibility from './LicenseCompatibility';

function OpenedDocuments({id, margin, metadata, parallelDocuments, hasSources, backend, setLastUpdate}) {
function OpenedDocuments({id, margin, metadata, parallelDocuments, hasSources, backend, user, setLastUpdate}) {
const marginMetadata = metadata.getDocument(margin);
const marginLicense = marginMetadata?.dc_license;
const sourceMetadata = metadata.focusedDocument;
return (
<Col className="lectern">
<Row className ="runningHead">
<RunningHeadSource {...{id, metadata, hasSources, parallelDocuments, backend}} />
<RunningHeadSource {...{id, metadata, hasSources, parallelDocuments, backend, user}} />
<RunningHeadMargin {...{parallelDocuments, margin, backend, setLastUpdate}}
metadata={marginMetadata}
/>
Expand Down Expand Up @@ -50,14 +50,14 @@ function OpenedDocuments({id, margin, metadata, parallelDocuments, hasSources, b
);
}

function RunningHeadSource({id, metadata, hasSources, parallelDocuments, backend}) {
function RunningHeadSource({id, metadata, hasSources, parallelDocuments, backend, user}) {
metadata = metadata.focusedDocument;
if (parallelDocuments.isFromScratch) return (
<Col className="main" />
);
return (
<Col className="main">
<Bookmark {...{backend, id}} />
<Bookmark {...{backend, user, id}} />
<BrowseTools {...{id}} editable={!hasSources} focusable={false} />
<Metadata {...{metadata}} />
<TypeBadge type={metadata?.type} />
Expand Down
30 changes: 7 additions & 23 deletions frontend/src/hyperglosae.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ function Hyperglosae(logger) {
this.getSession = () =>
fetch(`${service}/_session`)
.then(x => x.json())
.then(x => {
this.user = x.userCtx?.name;
return this.user;
});
.then(x => x.userCtx?.name);

this.postSession = ({name, password}) =>
fetch(`${service}/_session`, {
Expand All @@ -87,19 +84,12 @@ function Hyperglosae(logger) {
body: `name=${name}&password=${password}`
})
.then(x => x.json())
.then(x => {
if (!x.reason) return this.user = name;
logger(x.reason);
});
.then(x => x.reason ? logger(x.reason) : name);

this.deleteSession = () => {
this.deleteSession = () =>
fetch(`${service}/_session`, {
method: 'DELETE'
})
.then(() => {
this.user = null;
});
};
});

this.refreshMetadata = (id, callback) => {
this.getView({view: 'metadata', id, options: ['include_docs']})
Expand All @@ -123,15 +113,9 @@ function Hyperglosae(logger) {
);
};

this.refreshDocuments = (callback) => {
let id = this.user || 'PUBLIC';
this.getView({view: 'all_documents', id, options: ['include_docs']})
.then((rows) => {
callback(
rows.map(x => x.doc)
);
});
};
this.getAllDocuments = (user) =>
this.getView({view: 'all_documents', id: user || 'PUBLIC', options: ['include_docs']})
.then((rows) => rows.map(x => x.doc));

return this;
}
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/routes/Bookshelf.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ function Bookshelf({backend, user}) {
const [lastUpdate, setLastUpdate] = useState();

useEffect(() => {
backend.refreshDocuments(setDocuments);
backend.getAllDocuments(user)
.then(setDocuments);
}, [lastUpdate, user, backend]);

return (
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/routes/Lectern.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function Lectern({backend, user}) {
</Col>
<OpenedDocuments
hasSources={metadata.forwardLinkedDocuments.length > 0}
{...{id, margin, metadata, parallelDocuments, backend, setLastUpdate}}
{...{id, margin, metadata, parallelDocuments, user, backend, setLastUpdate}}
/>
<References active={!margin} createOn={[id]}
{...{metadata, user, setLastUpdate, backend}}
Expand Down

0 comments on commit 8899207

Please sign in to comment.