2022-01-02 00:19:18 +00:00
|
|
|
import React, { Component } from "react";
|
2022-01-18 13:20:19 +00:00
|
|
|
import { Button , Dialog, Grid, Typography, TextField, ButtonGroup, CircularProgress, IconButton} from "@mui/material"
|
2022-01-02 21:41:22 +00:00
|
|
|
import { Link } from 'react-router-dom'
|
|
|
|
import Image from 'material-ui-image'
|
2022-01-15 00:28:19 +00:00
|
|
|
import InfoDialog from './InfoDialog'
|
2022-01-18 13:20:19 +00:00
|
|
|
import ContentCopyIcon from '@mui/icons-material/ContentCopy';
|
|
|
|
import ContentCopy from "@mui/icons-material/ContentCopy";
|
2022-01-02 00:19:18 +00:00
|
|
|
|
2022-01-03 01:31:28 +00:00
|
|
|
function getCookie(name) {
|
|
|
|
let cookieValue = null;
|
|
|
|
if (document.cookie && document.cookie !== '') {
|
|
|
|
const cookies = document.cookie.split(';');
|
|
|
|
for (let i = 0; i < cookies.length; i++) {
|
|
|
|
const cookie = cookies[i].trim();
|
|
|
|
// Does this cookie string begin with the name we want?
|
|
|
|
if (cookie.substring(0, name.length + 1) === (name + '=')) {
|
|
|
|
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cookieValue;
|
|
|
|
}
|
|
|
|
const csrftoken = getCookie('csrftoken');
|
|
|
|
|
2022-01-02 15:19:49 +00:00
|
|
|
export default class UserGenPage extends Component {
|
2022-01-02 17:42:33 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
2022-01-10 01:12:58 +00:00
|
|
|
token: this.genBase62Token(34),
|
2022-01-15 00:28:19 +00:00
|
|
|
openInfo: false,
|
2022-01-18 13:20:19 +00:00
|
|
|
showRobosat: true,
|
2022-01-02 17:42:33 +00:00
|
|
|
};
|
2022-01-09 14:43:47 +00:00
|
|
|
this.getGeneratedUser(this.state.token);
|
2022-01-02 17:42:33 +00:00
|
|
|
}
|
2022-01-02 18:27:40 +00:00
|
|
|
|
2022-01-02 17:42:33 +00:00
|
|
|
// sort of cryptographically strong function to generate Base62 token client-side
|
|
|
|
genBase62Token(length)
|
|
|
|
{
|
|
|
|
return window.btoa(Array.from(
|
|
|
|
window.crypto.getRandomValues(
|
|
|
|
new Uint8Array(length * 2)))
|
|
|
|
.map((b) => String.fromCharCode(b))
|
|
|
|
.join("")).replace(/[+/]/g, "")
|
|
|
|
.substring(0, length);
|
|
|
|
}
|
|
|
|
|
2022-01-09 14:43:47 +00:00
|
|
|
getGeneratedUser(token) {
|
|
|
|
fetch('/api/user' + '?token=' + token)
|
2022-01-02 18:27:40 +00:00
|
|
|
.then((response) => response.json())
|
|
|
|
.then((data) => {
|
|
|
|
this.setState({
|
|
|
|
nickname: data.nickname,
|
|
|
|
bit_entropy: data.token_bits_entropy,
|
2022-01-02 21:41:22 +00:00
|
|
|
avatar_url: 'static/assets/avatars/' + data.nickname + '.png',
|
2022-01-02 18:27:40 +00:00
|
|
|
shannon_entropy: data.token_shannon_entropy,
|
|
|
|
bad_request: data.bad_request,
|
2022-01-02 21:41:22 +00:00
|
|
|
found: data.found,
|
2022-01-18 13:20:19 +00:00
|
|
|
showRobosat:true,
|
2022-01-02 18:27:40 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-03 01:31:28 +00:00
|
|
|
delGeneratedUser() {
|
|
|
|
const requestOptions = {
|
|
|
|
method: 'DELETE',
|
2022-01-06 12:32:17 +00:00
|
|
|
headers: {'Content-Type':'application/json', 'X-CSRFToken': getCookie('csrftoken')},
|
2022-01-03 01:31:28 +00:00
|
|
|
};
|
2022-01-09 15:24:41 +00:00
|
|
|
fetch("/api/user", requestOptions)
|
2022-01-03 01:31:28 +00:00
|
|
|
.then((response) => response.json())
|
|
|
|
.then((data) => console.log(data));
|
|
|
|
}
|
|
|
|
|
2022-01-02 21:41:22 +00:00
|
|
|
handleAnotherButtonPressed=(e)=>{
|
2022-01-03 01:31:28 +00:00
|
|
|
this.delGeneratedUser()
|
2022-01-18 13:20:19 +00:00
|
|
|
// this.setState({
|
|
|
|
// showRobosat: false,
|
|
|
|
// token: this.genBase62Token(34),
|
|
|
|
// });
|
|
|
|
// this.getGeneratedUser(this.state.token);
|
|
|
|
window.location.reload();
|
2022-01-02 21:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleChangeToken=(e)=>{
|
2022-01-03 01:31:28 +00:00
|
|
|
this.delGeneratedUser()
|
2022-01-02 21:41:22 +00:00
|
|
|
this.setState({
|
|
|
|
token: e.target.value,
|
|
|
|
})
|
2022-01-09 14:43:47 +00:00
|
|
|
this.getGeneratedUser(e.target.value);
|
2022-01-18 13:20:19 +00:00
|
|
|
this.setState({showRobosat: false})
|
2022-01-02 21:41:22 +00:00
|
|
|
}
|
2022-01-02 18:27:40 +00:00
|
|
|
|
2022-01-15 00:28:19 +00:00
|
|
|
handleClickOpenInfo = () => {
|
|
|
|
this.setState({openInfo: true});
|
|
|
|
};
|
|
|
|
|
|
|
|
handleCloseInfo = () => {
|
|
|
|
this.setState({openInfo: false});
|
|
|
|
};
|
|
|
|
|
|
|
|
InfoDialog =() =>{
|
|
|
|
return(
|
|
|
|
<Dialog
|
|
|
|
open={this.state.openInfo}
|
|
|
|
onClose={this.handleCloseInfo}
|
|
|
|
aria-labelledby="info-dialog-title"
|
|
|
|
aria-describedby="info-dialog-description"
|
|
|
|
scroll="paper"
|
|
|
|
>
|
|
|
|
<InfoDialog/>
|
|
|
|
</Dialog>
|
|
|
|
)
|
2022-01-04 15:00:34 +00:00
|
|
|
}
|
|
|
|
|
2022-01-15 00:28:19 +00:00
|
|
|
|
2022-01-02 17:42:33 +00:00
|
|
|
render() {
|
|
|
|
return (
|
2022-01-02 21:41:22 +00:00
|
|
|
<Grid container spacing={1}>
|
2022-01-18 13:20:19 +00:00
|
|
|
<Grid item xs={12} align="center">
|
|
|
|
{this.state.showRobosat ?
|
|
|
|
<div>
|
|
|
|
<Grid item xs={12} align="center">
|
|
|
|
<Typography component="h5" variant="h5">
|
|
|
|
<b>{this.state.nickname ? "⚡"+this.state.nickname+"⚡" : ""}</b>
|
|
|
|
</Typography>
|
|
|
|
</Grid>
|
|
|
|
<Grid item xs={12} align="center">
|
|
|
|
<div style={{ maxWidth: 200, maxHeight: 200 }}>
|
|
|
|
<Image className='newAvatar'
|
|
|
|
disableError='true'
|
|
|
|
cover='true'
|
|
|
|
color='null'
|
|
|
|
src={this.state.avatar_url}
|
|
|
|
/>
|
|
|
|
</div><br/>
|
|
|
|
</Grid>
|
|
|
|
</div>
|
|
|
|
: <CircularProgress />}
|
2022-01-02 21:41:22 +00:00
|
|
|
</Grid>
|
|
|
|
{
|
|
|
|
this.state.found ?
|
|
|
|
<Grid item xs={12} align="center">
|
|
|
|
<Typography component="subtitle2" variant="subtitle2" color='primary'>
|
2022-01-03 09:06:51 +00:00
|
|
|
{this.state.found}<br/>
|
2022-01-02 21:41:22 +00:00
|
|
|
</Typography>
|
|
|
|
</Grid>
|
|
|
|
:
|
2022-01-03 14:27:25 +00:00
|
|
|
""
|
2022-01-02 21:41:22 +00:00
|
|
|
}
|
2022-01-18 13:20:19 +00:00
|
|
|
<Grid container align="center">
|
|
|
|
<Grid item xs={12} align="center">
|
|
|
|
<IconButton onClick= {()=>navigator.clipboard.writeText(this.state.token)}>
|
2022-01-18 15:23:57 +00:00
|
|
|
<ContentCopy/>
|
2022-01-18 13:20:19 +00:00
|
|
|
</IconButton>
|
|
|
|
<TextField
|
|
|
|
//sx={{ input: { color: 'purple' } }}
|
|
|
|
InputLabelProps={{
|
2022-01-18 15:23:57 +00:00
|
|
|
style: { color: 'green' },
|
2022-01-18 13:20:19 +00:00
|
|
|
}}
|
|
|
|
error={this.state.bad_request}
|
2022-01-18 15:23:57 +00:00
|
|
|
label='Store your token safely'
|
2022-01-18 13:20:19 +00:00
|
|
|
required='true'
|
|
|
|
value={this.state.token}
|
|
|
|
variant='standard'
|
|
|
|
helperText={this.state.bad_request}
|
|
|
|
size='small'
|
|
|
|
// multiline = {true}
|
|
|
|
onChange={this.handleChangeToken}
|
|
|
|
/>
|
|
|
|
</Grid>
|
2022-01-03 14:27:25 +00:00
|
|
|
</Grid>
|
|
|
|
<Grid item xs={12} align="center">
|
2022-01-18 13:20:19 +00:00
|
|
|
<Button size='small' onClick={this.handleAnotherButtonPressed}>Generate Another Robosat</Button>
|
2022-01-03 14:27:25 +00:00
|
|
|
</Grid>
|
|
|
|
<Grid item xs={12} align="center">
|
|
|
|
<ButtonGroup variant="contained" aria-label="outlined primary button group">
|
2022-01-04 13:47:37 +00:00
|
|
|
<Button color='primary' to='/make/' component={Link}>Make Order</Button>
|
2022-01-15 00:28:19 +00:00
|
|
|
<Button color='inherit' onClick={this.handleClickOpenInfo}>Info</Button>
|
|
|
|
<this.InfoDialog/>
|
2022-01-04 13:47:37 +00:00
|
|
|
<Button color='secondary' to='/book/' component={Link}>View Book</Button>
|
2022-01-03 14:27:25 +00:00
|
|
|
</ButtonGroup>
|
|
|
|
</Grid>
|
|
|
|
<Grid item xs={12} align="center">
|
|
|
|
<Typography component="h5" variant="h5">
|
2022-01-08 20:14:19 +00:00
|
|
|
Simple and Private Lightning peer-to-peer Exchange
|
2022-01-03 14:27:25 +00:00
|
|
|
</Typography>
|
2022-01-02 21:41:22 +00:00
|
|
|
</Grid>
|
|
|
|
</Grid>
|
2022-01-02 17:42:33 +00:00
|
|
|
);
|
|
|
|
}
|
2022-01-02 00:19:18 +00:00
|
|
|
}
|