import React, { Component } from "react"; import PropTypes from "prop-types"; import { connect } from "react-redux"; import { shareProject } from "../../actions/projectActions"; import { clearMessages } from "../../actions/messageActions"; import QRCode from "react-qr-code"; // import axios from 'axios'; import moment from "moment"; import Dialog from "../Dialog"; import Snackbar from "../Snackbar"; // import { Link } from "react-router-dom"; import GridLoader from "react-spinners/GridLoader"; import { withStyles } from "@material-ui/core/styles"; import IconButton from "@material-ui/core/IconButton"; import Tooltip from "@material-ui/core/Tooltip"; import Typography from "@material-ui/core/Typography"; import { faShareAlt, faCopy } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import * as Blockly from "blockly/core"; const styles = (theme) => ({ 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 WorkspaceFunc extends Component { constructor(props) { super(props); this.inputRef = React.createRef(); this.state = { snackbar: false, type: "", key: "", message: "", title: "", content: "", open: false, id: "", shortLink: "", isFetching: false, loading: false, }; } componentDidUpdate(props) { if (this.props.message !== props.message) { if ( this.props.message.id === "SHARE_SUCCESS" && (!this.props.multiple || this.props.message.status === this.props.project._id) ) { this.createShortlink(this.props.message.status); this.setState({ share: true, open: true, title: Blockly.Msg.messages_SHARE_SUCCESS, id: this.props.message.status, }); } else if ( this.props.message.id === "SHARE_FAIL" && (!this.props.multiple || this.props.message.status === this.props.project._id) ) { this.setState({ snackbar: true, key: Date.now(), message: Blockly.Msg.messages_SHARE_FAIL, type: "error", }); window.scrollTo(0, 0); } } } componentWillUnmount() { this.props.clearMessages(); } toggleDialog = () => { this.setState({ open: !this.state, title: "", content: "" }); }; shareBlocks = () => { if (this.props.projectType === "project" && this.props.project.shared) { // project is already shared this.setState({ open: true, title: Blockly.Msg.messages_SHARE_SUCCESS, id: this.props.project._id, }); } else { this.props.shareProject( this.props.name || this.props.project.title, this.props.projectType, this.props.project ? this.props.project._id : undefined ); } }; createShortlink(id) { this.setState({ isFetching: true, loading: true }) const requestOptions = { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ "slug": `blockly-${id}`, "url": `${window.location.origin}/share/${id}` }) }; fetch('https://www.snsbx.de/api/shorty', requestOptions) .then(response => response.json()) .then(data => this.setState({ shortLink: data[0].link, isFetching: false, loading: false })); } render() { return (