gatsby-pingbot/src/pages/add.js

88 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"
2021-08-07 11:32:58 +00:00
import { NotificationManager, NotificationContainer } from "react-notifications"
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 NotificationManager.error("Empty")
2021-08-04 19:17:51 +00:00
}
try {
2021-08-09 14:21:08 +00:00
const res = await axios.post("/url", {
2021-08-04 19:17:51 +00:00
url: url,
})
const data = res.data
2021-08-09 14:21:08 +00:00
NotificationManager.success(data.url, "Added")
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()
}
NotificationManager.error(e)
}
}
return (
<>
<Seo
title="Add URL"
/>
<NotificationContainer />
<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="/">
2021-08-04 19:17:51 +00:00
<button className="blue-buton">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