import React, { Component } from "react";
import { Button , Dialog, Grid, Typography, TextField, ButtonGroup, CircularProgress, IconButton} from "@mui/material"
import { Link } from 'react-router-dom'
import Image from 'material-ui-image'
import InfoDialog from './InfoDialog'
import SmartToyIcon from '@mui/icons-material/SmartToy';
import CasinoIcon from '@mui/icons-material/Casino';
import ContentCopy from "@mui/icons-material/ContentCopy";
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');
export default class UserGenPage extends Component {
constructor(props) {
super(props);
this.state = {
token: this.genBase62Token(34),
openInfo: false,
loadingRobot: true,
tokenHasChanged: false,
};
this.getGeneratedUser(this.state.token);
}
// 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);
}
getGeneratedUser=(token)=>{
fetch('/api/user' + '?token=' + token)
.then((response) => response.json())
.then((data) => {
this.setState({
nickname: data.nickname,
bit_entropy: data.token_bits_entropy,
avatar_url: 'static/assets/avatars/' + data.nickname + '.png',
shannon_entropy: data.token_shannon_entropy,
bad_request: data.bad_request,
found: data.found,
loadingRobot:false,
})
&
// Add nick and token to App state (token only if not a bad request)
(data.bad_request ? this.props.setAppState({
nickname: data.nickname,
})
:
this.props.setAppState({
nickname: data.nickname,
token: this.state.token,
}));
});
}
delGeneratedUser() {
const requestOptions = {
method: 'DELETE',
headers: {'Content-Type':'application/json', 'X-CSRFToken': getCookie('csrftoken')},
};
fetch("/api/user", requestOptions)
.then((response) => response.json())
.then((data) => console.log(data));
}
handleClickNewRandomToken=()=>{
this.setState({
token: this.genBase62Token(34),
tokenHasChanged: true,
});
}
handleChangeToken=(e)=>{
this.setState({
token: e.target.value,
tokenHasChanged: true,
})
}
handleClickSubmitToken=()=>{
this.delGeneratedUser()
this.getGeneratedUser(this.state.token);
this.setState({loadingRobot: true, tokenHasChanged: false})
}
handleClickOpenInfo = () => {
this.setState({openInfo: true});
};
handleCloseInfo = () => {
this.setState({openInfo: false});
};
InfoDialog =() =>{
return(
)
}
render() {
return (
{!this.state.loadingRobot ?
{this.state.nickname ? "⚡"+this.state.nickname+"⚡" : ""}
: }
{
this.state.found ?
{this.state.found}
:
""
}
{
if (e.key === 'Enter') {
this.handleClickSubmitToken();
}
}}
InputProps={{
startAdornment:
navigator.clipboard.writeText(this.state.token)}>
,
endAdornment:
,
}}
/>
Simple and Private Lightning peer-to-peer Exchange
);
}
}