From a90c2879892650c95d6f2e9b072a5c41a9e28747 Mon Sep 17 00:00:00 2001 From: Torjus Date: Mon, 30 Jul 2018 11:19:36 +0200 Subject: [PATCH] Add notification while creating torrent file --- app/lib/components/Chat/FileChatEntry.jsx | 4 ++-- app/lib/components/Chat/FileSharing.jsx | 23 ++++++++++++++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/app/lib/components/Chat/FileChatEntry.jsx b/app/lib/components/Chat/FileChatEntry.jsx index b36cf72..b0712ac 100644 --- a/app/lib/components/Chat/FileChatEntry.jsx +++ b/app/lib/components/Chat/FileChatEntry.jsx @@ -1,6 +1,6 @@ import React, { Component, Fragment } from 'react'; import { connect } from 'react-redux'; -import { notifyAction } from '../../redux/stateActions'; +import * as requestActions from '../../redux/requestActions'; import { saveAs } from 'file-saver/FileSaver'; import { client } from './FileSharing'; @@ -130,7 +130,7 @@ class FileChatEntry extends Component } const mapDispatchToProps = { - notify : notifyAction + notify : requestActions.notify }; export default connect( diff --git a/app/lib/components/Chat/FileSharing.jsx b/app/lib/components/Chat/FileSharing.jsx index 339b346..fe801a2 100644 --- a/app/lib/components/Chat/FileSharing.jsx +++ b/app/lib/components/Chat/FileSharing.jsx @@ -2,6 +2,7 @@ import React, { Component } from 'react'; import WebTorrent from 'webtorrent'; import createTorrent from 'create-torrent'; import dragDrop from 'drag-drop'; +import randomString from 'random-string'; import * as stateActions from '../../redux/stateActions'; import * as requestActions from '../../redux/requestActions'; import { store } from '../../store'; @@ -25,12 +26,22 @@ const notifyPeers = (file) => 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) => { if (err) { - console.error('Error creating torrent', err); - return; + return store.dispatch(requestActions.notify({ + text: 'An error occured while uploading a file' + })); } const existingTorrent = client.get(torrent); @@ -41,9 +52,15 @@ const shareFiles = async (files) => magnet: existingTorrent.magnetURI }); } - + client.seed(files, (newTorrent) => { + store.dispatch(stateActions.removeNotification(notification.id)); + + store.dispatch(requestActions.notify({ + text: 'Torrent successfully created' + })) + notifyPeers({ magnet : newTorrent.magnetURI });