import ListItem from '@material-ui/core/ListItem' import ListItemIcon from '@material-ui/core/ListItemIcon' import ListItemText from '@material-ui/core/ListItemText' import React, { useEffect } from 'react' import SettingsIcon from '@material-ui/icons/Settings' import Dialog from '@material-ui/core/Dialog' import DialogTitle from '@material-ui/core/DialogTitle' import DialogContent from '@material-ui/core/DialogContent' import TextField from '@material-ui/core/TextField' import DialogActions from '@material-ui/core/DialogActions' import Button from '@material-ui/core/Button' import { FormControlLabel, InputLabel, Select, Switch } from '@material-ui/core' import { settingsHost, setTorrServerHost, torrserverHost } from '../utils/Hosts' export default function SettingsDialog() { const [open, setOpen] = React.useState(false) const [settings, setSets] = React.useState({}) const [show, setShow] = React.useState(false) const [tsHost, setTSHost] = React.useState(torrserverHost ? torrserverHost : window.location.protocol + '//' + window.location.hostname + (window.location.port ? ':' + window.location.port : '')) const handleClickOpen = () => { setOpen(true) } const handleClose = () => { setOpen(false) } const handleCloseSave = () => { setOpen(false) let sets = JSON.parse(JSON.stringify(settings)) sets.CacheSize *= 1024 * 1024 sets.PreloadBufferSize *= 1024 * 1024 fetch(settingsHost(), { method: 'post', body: JSON.stringify({ action: 'set', sets: sets }), headers: { Accept: 'application/json, text/plain, */*', 'Content-Type': 'application/json', }, }) } useEffect(() => { fetch(settingsHost(), { method: 'post', body: JSON.stringify({ action: 'get' }), headers: { Accept: 'application/json, text/plain, */*', 'Content-Type': 'application/json', }, }) .then((res) => res.json()) .then( (json) => { json.CacheSize /= 1024 * 1024 json.PreloadBufferSize /= 1024 * 1024 setSets(json) setShow(true) }, (error) => { setShow(false) console.log(error) } ) .catch((e) => { setShow(false) console.log(e) }) }, [tsHost]) const onInputHost = (event) => { let host = event.target.value setTorrServerHost(host) setTSHost(host) } const inputForm = (event) => { let sets = JSON.parse(JSON.stringify(settings)) if (event.target.type === 'number' || event.target.type === 'select-one') { sets[event.target.id] = Number(event.target.value) } else if (event.target.type === 'checkbox') { sets[event.target.id] = Boolean(event.target.checked) } setSets(sets) } return (