diff --git a/.env b/.env index f3793fa..9af8f29 100644 --- a/.env +++ b/.env @@ -1,3 +1,6 @@ REACT_APP_COMPILER_URL=https://compiler.sensebox.de REACT_APP_BOARD=sensebox-mcu REACT_APP_BLOCKLY_API=https://api.blockly.sensebox.de + +# in days +REACT_APP_SHARE_LINK_EXPIRES=30 diff --git a/src/actions/projectActions.js b/src/actions/projectActions.js index 28c0484..94909ef 100644 --- a/src/actions/projectActions.js +++ b/src/actions/projectActions.js @@ -78,7 +78,7 @@ export const updateProject = () => (dispatch, getState) => { title: workspace.name } var project = getState().project; - var id = project.projects[0]._id; + var id = project.projects[0]._id._id ? project.projects[0]._id._id : project.projects[0]._id; var type = project.type; if(type==='gallery'){ body.description = project.description; @@ -105,7 +105,7 @@ export const updateProject = () => (dispatch, getState) => { export const deleteProject = () => (dispatch, getState) => { var project = getState().project; - var id = project.projects[0]._id; + var id = project.projects[0]._id._id ? project.projects[0]._id._id : project.projects[0]._id; var type = project.type; axios.delete(`${process.env.REACT_APP_BLOCKLY_API}/${type}/${id}`) .then(res => { diff --git a/src/components/Project/Project.js b/src/components/Project/Project.js index 14ba164..80eb59d 100644 --- a/src/components/Project/Project.js +++ b/src/components/Project/Project.js @@ -24,7 +24,6 @@ class Project extends Component { } componentDidUpdate(props) { - console.log(this.props); if(props.location.pathname !== this.props.location.pathname || props.match.params[`${this.props.type}Id`] !== this.props.match.params[`${this.props.type}Id`]){ if(this.props.message.msg){ diff --git a/src/components/Project/ProjectHome.js b/src/components/Project/ProjectHome.js index 668d5d9..5ccd49b 100644 --- a/src/components/Project/ProjectHome.js +++ b/src/components/Project/ProjectHome.js @@ -86,7 +86,7 @@ class ProjectHome extends Component { {this.props.projects.map((project, i) => { return ( - +

{project.title}

diff --git a/src/components/WorkspaceFunc.js b/src/components/WorkspaceFunc.js index ef856ac..a0a7600 100644 --- a/src/components/WorkspaceFunc.js +++ b/src/components/WorkspaceFunc.js @@ -8,6 +8,7 @@ import * as Blockly from 'blockly/core'; import { withRouter } from 'react-router-dom'; import axios from 'axios'; +import moment from 'moment'; import { saveAs } from 'file-saver'; import { detectWhitespacesAndReturnReadableResult } from '../helpers/whitespace'; @@ -149,19 +150,29 @@ class WorkspaceFunc extends Component { } shareBlocks = () => { - var body = { - name: this.state.name, - xml: this.props.xml - }; - axios.post(`${process.env.REACT_APP_BLOCKLY_API}/share`, body) - .then(res => { - var shareContent = res.data.content; - this.setState({ share: true, open: true, title: 'Programm teilen', id: shareContent.link }); - }) - .catch(err => { - this.setState({ snackbar: true, key: Date.now(), message: `Fehler beim Erstellen eines Links zum Teilen deines Programmes. Versuche es noch einmal.`, type: 'error' }); - window.scrollTo(0, 0); - }); + if(this.props.projectType === 'project' && this.props.project._id._id){ + // project is already shared + this.setState({ share: true, open: true, title: 'Programm teilen', id: this.props.project._id._id }); + } + else { + var body = { + title: this.state.name + }; + if(this.props.projectType === 'project'){ + body.projectId = this.props.project._id._id ? this.props.project._id._id : this.props.project._id + } else { + body.xml = this.props.xml; + } + axios.post(`${process.env.REACT_APP_BLOCKLY_API}/share`, body) + .then(res => { + var shareContent = res.data.content; + this.setState({ share: true, open: true, title: 'Programm teilen', id: shareContent._id }); + }) + .catch(err => { + this.setState({ snackbar: true, key: Date.now(), message: `Fehler beim Erstellen eines Links zum Teilen deines Programmes. Versuche es noch einmal.`, type: 'error' }); + window.scrollTo(0, 0); + }); + } } getSvg = () => { @@ -414,6 +425,14 @@ class WorkspaceFunc extends Component { + {this.props.project && this.props.project._id._id ? + {`Das Projekt wurde bereits geteilt. Der Link ist noch ${ + moment(this.props.project._id.expiresAt).diff(moment().utc(), 'days') === 0 ? + moment(this.props.project._id.expiresAt).diff(moment().utc(), 'hours') === 0 ? + `${moment(this.props.project._id.expiresAt).diff(moment().utc(), 'minutes')} Minuten` + : `${moment(this.props.project._id.expiresAt).diff(moment().utc(), 'hours')} Stunden` + : `${moment(this.props.project._id.expiresAt).diff(moment().utc(), 'days')} Tage`} gültig.`} + : {`Der Link ist nun ${process.env.REACT_APP_SHARE_LINK_EXPIRES} Tage gültig.`}} : null} @@ -442,6 +461,7 @@ WorkspaceFunc.propTypes = { name: PropTypes.string.isRequired, description: PropTypes.string.isRequired, projectType: PropTypes.string.isRequired, + project: PropTypes.object.isRequired, message: PropTypes.object.isRequired }; @@ -451,6 +471,7 @@ const mapStateToProps = state => ({ name: state.workspace.name, description: state.project.description, projectType: state.project.type, + project: state.project.projects[0], message: state.message });