gatsby-pingbot/src/pages/add.js

95 lines
1.7 KiB
JavaScript
Raw Normal View History

2021-08-04 19:17:51 +00:00
import React from "react"
2021-08-06 18:17:07 +00:00
import AniLink from "gatsby-plugin-transition-link/AniLink"
import { ToastContainer, toast } from "react-toastify"
2021-08-04 19:17:51 +00:00
import Seo from "../components/seo"
import axios from "../axios"
2021-08-07 10:33:58 +00:00
import "../css/url.sass"
2021-08-07 11:32:58 +00:00
import "../css/notifications.sass"
2021-08-04 19:17:51 +00:00
const AddURLPage = () => {
2021-08-09 14:21:08 +00:00
const [url, setURL] = React.useState("")
2021-08-04 19:17:51 +00:00
2021-08-07 11:32:58 +00:00
const handleKeypress = e => {
if (e.charCode === 13 || e.keyCode === 13) {
addURL()
}
}
2021-08-04 19:17:51 +00:00
const changeURLHandler = event => {
const value = event.target.value
setURL(value)
}
const addURL = async () => {
2021-08-09 14:21:08 +00:00
setURL("")
2021-08-04 19:17:51 +00:00
2021-08-09 14:21:08 +00:00
if (url === "") {
return toast("Empty", {
type: "error"
})
2021-08-04 19:17:51 +00:00
}
try {
const res = await axios.post("", {
2021-08-04 19:17:51 +00:00
url: url,
cluster: 1
2021-08-04 19:17:51 +00:00
})
const data = res.data
toast("Added " + data.url, {
type: "success"
})
2021-08-04 19:17:51 +00:00
} catch (err) {
2021-08-09 14:21:08 +00:00
let e = ""
2021-08-04 19:17:51 +00:00
if (err.response && err.response.data.message) {
e = err.response.data.message
} else {
e = err.toString()
}
toast(e, {
type: "error"
})
2021-08-04 19:17:51 +00:00
}
}
return (
<>
<Seo
title="Add URL"
/>
<ToastContainer />
2021-08-04 19:17:51 +00:00
<div className="field">
<input
type="url"
name="url"
inputMode="url"
className="input"
placeholder="URL"
value={url}
2021-08-07 11:32:58 +00:00
onChange={changeURLHandler}
onKeyPress={handleKeypress} />
2021-08-04 19:17:51 +00:00
</div>
<button
className="yellow-button"
onClick={() => addURL()}
>Add URL</button>
<br />
2021-08-06 18:17:07 +00:00
<AniLink cover to="/">
<button className="blue-button">Go to home page</button>
2021-08-06 18:17:07 +00:00
</AniLink>
2021-08-04 19:17:51 +00:00
</>
)
}
export default AddURLPage