2022-04-22 07:13:22 -05:00
|
|
|
import { useState } from 'react';
|
|
|
|
import '../styles/Modal.css';
|
|
|
|
|
|
|
|
const AddModal = ({onExit}) => {
|
|
|
|
const [name, setName] = useState('');
|
|
|
|
const [link, setLink] = useState('');
|
|
|
|
const [tag, setTag] = useState('');
|
|
|
|
|
|
|
|
function SetName(e) {
|
|
|
|
setName(e.target.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
function SetLink(e) {
|
|
|
|
setLink(e.target.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
function SetTag(e) {
|
|
|
|
setTag(e.target.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function submitBookmark() {
|
2022-04-26 05:50:07 -05:00
|
|
|
function isValidHttpUrl(string) {
|
|
|
|
let url;
|
|
|
|
|
|
|
|
try {
|
|
|
|
url = new URL(string);
|
|
|
|
} catch (_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return url.protocol === "http:" || url.protocol === "https:";
|
|
|
|
}
|
|
|
|
|
|
|
|
if(name != '' && isValidHttpUrl(link) && tag != '') {
|
2022-04-22 07:13:22 -05:00
|
|
|
fetch("/post", {
|
|
|
|
|
|
|
|
// Adding method type
|
|
|
|
method: "POST",
|
|
|
|
|
|
|
|
// Adding body or contents to send
|
|
|
|
body: JSON.stringify({
|
|
|
|
name: name,
|
|
|
|
title: "foo",
|
|
|
|
link: link,
|
|
|
|
tag: tag
|
|
|
|
}),
|
|
|
|
|
|
|
|
// Adding headers to the request
|
|
|
|
headers: {
|
|
|
|
"Content-type": "application/json; charset=UTF-8"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
2022-04-26 05:50:07 -05:00
|
|
|
alert('Please fill all fields and make sure url is valid.\n\n(i.e. starts with http/https)');
|
2022-04-22 07:13:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function abort(e) {
|
|
|
|
if (e.target.className == "overlay" || e.target.className == "cancel-btn") {
|
|
|
|
onExit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className='overlay' onClick={abort}>
|
|
|
|
<div className='box'>
|
|
|
|
<div className='modal-content'>
|
|
|
|
<h2>Add Bookmark</h2>
|
|
|
|
<h3>Name:</h3>
|
|
|
|
<input onChange={SetName} className="modal-input" type="search" placeholder="e.g. Example Tutorial"/>
|
|
|
|
<h3>Link:</h3>
|
|
|
|
<input onChange={SetLink} className="modal-input" type="search" placeholder="e.g. https://example.com/"/>
|
|
|
|
<h3>Tag:</h3>
|
|
|
|
<input onChange={SetTag} className="modal-input" type="search" placeholder="e.g. Tutorials"/>
|
|
|
|
<button onClick={submitBookmark} className="upload-btn"><span className="material-icons-outlined md-36">upload</span></button>
|
|
|
|
<button className="cancel-btn">Cancel</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default AddModal
|