Skip to content

Commit

Permalink
FEATURE: Delete a reference (close #3)
Browse files Browse the repository at this point in the history
  • Loading branch information
LIUC2H5OH committed Jun 6, 2023
1 parent 0d4b934 commit 3765dd2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"react-bootstrap": "^2.5.0",
"react-bootstrap-icons": "^1.10.2",
"react-dom": "^18.2.0",
"react-icons": "^4.8.0",
"react-image-crop": "^10.0.9",
"react-markdown": "^8.0.4",
"react-notifications": "^1.7.4",
Expand Down
25 changes: 25 additions & 0 deletions frontend/src/DeleteReferenceButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {FaTimes} from 'react-icons/fa';
import Button from 'react-bootstrap/Button';
import {useNavigate} from 'react-router-dom';

function DeleteReferenceButton({doc, ref_id, setLastUpdate, backend}) {
const navigate = useNavigate();
let handleDelete = async () => {
let _id = doc._id;
doc.links = doc.links.filter(link => link.object.toString() !== ref_id.toString());
Promise.all([
backend.putDocument(doc)
]).then(() => {
setLastUpdate(_id);
navigate('/' + ref_id);
});
};

return (
<Button variant="danger" size="sm" className="delete-button" onClick={handleDelete}>
<FaTimes className="delete_reference"/>
</Button>
);
}

export default DeleteReferenceButton;
12 changes: 9 additions & 3 deletions frontend/src/DocumentsCards.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import BrowseTools from './BrowseTools';
import FutureDocument from './FutureDocument';
import FutureCollection from './FutureCollection';
import { TypeBadge } from './Type';
import DeleteReferenceButton from './DeleteReferenceButton';

function DocumentsCards({docs, expandable, byRow, createOn, setLastUpdate, backend}) {
function DocumentsCards({docs, expandable, byRow, isDeletable, createOn, setLastUpdate, backend}) {
return (
<Row className="gy-4">
{docs.map(x =>
<Col key={x._id} md={ byRow && (12 / byRow) }>
<DocumentCard doc={x} expandable={expandable} />
<DocumentCard doc={x} expandable={expandable} isDeletable={isDeletable} ref_id={createOn} {...{setLastUpdate, backend}}/>
</Col>
)}
{createOn &&
Expand All @@ -35,7 +36,7 @@ function DocumentsCards({docs, expandable, byRow, createOn, setLastUpdate, backe
);
}

function DocumentCard({doc, expandable}) {
function DocumentCard({doc, expandable, isDeletable, ref_id, setLastUpdate, backend}) {
const collectionId = useMemo(() => {
if (doc?.links?.length > 1) {
return doc.links.every((item) => {
Expand All @@ -48,6 +49,11 @@ function DocumentCard({doc, expandable}) {

return (
<Card className="h-100">
{isDeletable && (
<Card.Header className="d-flex justify-content-end">
<DeleteReferenceButton doc={doc} ref_id={ref_id} setLastUpdate={setLastUpdate} backend={backend}/>
</Card.Header>
)}
<Card.Body>
<BrowseTools
id={collectionId ? doc.links.length && windowWidth.current < 820 ? doc.links[0].object : doc._id : doc._id}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ function References({scholiaMetadata, active, createOn, setLastUpdate, backend})
if (!active) return;
return (
<Col className="gloses" >
<DocumentsCards docs={scholiaMetadata} expandable={true} byRow={1}
<DocumentsCards docs={scholiaMetadata} expandable={true} byRow={1} isDeletable={true}
{...{createOn, setLastUpdate, backend}}
/>
</Col>
Expand Down

0 comments on commit 3765dd2

Please sign in to comment.