import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import { connectMyBadges, disconnectMyBadges } from '../../actions/authActions'; import axios from 'axios'; import { withRouter } from 'react-router-dom'; import Breadcrumbs from '../Breadcrumbs'; import Alert from '../Alert'; import { withStyles } from '@material-ui/core/styles'; import Paper from '@material-ui/core/Paper'; import Button from '@material-ui/core/Button'; import IconButton from '@material-ui/core/IconButton'; import TextField from '@material-ui/core/TextField'; import Divider from '@material-ui/core/Divider'; import InputAdornment from '@material-ui/core/InputAdornment'; import Link from '@material-ui/core/Link'; import Typography from '@material-ui/core/Typography'; import Grid from '@material-ui/core/Grid'; import Avatar from '@material-ui/core/Avatar'; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faEye, faEyeSlash } from "@fortawesome/free-solid-svg-icons"; const styles = (theme) => ({ root: { '& label.Mui-focused': { color: '#aed9c8' }, '& .MuiOutlinedInput-root': { '&.Mui-focused fieldset': { borderColor: '#aed9c8' }, borderRadius: '0.75rem' } }, text: { fontFamily: [ '"Open Sans"', 'BlinkMacSystemFont', '"Segoe UI"', 'Roboto', '"Helvetica Neue"', 'Arial', 'sans-serif', '"Apple Color Emoji"', '"Segoe UI Emoji"', '"Segoe UI Symbol"', ].join(','), fontSize: 16 } }); export class MyBadges extends Component { constructor(props) { super(props); this.state = { username: '', password: '', showPassword: false, msg: '', badges: [], progress: false }; } componentDidMount(){ if(this.props.user.badge){ this.getBadges(); } } componentDidUpdate(props){ const { message } = this.props; if (message !== props.message) { // Check for login error if(message.id === 'MYBADGES_CONNECT_FAIL'){ this.setState({msg: 'Der Benutzername oder das Passwort ist nicht korrekt.', username: '', password: '', showPassword: false}); } else if(message.id === 'MYBADGES_CONNECT_SUCCESS'){ this.getBadges(); } else if(message.id === 'MYBADGES_DISCONNECT_SUCCESS' || message.id === 'MYBADGES_DISCONNECT_FAIL'){ this.setState({progress: false}); } else { this.setState({msg: null}); } } } getBadges = () => { this.setState({progress: true}); const config = { success: res => { this.setState({badges: res.data.badges, progress: false}); }, error: err => { this.setState({progress: false}); } }; axios.get(`${process.env.REACT_APP_BLOCKLY_API}/user/badge`, config) .then(res => { res.config.success(res); }) .catch(err => { err.config.error(err); }); }; onChange = e => { this.setState({ [e.target.name]: e.target.value, msg: '' }); }; onSubmit = e => { e.preventDefault(); const {username, password} = this.state; // create user object const user = { username, password }; this.props.connectMyBadges(user); }; handleClickShowPassword = () => { this.setState({ showPassword: !this.state.showPassword }); }; handleMouseDownPassword = (e) => { e.preventDefault(); }; render(){ return(
{!this.props.user.badge ? Du kannst dein Blockly-Konto mit deinem MyBadges-Konto verknüpfen, um Badges erwerben zu können. : null}
My Badges
{!this.props.user.badge ?
{this.state.msg ?
{this.state.msg}
: null } }} onChange={this.onChange} fullWidth={true} />

Passwort vergessen?

Du hast noch kein Konto? Registrieren

:
MyBadges-Konto ist erfolgreich verknüpft.
}
{this.props.user.badge && !this.state.progress ? {this.state.badges && this.state.badges.length > 0 ? Du hast {this.state.badges.length} {this.state.badges.length === 1 ? 'Badge' : 'Badges'} im Kontext Blockly for senseBox erreicht. : null} {this.state.badges && this.state.badges.length > 0 ? this.state.badges.map(badge => ( {badge.image && badge.image.path ? : }
{badge.name}
)) : Du hast noch keine Badges im Kontext senseBox for Blockly erreicht. }
: null}
); } } MyBadges.propTypes = { connectMyBadges: PropTypes.func.isRequired, disconnectMyBadges: PropTypes.func.isRequired, message: PropTypes.object.isRequired, user: PropTypes.object.isRequired }; const mapStateToProps = state => ({ message: state.message, user: state.auth.user }); export default connect(mapStateToProps, { connectMyBadges, disconnectMyBadges })(withStyles(styles, { withTheme: true })(withRouter(MyBadges)));