import React from 'react' import { connect } from 'react-redux' import { bindActionCreators } from 'redux' import Modal from 'react-modal' import Admin from './../admin' import { fetchEntity, uploadReplay } from '../actions/index' import '../../app/assets/javascripts/teachable/chat'; import { withExitPrompt } from './util/with-exit-prompt' const UploadModalStyles = { overlay: { background: 'rgba(0, 0, 0, 0.50)' }, content: { background: 'white', fontFamily: 'TeachableSans-Bold', fontSize: 14, height: 132, margin: 'auto', maxWidth: 500, padding: 20, textAlign: 'center' } }; interface WebinarChatProps { downloads: any[]; fetchEntity: any; webinar?: any; uploadReplay: any; } interface DownloadSwitches {} interface WebinarChatState extends ComponentState { clearChatModalOpen?: boolean; downloads: any[]; downloadSwitches?: DownloadSwitches; firebaseReference?: any; hangoutStatus: string; replayVideoStatus: string; savedYouTubeID: string; showUpgradeButton?: boolean; tab: string; uploadModalOpen: boolean; webinar: any; youTubeID: string; } class WebinarChat extends React.Component { constructor(props) { super(props); this.state = { fetching: true, errors: [], tab: 'All', webinar: { chatTimeString: '' }, hangoutStatus: 'Starting', downloads: [], youTubeID: '', savedYouTubeID: '', replayVideoStatus: 'unclicked', uploadModalOpen: false, clearChatModalOpen: false }; } componentDidMount() { window.Chat.initialize('admin'); const downloadSwitches = {}; for (let i = 0; i < 8; i++) { downloadSwitches[i] = false; } $('.match-height-layout').matchHeight(); let pathDirectories = window.location.pathname.split('/'); const webinarPromise = this.props.fetchEntity({ id: pathDirectories[pathDirectories.length - 2], directory: 'webinars', entityName: 'webinar' }) Promise.all([ webinarPromise, window.Chat.authPromise ]).then(() => { this.setState({ fetching: false, webinar: this.props.webinar, downloads: this.props.downloads, downloadSwitches, changesToSave: false, firebaseReference: window.firebase.database().ref().child(this.props.webinar.id) }, () => { this.state.firebaseReference.on('value', this.firebaseUpdate.bind(this), (e) => { window.firebase.auth().currentUser.getIdTokenResult().then(t => console.log(t.claims)) console.log(e) }); }); }); } checkForChanges() { return (this.state.youTubeID !== this.state.savedYouTubeID); } firebaseUpdate(snap) { var value = snap.val(); window.Chat.updateChatSection(snap.val()); var downloadSwitches = { 0: value.controls.downloadSwitches[0], 1: value.controls.downloadSwitches[1], 2: value.controls.downloadSwitches[2], 3: value.controls.downloadSwitches[3], 4: value.controls.downloadSwitches[4], 5: value.controls.downloadSwitches[5], 6: value.controls.downloadSwitches[6], 7: value.controls.downloadSwitches[7] }; this.setState({ fetching: false, showUpgradeButton: value.controls.showUpgradeButton, hangoutStatus: value.controls.hangoutStatus, downloadSwitches: downloadSwitches, youTubeID: value.controls.youTubeID, savedYouTubeID: value.controls.youTubeID, replayVideoStatus: value.controls.replayVideoStatus }); } changeYouTubeID(e) { this.setState({ youTubeID: e.target.value }, () => { this.setState({ changesToSave: this.checkForChanges() }); }); } clickSaveYouTubeID() { if (this.state.changesToSave) { if (this.state.youTubeID.indexOf("&") > -1) { alert('The & symbol is not allowed in the YouTube ID!'); } else { this.state.firebaseReference.child('controls').update({ "youTubeID": this.state.youTubeID }); this.setState({ savedYouTubeID: this.state.youTubeID }, () => { this.setState({ changesToSave: this.checkForChanges() }); }); } } } clickStatus(e) { var status = e.target.innerHTML; var obj = { 'hangoutStatus': status }; if (status !== 'End') { obj["replayVideoStatus"] = 'unclicked'; } this.state.firebaseReference.child('controls').update(obj); } clickClearChat() { this.setState({ clearChatModalOpen: true }); } confirmClearChat() { this.state.firebaseReference.child("messages").remove(); this.state.firebaseReference.child("users").remove(); window.location.reload(); } changeShowUpgrade(e) { this.state.firebaseReference.child('controls').update({ "showUpgradeButton": e.target.checked }); } changeDownloadSwitch(e) { this.state.firebaseReference.child('controls').child('downloadSwitches').update({ [e.target.dataset.index]: e.target.checked }); } clickTab(e) { var words = e.target.innerHTML.split(' '); this.setState({ tab: words.slice(0, words.length - 1).join(' ') }, function() { window.Common.scrollOverflowElementToBottom($('.chat-messages:not(.hidden)')[0]); }); } clickUploadReplay() { if (!this.state.changesToSave && this.state.youTubeID.trim() !== "") { this.setState({ uploadModalOpen: true }); } } confirmUploadReplay() { this.setState({ uploadModalOpen: false, fetching: true }); this.props.uploadReplay({ webinarId: this.state.webinar.id, youTubeID: this.state.youTubeID }).then(() => { this.setState({ fetching: false }); }); } closeModal() { this.setState({ uploadModalOpen: false, clearChatModalOpen: false }); } render() { return(
{ Admin.renderSpinner(this.state.fetching) } { Admin.renderGrayedOut(this.state.fetching) }
  • { "All (" + $('.chat-messages.all .chat-message').length + ")" }
  • { "Questions (" + $('.chat-messages.questions .chat-message').length + ")" }
  • { "General comments (" + $('.chat-messages.comments .chat-message').length + ")" }
  • { "Hello (" + $('.chat-messages.hello .chat-message').length + ")" }
  • { "Blocked (" + $('.chat-messages.blocked .chat-message').length + ")" }
  • Clear Chat

{ this.state.webinar.chatTimeString.split('@')[0] }

@{ this.state.webinar.chatTimeString.split('@')[1] }

{ this.state.webinar.platform === "vimeo" ? "Vimeo" : "YouTube" } video ID

Save

Hangout status

  • Starting
  • Go live
  • End

{ this.renderReplaySection() }

CTA live?


{ this.state.downloads.map(function(download, index) { return(

{ download.name }

); }.bind(this)) }
Do you want to upload the replay video now?
Upload
Cancel
Are you sure you want to clear the chat?
Yes
Cancel
) } renderReplaySection() { var replayText = { "unclicked": "The replay has not been uploaded", "scheduled": "The replay video upload has been scheduled", "uploaded": "The replay video has been uploaded", "error": "" }; if (this.state.hangoutStatus === 'End') { return(

Replay video

{ this.renderReplayUploadButton() }
{ replayText[this.state.replayVideoStatus] }

) } else { return null; } } renderReplayUploadButton() { if (this.state.replayVideoStatus === 'unclicked') { return(
Click to upload replay video
Upload replay video
) } else if (this.state.replayVideoStatus === 'error') { return(
There was an error uploading the video
Try again
) } else { return null; } } } const mapStateToProps = (reducers, props) => { return reducers.standardReducer; }; function mapDispatchToProps(dispatch) { return bindActionCreators({ fetchEntity, uploadReplay }, dispatch); } export default connect(mapStateToProps, mapDispatchToProps)(withExitPrompt(WebinarChat));