import React, { Component } from "react"; import PropTypes from "prop-types"; import { connect } from "react-redux"; import { getTutorials, getAllTutorials, getUserTutorials, resetTutorial, tutorialProgress, } from "../../actions/tutorialActions"; import { progress } from "../../actions/tutorialBuilderActions"; import { clearMessages } from "../../actions/messageActions"; import clsx from "clsx"; import Breadcrumbs from "../Breadcrumbs"; import { Link } from "react-router-dom"; import { alpha } from "@material-ui/core/styles"; import { withStyles } from "@material-ui/core/styles"; import Grid from "@material-ui/core/Grid"; import Paper from "@material-ui/core/Paper"; import Typography from "@material-ui/core/Typography"; import { faCheck, faTimes, faShareAlt, faEye, faUserCheck, } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import * as Blockly from "blockly"; import ReactStars from "react-rating-stars-component"; import Tooltip from "@material-ui/core/Tooltip"; import IconButton from "@material-ui/core/IconButton"; import Snackbar from "../Snackbar"; import Divider from "@material-ui/core/Divider"; import DeviceSelection from "../DeviceSelection"; const styles = (theme) => ({ outerDiv: { position: "absolute", right: "-30px", bottom: "-30px", width: "160px", height: "160px", color: alpha(theme.palette.secondary.main, 0.6), }, outerDivError: { stroke: alpha(theme.palette.error.dark, 0.6), color: alpha(theme.palette.error.dark, 0.6), }, outerDivSuccess: { stroke: alpha(theme.palette.primary.main, 0.6), color: alpha(theme.palette.primary.main, 0.6), }, outerDivOther: { stroke: alpha(theme.palette.secondary.main, 0.6), }, innerDiv: { width: "inherit", height: "inherit", display: "table-cell", verticalAlign: "middle", textAlign: "center", }, button: { backgroundColor: theme.palette.primary.main, color: theme.palette.primary.contrastText, width: "40px", height: "40px", "&:hover": { backgroundColor: theme.palette.primary.main, color: theme.palette.primary.contrastText, }, }, link: { color: theme.palette.primary.main, textDecoration: "none", "&:hover": { color: theme.palette.primary.main, textDecoration: "underline", }, }, }); class TutorialHome extends Component { constructor(props) { super(props); this.state = { userTutorials: [], tutorials: [], snackbar: false, }; } componentDidMount() { this.props.tutorialProgress(); // retrieve tutorials only if a potential user is loaded - authentication // is finished (success or failed) // if (!this.props.progress) { // if (this.props.user) { // if (this.props.user.blocklyRole === "admin") { // this.props.getAllTutorials(); // } // if (this.props.user.blocklyRole === "creator") { // this.props.getUserTutorials(); // this.props.getTutorials(); // console.log("get user tutorials"); // console.log(this.props.userTutorials); // } // } else { // this.props.getTutorials(); // } // } if (!this.props.authProgress) { if (this.props.user) { if (this.props.user.role === "admin") { this.props.getAllTutorials(); } else { this.props.getUserTutorials(); //this.props.getTutorials(); } } else { this.props.getTutorials(); } } } componentDidUpdate(props, state) { if ( props.authProgress !== this.props.authProgress && !this.props.authProgress ) if (this.props.user) { if (this.props.user.role === "admin") { // authentication is completed this.props.getAllTutorials(); } else { this.props.getUserTutorials(); } } else { this.props.getTutorials(); } if (this.props.message.id === "GET_TUTORIALS_FAIL") { alert(this.props.message.msg); } } componentWillUnmount() { this.props.resetTutorial(); if (this.props.message.msg) { this.props.clearMessages(); } } render() { var userTutorials = []; const publicTutorials = this.props.tutorials.filter( (tutorial) => tutorial.public === true ); if (this.props.user && this.props.user.blocklyRole === "admin") { userTutorials = this.props.tutorials; } if (this.props.user && this.props.user.blocklyRole === "creator") { userTutorials = this.props.tutorials.filter( (tutorial) => tutorial.creator === this.props.user.email ); } return this.props.isLoading ? null : (

{Blockly.Msg.tutorials_home_head}

Alle Tutorials

{publicTutorials.map((tutorial, i) => { var status = this.props.status.filter( (status) => status._id === tutorial._id )[0]; var tasks = status.tasks; var error = status.tasks.filter((task) => task.type === "error").length > 0; var success = status.tasks.filter((task) => task.type === "success").length / tasks.length; var tutorialStatus = success === 1 ? "Success" : error ? "Error" : "Other"; const firstExample = { size: 30, value: tutorial.difficulty, edit: false, isHalf: true, }; return ( {tutorial.title}
{error || success === 1 ? ( ) : ( )} {success < 1 && !error ? ( ) : null}
{error || success === 1 ? ( ) : ( 0 ? this.props.classes.outerDivSuccess : {} } > {Math.round(success * 100)}% )}
); })}
{this.props.user ? (

User Tutorials

{userTutorials.map((tutorial, i) => { var status = this.props.status.filter( (status) => status._id === tutorial._id )[0]; var tasks = status.tasks; var error = status.tasks.filter((task) => task.type === "error").length > 0; var success = status.tasks.filter((task) => task.type === "success") .length / tasks.length; var tutorialStatus = success === 1 ? "Success" : error ? "Error" : "Other"; const firstExample = { size: 30, value: tutorial.difficulty, edit: false, isHalf: true, }; return ( {tutorial.title}

Creator:{tutorial.creator}

{ navigator.clipboard.writeText( `${window.location.origin}/tutorial/${tutorial._id}` ); this.setState({ snackbar: true, key: Date.now(), message: Blockly.Msg.messages_copylink_success, type: "success", }); }} > {tutorial.review ? ( ) : null}

); })}
) : null}
); } } TutorialHome.propTypes = { getTutorials: PropTypes.func.isRequired, getAllTutorials: PropTypes.func.isRequired, getUserTutorials: PropTypes.func.isRequired, resetTutorial: PropTypes.func.isRequired, tutorialProgress: PropTypes.func.isRequired, clearMessages: PropTypes.func.isRequired, status: PropTypes.array.isRequired, change: PropTypes.number.isRequired, tutorials: PropTypes.array.isRequired, isLoading: PropTypes.bool.isRequired, message: PropTypes.object.isRequired, progress: PropTypes.bool.isRequired, user: PropTypes.object.isRequired, authProgress: PropTypes.bool.isRequired, }; const mapStateToProps = (state) => ({ change: state.tutorial.change, status: state.tutorial.status, tutorials: state.tutorial.tutorials, userTutorials: state.tutorial.userTutorials, isLoading: state.tutorial.progress, message: state.message, progress: state.auth.progress, user: state.auth.user, authProgress: state.auth.progress, }); export default connect(mapStateToProps, { getTutorials, progress, getUserTutorials, getAllTutorials, resetTutorial, clearMessages, tutorialProgress, })(withStyles(styles, { withTheme: true })(TutorialHome));