Added timestamp to each bookmark + Bug fix.

This commit is contained in:
Daniel
2022-06-12 07:18:12 +04:30
parent 870c67027c
commit 25a64dcccc
5 changed files with 82 additions and 49 deletions
+40 -37
View File
@@ -1,45 +1,48 @@
import config from '../config';
import { nanoid } from 'nanoid';
const addItem = async (name, link, tag, reFetch, onExit, SetLoader, method, id=nanoid(), title='') => {
function isValidHttpUrl(string) {
let url;
try {
url = new URL(string);
} catch (_) {
return false;
}
const addItem = async (name, link, tag, reFetch, onExit, SetLoader, method, id=nanoid(), title='', date=new Date()) => {
const dateCreated = date.toString();
function isValidHttpUrl(string) {
let url;
return url.protocol === "http:" || url.protocol === "https:";
}
if(isValidHttpUrl(link)) {
const ADDRESS = config.API.ADDRESS + ":" + config.API.PORT;
fetch(ADDRESS + "/api", {
method: method,
body: JSON.stringify({
_id: id,
name: name,
title: title,
link: link,
tag: tag
}),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
.then(res => res.text())
.then(() => reFetch())
.then(() => {SetLoader(false)});
onExit();
} else if(!isValidHttpUrl(link) && link !== '') {
SetLoader(false)
alert('Please make sure the link is valid.\n\n(i.e. starts with "http"/"https")');
} else {
SetLoader(false)
try {
url = new URL(string);
} catch (_) {
return false;
}
return url.protocol === "http:" || url.protocol === "https:";
}
if(isValidHttpUrl(link)) {
const ADDRESS = config.API.ADDRESS + ":" + config.API.PORT;
fetch(ADDRESS + "/api", {
method: method,
body: JSON.stringify({
_id: id,
name: name,
title: title,
link: link,
tag: tag,
date: dateCreated
}),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
.then(res => res.text())
.then(() => reFetch())
.then(() => {SetLoader(false)});
onExit();
} else if(!isValidHttpUrl(link) && link !== '') {
SetLoader(false)
alert('Please make sure the link is valid.\n\n(i.e. starts with "http"/"https")');
} else {
SetLoader(false)
}
}
export default addItem;