Add file select input in addition to drag n drop

master
Torjus 2018-07-27 10:47:49 +02:00
parent 8129cc0753
commit 734bb1eb0a
6 changed files with 87 additions and 98 deletions

View File

@ -5,17 +5,9 @@ import * as stateActions from '../../redux/stateActions';
import * as requestActions from '../../redux/requestActions'; import * as requestActions from '../../redux/requestActions';
import MessageList from './MessageList'; import MessageList from './MessageList';
import FileSharing from './FileSharing'; import FileSharing from './FileSharing';
import WebTorrent from 'webtorrent';
class Chat extends Component class Chat extends Component
{ {
constructor(props)
{
super(props);
this.client = new WebTorrent();
}
render() render()
{ {
const { const {
@ -29,9 +21,9 @@ class Chat extends Component
return ( return (
<div data-component='Chat'> <div data-component='Chat'>
<MessageList client={this.client} /> <MessageList />
<FileSharing client={this.client} /> <FileSharing />
<form <form
data-component='Sender' data-component='Sender'

View File

@ -1,6 +1,6 @@
import React, { Component, Fragment } from 'react'; import React, { Component, Fragment } from 'react';
import WebTorrent from 'webtorrent';
import { saveAs } from 'file-saver/FileSaver'; import { saveAs } from 'file-saver/FileSaver';
import { client } from './FileSharing';
const saveFile = (file) => const saveFile = (file) =>
{ {
@ -32,7 +32,7 @@ class FileChatEntry extends Component
active: true active: true
}); });
this.props.client.add(this.props.message.file.magnet, (torrent) => client.add(this.props.message.file.magnet, (torrent) =>
{ {
const onProgress = () => const onProgress = () =>
{ {

View File

@ -4,55 +4,48 @@ import WebTorrent from 'webtorrent';
import dragDrop from 'drag-drop'; import dragDrop from 'drag-drop';
import * as stateActions from '../../redux/stateActions'; import * as stateActions from '../../redux/stateActions';
import * as requestActions from '../../redux/requestActions'; import * as requestActions from '../../redux/requestActions';
import { store } from '../../store';
class FileSharing extends Component { export const client = new WebTorrent();
notifyPeers = (file) =>
const notifyPeers = (file) =>
{ {
this.props.notifyPeers( const { displayName, picture } = store.getState().me;
file, store.dispatch(stateActions.addUserFile(file));
this.props.displayName, store.dispatch(requestActions.sendChatFile(file, displayName, picture));
this.props.picture
);
}; };
componentDidMount() const shareFiles = (files) =>
{ {
dragDrop('body', (files) => client.seed(files, (torrent) => {
{ notifyPeers({
this.props.client.seed(files, (torrent) => {
this.notifyPeers({
magnet: torrent.magnetURI magnet: torrent.magnetURI
}); });
}); });
}); };
dragDrop('body', shareFiles);
class FileSharing extends Component {
constructor(props)
{
super(props);
} }
handleFileChange = (event) =>
{
if (event.target.files.length > 0)
{
shareFiles(event.target.files);
}
};
render() render()
{ {
return ( return (
<div> <input type="file" onChange={this.handleFileChange} />
drag & drop files to share them!!!
</div>
); );
} }
} }
const mapStateToProps = (state) => export default FileSharing;
({
displayName: state.me.displayName,
picture: state.me.picture
});
const mapDispatchToProps = (dispatch) =>
({
notifyPeers: (file, displayName, picture) =>
{
dispatch(stateActions.addUserFile(file));
dispatch(requestActions.sendChatFile(file, displayName, picture));
}
});
export default connect(
mapStateToProps,
mapDispatchToProps
)(FileSharing);

View File

@ -74,7 +74,7 @@ class MessageList extends Component
)} )}
{message.type === 'file' && ( {message.type === 'file' && (
<FileChatEntry message={message} client={this.props.client} /> <FileChatEntry message={message} />
)} )}
</div> </div>

View File

@ -3,13 +3,6 @@ import UrlParse from 'url-parse';
import React from 'react'; import React from 'react';
import { render } from 'react-dom'; import { render } from 'react-dom';
import { Provider } from 'react-redux'; import { Provider } from 'react-redux';
import {
applyMiddleware as applyReduxMiddleware,
createStore as createReduxStore,
compose as composeRedux
} from 'redux';
import thunk from 'redux-thunk';
import { createLogger as createReduxLogger } from 'redux-logger';
import { getDeviceInfo } from 'mediasoup-client'; import { getDeviceInfo } from 'mediasoup-client';
import randomString from 'random-string'; import randomString from 'random-string';
import Logger from './Logger'; import Logger from './Logger';
@ -17,48 +10,11 @@ import * as utils from './utils';
import * as cookiesManager from './cookiesManager'; import * as cookiesManager from './cookiesManager';
import * as requestActions from './redux/requestActions'; import * as requestActions from './redux/requestActions';
import * as stateActions from './redux/stateActions'; import * as stateActions from './redux/stateActions';
import reducers from './redux/reducers';
import roomClientMiddleware from './redux/roomClientMiddleware';
import Room from './components/Room'; import Room from './components/Room';
import { loginEnabled } from '../config'; import { loginEnabled } from '../config';
import { store } from './store';
const logger = new Logger(); const logger = new Logger();
const reduxMiddlewares =
[
thunk,
roomClientMiddleware
];
if (process.env.NODE_ENV === 'development')
{
const reduxLogger = createReduxLogger(
{
duration : true,
timestamp : false,
level : 'log',
logErrors : true
});
reduxMiddlewares.push(reduxLogger);
}
const composeEnhancers =
typeof window === 'object' &&
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ?
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
// Specify extensions options like name, actionsBlacklist, actionsCreators, serialize...
}) : composeRedux;
const enhancer = composeEnhancers(
applyReduxMiddleware(...reduxMiddlewares)
// other store enhancers if any
);
const store = createReduxStore(
reducers,
undefined,
enhancer
);
domready(() => domready(() =>
{ {

48
app/lib/store.js 100644
View File

@ -0,0 +1,48 @@
import {
applyMiddleware,
createStore,
compose
} from 'redux';
import thunk from 'redux-thunk';
import { createLogger } from 'redux-logger';
import reducers from './redux/reducers';
import roomClientMiddleware from './redux/roomClientMiddleware';
const reduxMiddlewares =
[
thunk,
roomClientMiddleware
];
if (process.env.NODE_ENV === 'development')
{
const reduxLogger = createLogger(
{
duration : true,
timestamp : false,
level : 'log',
logErrors : true
});
reduxMiddlewares.push(reduxLogger);
}
const composeEnhancers =
typeof window === 'object' &&
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ?
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
// Specify extensions options like name, actionsBlacklist, actionsCreators, serialize...
}) : compose;
const enhancer = composeEnhancers(
applyMiddleware(...reduxMiddlewares)
// other store enhancers if any
);
export const store = createStore(
reducers,
undefined,
enhancer
);
console.log(store);