2022-02-20 03:26:58 +00:00
|
|
|
import * as React from 'react';
|
|
|
|
import Button from '@mui/material/Button';
|
|
|
|
import TextField from '@mui/material/TextField';
|
|
|
|
import Dialog from '@mui/material/Dialog';
|
|
|
|
import DialogActions from '@mui/material/DialogActions';
|
|
|
|
import DialogContent from '@mui/material/DialogContent';
|
|
|
|
import DialogContentText from '@mui/material/DialogContentText';
|
|
|
|
import DialogTitle from '@mui/material/DialogTitle';
|
|
|
|
import {useState} from "react";
|
2022-02-20 21:55:55 +00:00
|
|
|
import Subscription from "../app/Subscription";
|
2022-02-25 18:40:03 +00:00
|
|
|
import {useMediaQuery} from "@mui/material";
|
|
|
|
import theme from "./theme";
|
|
|
|
import api from "../app/Api";
|
|
|
|
import {topicUrl} from "../app/utils";
|
2022-02-25 21:07:25 +00:00
|
|
|
import useStyles from "./styles";
|
2022-02-20 03:26:58 +00:00
|
|
|
|
2022-02-24 14:52:49 +00:00
|
|
|
const defaultBaseUrl = "http://127.0.0.1"
|
|
|
|
//const defaultBaseUrl = "https://ntfy.sh"
|
2022-02-20 03:26:58 +00:00
|
|
|
|
2022-02-25 01:18:46 +00:00
|
|
|
const SubscribeDialog = (props) => {
|
2022-02-25 18:40:03 +00:00
|
|
|
const [baseUrl, setBaseUrl] = useState(defaultBaseUrl); // FIXME
|
2022-02-20 03:26:58 +00:00
|
|
|
const [topic, setTopic] = useState("");
|
2022-02-25 21:07:25 +00:00
|
|
|
const [user, setUser] = useState(null);
|
2022-02-25 18:40:03 +00:00
|
|
|
const [showLoginPage, setShowLoginPage] = useState(false);
|
|
|
|
const fullScreen = useMediaQuery(theme.breakpoints.down('sm'));
|
2022-02-20 03:26:58 +00:00
|
|
|
const handleCancel = () => {
|
|
|
|
setTopic('');
|
|
|
|
props.onCancel();
|
|
|
|
}
|
2022-02-25 18:40:03 +00:00
|
|
|
const handleSubmit = async () => {
|
|
|
|
const success = await api.auth(baseUrl, topic, null);
|
|
|
|
if (!success) {
|
|
|
|
console.log(`[SubscribeDialog] Login required for ${topicUrl(baseUrl, topic)}`)
|
|
|
|
setShowLoginPage(true);
|
|
|
|
return;
|
|
|
|
}
|
2022-02-20 03:26:58 +00:00
|
|
|
const subscription = new Subscription(defaultBaseUrl, topic);
|
|
|
|
props.onSubmit(subscription);
|
|
|
|
setTopic('');
|
|
|
|
}
|
2022-02-25 18:40:03 +00:00
|
|
|
return (
|
|
|
|
<Dialog open={props.open} onClose={props.onClose} fullScreen={fullScreen}>
|
|
|
|
{!showLoginPage && <SubscribePage
|
|
|
|
topic={topic}
|
|
|
|
setTopic={setTopic}
|
|
|
|
onCancel={handleCancel}
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
/>}
|
|
|
|
{showLoginPage && <LoginPage
|
2022-02-25 21:07:25 +00:00
|
|
|
baseUrl={baseUrl}
|
2022-02-25 18:40:03 +00:00
|
|
|
topic={topic}
|
|
|
|
onBack={() => setShowLoginPage(false)}
|
2022-02-25 21:07:25 +00:00
|
|
|
onSubmit={handleSubmit}
|
2022-02-25 18:40:03 +00:00
|
|
|
/>}
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const SubscribePage = (props) => {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<DialogTitle>Subscribe to topic</DialogTitle>
|
|
|
|
<DialogContent>
|
|
|
|
<DialogContentText>
|
|
|
|
Topics may not be password-protected, so choose a name that's not easy to guess.
|
|
|
|
Once subscribed, you can PUT/POST notifications.
|
|
|
|
</DialogContentText>
|
|
|
|
<TextField
|
|
|
|
autoFocus
|
|
|
|
margin="dense"
|
|
|
|
id="topic"
|
|
|
|
label="Topic name, e.g. phil_alerts"
|
|
|
|
value={props.topic}
|
|
|
|
onChange={ev => props.setTopic(ev.target.value)}
|
|
|
|
type="text"
|
|
|
|
fullWidth
|
|
|
|
variant="standard"
|
|
|
|
/>
|
|
|
|
</DialogContent>
|
|
|
|
<DialogActions>
|
|
|
|
<Button onClick={props.onCancel}>Cancel</Button>
|
|
|
|
<Button onClick={props.onSubmit} disabled={props.topic === ""}>Subscribe</Button>
|
|
|
|
</DialogActions>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const LoginPage = (props) => {
|
2022-02-25 21:07:25 +00:00
|
|
|
const styles = useStyles();
|
|
|
|
const [username, setUsername] = useState("");
|
|
|
|
const [password, setPassword] = useState("");
|
|
|
|
const [errorText, setErrorText] = useState("");
|
|
|
|
const baseUrl = props.baseUrl;
|
|
|
|
const topic = props.topic;
|
|
|
|
const handleLogin = async () => {
|
|
|
|
const user = {username: username, password: password};
|
|
|
|
const success = await api.auth(baseUrl, topic, user);
|
|
|
|
if (!success) {
|
|
|
|
console.log(`[SubscribeDialog] Login to ${topicUrl(baseUrl, topic)} failed for user ${username}`);
|
|
|
|
setErrorText(`User ${username} not authorized`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log(`[SubscribeDialog] Login to ${topicUrl(baseUrl, topic)} successful for user ${username}`);
|
|
|
|
};
|
2022-02-20 03:26:58 +00:00
|
|
|
return (
|
|
|
|
<>
|
2022-02-25 18:40:03 +00:00
|
|
|
<DialogTitle>Login required</DialogTitle>
|
|
|
|
<DialogContent>
|
|
|
|
<DialogContentText>
|
|
|
|
This topic is password-protected. Please enter username and
|
|
|
|
password to subscribe.
|
|
|
|
</DialogContentText>
|
|
|
|
<TextField
|
|
|
|
autoFocus
|
|
|
|
margin="dense"
|
|
|
|
id="username"
|
|
|
|
label="Username, e.g. phil"
|
2022-02-25 21:07:25 +00:00
|
|
|
value={username}
|
|
|
|
onChange={ev => setUsername(ev.target.value)}
|
2022-02-25 18:40:03 +00:00
|
|
|
type="text"
|
|
|
|
fullWidth
|
|
|
|
variant="standard"
|
|
|
|
/>
|
|
|
|
<TextField
|
|
|
|
margin="dense"
|
|
|
|
id="password"
|
|
|
|
label="Password"
|
|
|
|
type="password"
|
2022-02-25 21:07:25 +00:00
|
|
|
value={password}
|
|
|
|
onChange={ev => setPassword(ev.target.value)}
|
2022-02-25 18:40:03 +00:00
|
|
|
fullWidth
|
|
|
|
variant="standard"
|
|
|
|
/>
|
|
|
|
</DialogContent>
|
2022-02-25 21:07:25 +00:00
|
|
|
<div className={styles.bottomBar}>
|
|
|
|
<DialogContentText className={styles.statusText}>
|
|
|
|
{errorText}
|
|
|
|
</DialogContentText>
|
|
|
|
<DialogActions>
|
|
|
|
<Button onClick={props.onBack}>Back</Button>
|
|
|
|
<Button onClick={handleLogin}>Login</Button>
|
|
|
|
</DialogActions>
|
|
|
|
</div>
|
2022-02-20 03:26:58 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-02-25 01:18:46 +00:00
|
|
|
export default SubscribeDialog;
|