added delete function

This commit is contained in:
Daniel 2022-04-26 10:50:42 +04:30
parent c757b2bfa6
commit e49ade7462
6 changed files with 99 additions and 31 deletions

View File

@ -15,7 +15,7 @@ function App() {
function search(e) {
setSearchQuery(e.target.value);
}
const filteredData = data.filter((e) => {
return (e.name.toLowerCase().includes(searchQuery.toLowerCase()) || e.title.toLowerCase().includes(searchQuery.toLowerCase()) || e.tag.toLowerCase().includes(searchQuery.toLowerCase()))
});
@ -29,12 +29,12 @@ function App() {
}
fetchData();
}, []);
}, [data]);
return (
<div className="App">
<div className="head">
<input className="search" type="search" placeholder="Search for name/title/tag" onChange={search}/>
<input className="search" type="search" placeholder="Search for Name/Title/Tag" onChange={search}/>
<button className="add-btn"><span className="material-icons-outlined md-36" onClick={() => setIsAdding(true)}>add</span></button>
<button className="settings-btn"><span className="material-icons-outlined md-36">settings</span></button>
</div>

View File

@ -0,0 +1,9 @@
import React from 'react';
const EditModal = ({id}) => {
return (
<div>{id}</div>
)
}
export default EditModal

View File

@ -1,6 +1,27 @@
import '../styles/List.css';
import { useState } from 'react';
const List = ({data}) => {
const [reload, setReload] = useState(0);
async function deleteEntity(id) {
fetch("/delete", {
// Adding method type
method: "DELETE",
// Adding body or contents to send
body: JSON.stringify({id}),
// Adding headers to the request
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
.then(res => res.text())
.then(message => {console.log(message)})
}
return (
<table className="table">
<thead>
@ -22,6 +43,7 @@ const List = ({data}) => {
<td>{e.title}</td>
<td><a href={e.link}>{url.hostname}</a></td>
<td>{e.tag}</td>
<td className="delete" onClick={() => deleteEntity(e._id)}><div>X</div></td>
</tr>
} catch (e) {
console.log(e)

View File

@ -1,29 +1,40 @@
.table {
width: 100%;
text-align: left;
padding-top: 20px;
}
.table td {
font-size: 1.3rem;
padding: 5px;
}
.table th {
font-size: 1.6rem;
padding: 5px;
}
.table tbody tr:nth-of-type(2n-1) {
background-color:#273949;
}
width: 100%;
text-align: left;
padding-top: 20px;
}
.table a {
text-decoration: none;
color: rgb(194, 193, 193);
font-size: 1rem;
}
.table td {
font-size: 1.3rem;
padding: 5px;
}
.table a:hover {
text-decoration: underline;
}
.table th {
font-size: 1.6rem;
padding: 5px;
}
.table tbody tr:nth-of-type(2n-1) {
background-color:#273949;
}
.table tbody td {
border-radius: 10px;
}
.table a {
text-decoration: none;
color: rgb(194, 193, 193);
font-size: 1rem;
}
.table a:hover {
text-decoration: underline;
}
.delete {
background-color: red;
color: white;
cursor: pointer;
}

View File

@ -1,5 +1,5 @@
.overlay {
position: absolute;
position: fixed;
top: 0;
left: 0;
background-color: rgba(49, 64, 73, 0.781);

View File

@ -1,6 +1,6 @@
const express = require('express');
const app = express();
const { MongoClient } = require('mongodb');
const { MongoClient, ObjectId } = require('mongodb');
const phantom = require('phantom');
const port = process.env.PORT || 3001;
@ -23,6 +23,14 @@ app.post('/post', async (req, res) => {
insertDoc(req.body);
});
app.delete('/delete', async (req, res) => {
const id = req.body.id.toString();
await deleteDoc(id);
res.send(`Bookmark with ObjectId "${id}" deleted.`);
});
async function insertDoc(doc) {
try {
await client.connect();
@ -61,6 +69,24 @@ async function getDoc() {
}
}
async function deleteDoc(doc) {
try {
await client.connect();
const database = client.db("sample_db");
const list = database.collection("list");
const result = await list.deleteOne({"_id": ObjectId(doc)});
}
catch(err) {
console.log(err);
}
finally {
await client.close();
}
}
async function getTitle(url) {
const instance = await phantom.create();
const page = await instance.createPage();