import React, { Component } from "react"; import { styles } from "./PageLayout.styles"; import { Button, CircularProgress, IconButton, List, ListItem, ListItemAvatar, ListItemSecondaryAction, ListItemText, ListSubheader, Paper, Typography, Tooltip, withStyles, Dialog, DialogTitle, DialogContent, DialogContentText, DialogActions, TextField } from "@material-ui/core"; import { withSnackbar } from "notistack"; import DomainIcon from "@material-ui/icons/Domain"; import CloseIcon from "@material-ui/icons/Close"; import Mastodon from "megalodon"; interface IBlockedState { viewIsLoading: boolean; viewDidLoad: boolean; viewDidError: boolean; addBlockOpen: boolean; blockedServers?: [string]; blockTextField: string; } class Blocked extends Component { client: any; constructor(props: any) { super(props); this.client = new Mastodon( localStorage.getItem("access_token") as string, localStorage.getItem("baseurl") + "/api/v1" ); this.state = { addBlockOpen: false, viewIsLoading: true, viewDidLoad: false, viewDidError: false, blockTextField: "" }; } componentDidMount() { this.client .get("/domain_blocks") .then((resp: any) => { this.setState({ blockedServers: resp.data, viewDidLoad: true, viewIsLoading: false }); }) .catch((err: Error) => { console.error(err); this.setState({ viewIsLoading: false, viewDidError: true }); }); } addBlock(domain: string) { this.client.post("/domain_blocks", { domain }).then((resp: any) => { this.props.enqueueSnackbar(`Blocked ${domain} successfully.`); let blockedServers = this.state.blockedServers; if (blockedServers && blockedServers.length > 0) { blockedServers.push(domain); } else { blockedServers = [domain]; } this.setState({ blockTextField: "", addBlockOpen: false, blockedServers }); }); } removeBlock(domain: string) { this.client .del("/domain_blocks", { domain }) .then((resp: any) => { this.props.enqueueSnackbar(`Removed ${domain} from blacklist.`); let blockedServers = this.state.blockedServers; if (blockedServers && blockedServers.length > 0) { blockedServers.splice(blockedServers.indexOf(domain), 1); } this.setState({ blockedServers }); }) .catch((err: Error) => { this.props.enqueueSnackbar( `Couldn't remove ${domain}: ${err.name}`, { variant: "error" } ); }); } updateTextField(value: string) { this.setState({ blockTextField: value }); } showAddBlockDialog() { return ( this.toggleAddBlockState()} > Add a domain Type the domain that you want to block. You won't see any posts from this server or receive notifications from them. this.updateTextField(e.target.value)} > ); } toggleAddBlockState() { this.setState({ addBlockOpen: !this.state.addBlockOpen }); } render() { const { classes } = this.props; return (
Blocked servers {this.state.viewIsLoading ? (
) : ( )} {this.state.blockedServers && this.state.blockedServers.length > 0 ? ( {this.state.blockedServers && this.state.blockedServers.length > 0 ? this.state.blockedServers.map( (domain: string) => ( this.removeBlock( domain ) } > ) ) : null} ) : this.state.viewDidError ? ( Bummer. Something went wrong when loading blocked servers. ) : ( No blocked servers found. )}
You won't see any public posts and notifications from the following servers, and any followers from these servers are automatically removed. {this.showAddBlockDialog()}
); } } export default withStyles(styles)(withSnackbar(Blocked));