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-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 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
|
|
|
|
topic={topic}
|
|
|
|
onBack={() => setShowLoginPage(false)}
|
|
|
|
/>}
|
|
|
|
</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-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"
|
|
|
|
type="text"
|
|
|
|
fullWidth
|
|
|
|
variant="standard"
|
|
|
|
/>
|
|
|
|
<TextField
|
|
|
|
margin="dense"
|
|
|
|
id="password"
|
|
|
|
label="Password"
|
|
|
|
type="password"
|
|
|
|
fullWidth
|
|
|
|
variant="standard"
|
|
|
|
/>
|
|
|
|
</DialogContent>
|
|
|
|
<DialogActions>
|
|
|
|
<Button onClick={props.onBack}>Back</Button>
|
|
|
|
<Button>Login</Button>
|
|
|
|
</DialogActions>
|
2022-02-20 03:26:58 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-02-25 01:18:46 +00:00
|
|
|
export default SubscribeDialog;
|