import React from "react"; import {connect} from "react-redux" import { editFile } from "../../actions/files"; import axios from "../../axiosInterceptor" import env from "../../enviroment/envFrontEnd"; import capitalize from "../../utils/capitalize"; import bytes from "bytes"; import copy from "copy-text-to-clipboard"; import { setShareSelected, editSelectedItem, editShareSelected } from "../../actions/selectedItem"; import SpinnerImage from "../SpinnerImage"; import SpinnerPage from "../SpinnerPage"; import Swal from "sweetalert2"; const currentURL = env.url; class ShareModelContainer extends React.Component { constructor(props) { super(props); this.state= { shareableLink: "Fetching Link...", loaded: false, sendTo: "", emailSent: false, copySelected: false, public: false, shared: false, } this.lastLoadedID = '' } componentDidMount = () => { const id = this.props.shareSelected._id; //`/file-service/info/${id}` const url = this.props.shareSelected.metadata.drive ? `/file-service-google/info/${id}` : `/file-service/info/${id}`; axios.get(url).then((response) => { console.log("file info response", response.data); const linkType = response.data.metadata.linkType; const shareURL = this.props.shareSelected.metadata.drive ? response.data.metadata.link : `${currentURL}/download-page/${this.props.shareSelected._id}/${this.props.shareSelected.metadata.link}` console.log("share link type", linkType, response.data); this.setState(() => ({ ...this.state, shared: linkType ? true : false, loaded: true, shareableLink: shareURL })) }).catch((e) => { console.log("Cannot get share file data", e); }) } componentDidUpdate = () => { // console.log("share model updated"); // if (this.lastLoadedID !== this.props.shareSelected._id && this.props.shareSelected !== "") { // if (this.props.shareSelected.metadata.link && !this.props.shareSelected.metadata.drive) { // console.log("link already exists"); // this.lastLoadedID = this.props.shareSelected._id; // const shareURL = this.props.shareSelected.metadata.drive ? results.data : `${currentURL}/download-page/${this.props.shareSelected._id}/${this.props.shareSelected.metadata.link}` // return this.setState(() => { // return { // ...this.state, // shareableLink: shareURL, // loaded: true, // public: true // } // }) // } // this.lastLoadedID = this.props.shareSelected._id; // console.log("share props", this.props.shareSelected) // const url = this.props.shareSelected.metadata.drive ? currentURL +`/file-service-google/make-public/${this.props.shareSelected._id}` : currentURL +`/file-service/make-public/${this.props.shareSelected._id}`; // console.log("share", url) // axios.patch(url, undefined).then((results) => { // this.props.shareSelected.metadata.link = results.data; // this.props.dispatch(editFile(this.props.shareSelected._id,{"metadata": { // ...this.props.shareSelected.metadata, // link: results.data, // linkType: "public" // }})); // const shareURL = this.props.shareSelected.metadata.drive ? results.data : `${currentURL}/download-page/${this.props.shareSelected._id}/${this.props.shareSelected.metadata.link}` // this.setState(() => { // return { // ...this.state, // shareableLink: shareURL, // loaded: true, // public: true // } // }) // }) // } } sendEmail = (e) => { e.preventDefault() if (this.state.sendTo.length === 0 || !this.state.loaded) return; if (!this.state.shared) { Swal.fire({ icon: 'error', title: 'You must share the file first', text: 'Select make public or make one time before sending email', }) return; } console.log("sending email share request") const data = { file: { _id: this.props.shareSelected._id, resp: this.state.sendTo } } axios.post("/file-service/send-share-email", data).then((response) => { console.log("sent email share"); this.setState(() => { return { ...this.state, emailSent: true } }) }).catch((err) => { console.log("share file email failed", err) Swal.fire({ icon: 'error', title: 'Cannot send share email', text: 'An error occurred when sending share email', }) }) } onChangeEmail = (e) => { const value = e.target.value; this.setState(() => { return { ...this.state, sendTo: value } }) } closeShareModel = () => { this.props.dispatch(setShareSelected("")) this.setState(() => { return { shareableLink: "Fetching Link...", loaded: false, sendTo: "", emailSent: false, copySelected: false, public: false } }) this.lastLoadedID = '' } copyClick = () => { if (!this.state.loaded) return; copy(this.state.shareableLink) console.log("link copied") this.setState(() => { return { ...this.state, copySelected: true } }, () => { console.log("new state set") window.setTimeout(() => { this.setState(() => { return { ...this.state, copySelected: false } }) }, 750) }) } removePublicLink = async() => { const url = this.props.shareSelected.metadata.drive ? `/file-service-google/remove-link/${this.props.shareSelected._id}` : `/file-service/remove-link/${this.props.shareSelected._id}` axios.delete(url, { }).then(() => { this.props.dispatch(editFile(this.props.shareSelected._id,{"metadata": { ...this.props.shareSelected.metadata, link: undefined, linkType: undefined }})) this.props.dispatch(editSelectedItem({link: undefined, linkType: undefined})) this.props.dispatch(editShareSelected({"metadata": { ...this.props.shareSelected.metadata, link: undefined, linkType: undefined }})) this.setState(() => ({ ...this.state, shared: false, })) //this.closeShareModel(); }).catch((err) => { console.log(err) Swal.fire({ icon: 'error', title: 'Cannot remove public link', text: 'An error occurred when removing public link', }) }) } makePublic = () => { this.showingSwal = true; Swal.fire({ title: 'Are you sure?', text: "Making this public, will allow anyone to have access to it", icon: 'warning', showCancelButton: true, confirmButtonColor: '#3085d6', cancelButtonColor: '#d33', confirmButtonText: 'Yes, make public' }).then((result) => { this.showingSwal = false; if (result.value) { const url = this.props.shareSelected.metadata.drive ? `/file-service-google/make-public/${this.props.shareSelected._id}` : `/file-service/make-public/${this.props.shareSelected._id}`; axios.patch(url, undefined).then((results) => { this.props.dispatch(editFile(this.props.shareSelected._id,{"metadata": { ...this.props.shareSelected.metadata, link: results.data, linkType: "public" }})) this.props.dispatch(editSelectedItem({link: results.data, linkType: "public"})) this.props.dispatch(editShareSelected({"metadata": { ...this.props.shareSelected.metadata, link: results.data, linkType: "public" }})) const shareURL = this.props.shareSelected.metadata.drive ? results.data : `${currentURL}/download-page/${this.props.shareSelected._id}/${results.data}` this.setState(() => ({ ...this.state, shared: true, shareableLink: shareURL })) }).catch((err) => { console.log(err) Swal.fire({ icon: 'error', title: 'Cannot make public', text: 'An error occurred when making file public' }) }) } }) } makeOne = () => { this.showingSwal = true; Swal.fire({ title: 'Are you sure?', text: "One time link, will allow anyone to access this file once", icon: 'warning', showCancelButton: true, confirmButtonColor: '#3085d6', cancelButtonColor: '#d33', confirmButtonText: 'Yes, create link' }).then((result) => { this.showingSwal = false; if (result.value) { axios.patch(`/file-service/make-one/${this.props.shareSelected._id}`, undefined).then((results) => { this.props.dispatch(editFile(this.props.shareSelected._id,{"metadata": { ...this.props.shareSelected.metadata, link: results.data, linkType: "one" }})) this.props.dispatch(editSelectedItem({link: results.data, linkType: "one"})) this.props.dispatch(editShareSelected({"metadata": { ...this.props.shareSelected.metadata, link: results.data, linkType: "one" }})) this.setState(() => ({ ...this.state, shared: true, })) const shareURL = this.props.shareSelected.metadata.drive ? results.data : `${currentURL}/download-page/${this.props.shareSelected._id}/${results.data}` this.setState(() => ({ ...this.state, shared: true, shareableLink: shareURL })) }).catch((err) => { console.log(err) Swal.fire({ icon: 'error', title: 'Cannot make one time public link', text: 'An error occurred when making one time public link', }) }) } }) } removeLink = () => { const url = this.props.shareSelected.metadata.drive ? `/file-service-google/remove-link/${this.props.shareSelected._id}` : `/file-service/remove-link/${this.props.shareSelected._id}` axios.delete(url, { }).then(() => { this.props.dispatch(editFile(this.props.shareSelected._id,{"metadata": { ...this.props.shareSelected.metadata, link: undefined, linkType: undefined }})) this.props.dispatch(editSelectedItem({link: undefined, linkType: undefined})) this.props.dispatch(editShareSelected({"metadata": { ...this.props.shareSelected.metadata, link: undefined, linkType: undefined }})) }).catch((err) => { console.log(err) Swal.fire({ icon: 'error', title: 'Cannot remove public link', text: 'An error occurred when removing public link', }) }) } render() { return ( ); } } const connectStoreToProp = (state) => ({ shareSelected: state.selectedItem.shareSelected }) export default connect(connectStoreToProp)(ShareModelContainer);