gatsby-pingbot/src/pages/delete.js

92 lines
1.7 KiB
JavaScript
Raw Normal View History

2021-08-19 21:13:03 +00:00
import React from 'react'
import AniLink from 'gatsby-plugin-transition-link/AniLink'
import { ToastContainer, toast } from 'react-toastify'
2021-08-04 19:17:51 +00:00
2021-08-19 21:13:03 +00:00
import Seo from '../components/seo'
import axios from '../axios'
2021-08-04 19:17:51 +00:00
2021-08-19 21:13:03 +00:00
import '../css/url.sass'
import '../css/notifications.sass'
2021-08-04 19:17:51 +00:00
const DeleteURLPage = () => {
2021-08-19 21:13:03 +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) {
deleteURL()
}
}
2021-08-04 19:17:51 +00:00
const changeURLHandler = event => {
const value = event.target.value
setURL(value)
}
const deleteURL = async () => {
2021-08-19 21:13:03 +00:00
setURL('')
2021-08-04 19:17:51 +00:00
2021-08-19 21:13:03 +00:00
if (url === '') {
return toast('Empty', {
type: 'error'
})
2021-08-04 19:17:51 +00:00
}
try {
const res = await axios.delete(`?url=${url}`)
2021-08-04 19:17:51 +00:00
const data = res.data
2021-08-19 21:13:03 +00:00
toast('Deleted ' + data.url, {
type: 'success'
})
2021-08-04 19:17:51 +00:00
} catch (err) {
2021-08-19 21:13:03 +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, {
2021-08-19 21:13:03 +00:00
type: 'error'
})
2021-08-04 19:17:51 +00:00
}
}
return (
<>
<Seo
title="Delete 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="red-button"
onClick={() => deleteURL()}
>Delete 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 DeleteURLPage