Skip to content

Commit

Permalink
FEATURE: Add new reference (closes #2).
Browse files Browse the repository at this point in the history
Co-authored-by: khbgbgk1 <thibault.bisagni@utt.fr>
  • Loading branch information
koeltv and khbgbgk1 committed Apr 30, 2024
1 parent d924dbd commit 26cea41
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 22 deletions.
7 changes: 4 additions & 3 deletions frontend/src/components/DocumentsCards.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import BrowseTools from './BrowseTools';
import FutureDocument from './FutureDocument';
import { TypeBadge } from './Type';

function DocumentsCards({docs, expandable, byRow, createOn, setLastUpdate, backend}) {
// asSource is a flag that indicates whether to create a parent (left) or a glose (right)
function DocumentsCards({docs, expandable, byRow, createOn, setLastUpdate, backend, asSource = false}) {
return (
<Row className="gy-4">
{docs.map(x =>
Expand All @@ -19,9 +20,9 @@ function DocumentsCards({docs, expandable, byRow, createOn, setLastUpdate, backe
<Col>
<Row>
<Col>
<FutureDocument relatedTo={createOn} {...{setLastUpdate, backend}} />
<FutureDocument relatedTo={createOn} {...{setLastUpdate, backend, asSource}} />
</Col>
{(createOn.length > 0) &&
{(!asSource && createOn.length > 0) &&
<Col>
<FutureDocument relatedTo={createOn} verb="includes" {...{setLastUpdate, backend}} />
</Col>
Expand Down
67 changes: 49 additions & 18 deletions frontend/src/components/FutureDocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,67 @@ import Card from 'react-bootstrap/Card';
import { PlusLg, FolderPlus } from 'react-bootstrap-icons';
import { v4 as uuid } from 'uuid';

function FutureDocument({relatedTo, verb = 'refersTo', setLastUpdate, backend}) {
function FutureDocument({relatedTo, verb = 'refersTo', setLastUpdate, backend, asSource = false}) {
return (
<Card>
<Card.Body className="text-center">
<FutureDocumentIcon {...{relatedTo, verb, setLastUpdate, backend}} />
<FutureDocumentIcon {...{relatedTo, verb, setLastUpdate, backend, asSource}} />
</Card.Body>
</Card>
);
}

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

let handleClick = async () => {
let _id = uuid();
let editors = [backend.credentials.name];
let links = relatedTo.map(object => ({verb, object}));
backend.putDocument({
_id,
editors,
dc_creator: '<CREATOR>',
dc_title: '<TITLE>',
dc_issued: new Date(),
dc_license: '',
text: '<TEXT>',
links
}).then((x) => {
setLastUpdate(_id);
navigate((relatedTo.length ? '#' : '/') + _id);
}).catch(console.error);

if (asSource) {
let documentId = relatedTo[0];
backend.putDocument({
_id,
editors,
dc_creator: '<CREATOR>',
dc_title: '<TITLE>',
dc_issued: new Date(),
dc_license: '',
text: '<TEXT>',
}).then(() => {
setLastUpdate(_id);
backend.getDocument(documentId)
.then((x) => {
let updatedDocument = {
...Object.fromEntries(
Object.entries(x)
),
links: [{verb: 'refersTo', object: _id}]
};

backend.putDocument(updatedDocument)
.then((x) => {
setLastUpdate(documentId);
navigate('/' + _id);
});
});
});
} else {
let links = relatedTo.map(object => ({verb, object}));
backend.putDocument({
_id,
editors,
dc_creator: '<CREATOR>',
dc_title: '<TITLE>',
dc_issued: new Date(),
dc_license: '',
text: '<TEXT>',
links
}).then((x) => {
setLastUpdate(_id);
navigate((relatedTo.length ? '#' : '/') + _id);
});
}
};

switch (verb) {
Expand All @@ -46,7 +77,7 @@ function FutureDocumentIcon({relatedTo, verb, setLastUpdate, backend}) {
);
default:
return (
<PlusLg title={`Create a document ${relatedTo.length ? 'as a glose' : 'from scratch'}`}
<PlusLg title={`Create a document ${asSource ? 'as a source' : relatedTo.length ? 'as a glose' : 'from scratch'}`}
className="icon create-document" onClick={handleClick}
/>
);
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/routes/Lectern.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function Lectern({backend}) {
<Container className="screen">
<Row>
<Col md={2} className="sources">
<DocumentsCards docs={sourcesOfSourceMetadata} byRow={1} />
<DocumentsCards docs={sourcesOfSourceMetadata} createOn={[id]} asSource={true} byRow={1} {...{setLastUpdate, backend}} />
</Col>
<OpenedDocuments {...{backend, lectern, metadata, sourceMetadata, margin, setLastUpdate}} />
<References scholiaMetadata={scholiaMetadata} active={!margin}
Expand Down

0 comments on commit 26cea41

Please sign in to comment.