-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1582 from HyperTHD/issue-1539
[next] Fixes #1539: Porting DeleteFeedDialogButton to Next
- Loading branch information
Showing
1 changed file
with
111 additions
and
0 deletions.
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
src/frontend/next/src/components/DeleteFeedDialogButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { useState, useRef } from 'react'; | ||
import { | ||
Button, | ||
Dialog, | ||
DialogContent, | ||
DialogActions, | ||
DialogContentText, | ||
DialogTitle, | ||
IconButton, | ||
} from '@material-ui/core'; | ||
import { Delete } from '@material-ui/icons'; | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
import { Feed } from '../interfaces'; | ||
import useSiteMetadata from '../hooks/use-site-metadata'; | ||
|
||
type DeleteFeedDialogButtonProps = { | ||
feed: Feed; | ||
deletionCallback: (id: number) => void; | ||
}; | ||
|
||
const useStyles = makeStyles(() => ({ | ||
button: { | ||
padding: '3px 0 3px 0', | ||
}, | ||
})); | ||
|
||
const DeleteFeedDialogButton = ({ feed, deletionCallback }: DeleteFeedDialogButtonProps) => { | ||
const { id, url } = feed; | ||
const classes = useStyles(); | ||
const { telescopeUrl } = useSiteMetadata(); | ||
const [open, setOpen] = useState(false); | ||
|
||
const deleteBtnRef = useRef<HTMLButtonElement | null>(null); | ||
|
||
const handleClickOpen = () => { | ||
setOpen(true); | ||
}; | ||
|
||
const handleClose = () => { | ||
setOpen(false); | ||
}; | ||
|
||
const disableBtn = () => { | ||
if (deleteBtnRef.current !== null) { | ||
deleteBtnRef.current.setAttribute('disabled', 'disabled'); | ||
} | ||
}; | ||
|
||
const removeFeed = async () => { | ||
console.log(`Removing feed hosted at URL ${url}...`); | ||
try { | ||
const response = await fetch(`${telescopeUrl}/feeds/${id}`, { method: 'DELETE' }); | ||
|
||
if (!response.ok) { | ||
throw new Error(response.statusText); | ||
} | ||
|
||
deletionCallback(id); | ||
|
||
console.log(`Feed was successfully removed`); | ||
} catch (error) { | ||
console.error(`Error removing feed with ID ${id}`, error); | ||
throw error; | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<IconButton classes={{ root: classes.button }} onClick={handleClickOpen}> | ||
<Delete color="secondary" /> | ||
</IconButton> | ||
<Dialog | ||
open={open} | ||
onClose={handleClose} | ||
aria-labelledby="alert-dialog-title" | ||
aria-describedby="alert-dialog-description" | ||
> | ||
<DialogTitle id="alert-dialog-title">{`Remove feed hosted at ${url}?`}</DialogTitle> | ||
<DialogContent> | ||
<DialogContentText id="alert-dialog-description"> | ||
Telescope will no longer display blog posts from this feed. | ||
</DialogContentText> | ||
</DialogContent> | ||
<DialogActions> | ||
<Button | ||
ref={deleteBtnRef} | ||
onClick={handleClose} | ||
color="secondary" | ||
variant="outlined" | ||
autoFocus | ||
> | ||
Cancel | ||
</Button> | ||
<Button | ||
onClick={() => { | ||
disableBtn(); | ||
handleClose(); | ||
removeFeed(); | ||
}} | ||
color="primary" | ||
variant="contained" | ||
> | ||
Confirm | ||
</Button> | ||
</DialogActions> | ||
</Dialog> | ||
</div> | ||
); | ||
}; | ||
|
||
export default DeleteFeedDialogButton; |