Add notification while creating torrent file

master
Torjus 2018-07-30 11:19:36 +02:00
parent f912d82002
commit a90c287989
2 changed files with 22 additions and 5 deletions

View File

@ -1,6 +1,6 @@
import React, { Component, Fragment } from 'react'; import React, { Component, Fragment } from 'react';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { notifyAction } from '../../redux/stateActions'; import * as requestActions from '../../redux/requestActions';
import { saveAs } from 'file-saver/FileSaver'; import { saveAs } from 'file-saver/FileSaver';
import { client } from './FileSharing'; import { client } from './FileSharing';
@ -130,7 +130,7 @@ class FileChatEntry extends Component
} }
const mapDispatchToProps = { const mapDispatchToProps = {
notify : notifyAction notify : requestActions.notify
}; };
export default connect( export default connect(

View File

@ -2,6 +2,7 @@ import React, { Component } from 'react';
import WebTorrent from 'webtorrent'; import WebTorrent from 'webtorrent';
import createTorrent from 'create-torrent'; import createTorrent from 'create-torrent';
import dragDrop from 'drag-drop'; import dragDrop from 'drag-drop';
import randomString from 'random-string';
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'; import { store } from '../../store';
@ -25,12 +26,22 @@ const notifyPeers = (file) =>
const shareFiles = async (files) => const shareFiles = async (files) =>
{ {
const notification =
{
id: randomString({ length: 6 }).toLowerCase(),
text: 'Creating torrent',
type: 'info'
};
store.dispatch(stateActions.addNotification(notification));
createTorrent(files, (err, torrent) => createTorrent(files, (err, torrent) =>
{ {
if (err) if (err)
{ {
console.error('Error creating torrent', err); return store.dispatch(requestActions.notify({
return; text: 'An error occured while uploading a file'
}));
} }
const existingTorrent = client.get(torrent); const existingTorrent = client.get(torrent);
@ -41,9 +52,15 @@ const shareFiles = async (files) =>
magnet: existingTorrent.magnetURI magnet: existingTorrent.magnetURI
}); });
} }
client.seed(files, (newTorrent) => client.seed(files, (newTorrent) =>
{ {
store.dispatch(stateActions.removeNotification(notification.id));
store.dispatch(requestActions.notify({
text: 'Torrent successfully created'
}))
notifyPeers({ notifyPeers({
magnet : newTorrent.magnetURI magnet : newTorrent.magnetURI
}); });