diff --git a/client/source/scripts/actions/ClientActions.js b/client/source/scripts/actions/ClientActions.js index b08ae13f..dc98def0 100644 --- a/client/source/scripts/actions/ClientActions.js +++ b/client/source/scripts/actions/ClientActions.js @@ -1,137 +1,31 @@ import axios from 'axios'; -export function addTorrents(urls, destination) { - return function(dispatch) { - return axios.post('/client/add', { - urls, - destination - }) - .then((json = {}) => { - return json.data; - }) - .then((response) => { - dispatch({ - type: 'ADD_TORRENT', - payload: { - response - } - }); - }) - .catch((error) => { - console.error('error', error); - }); - } -}; +import AppDispatcher from '../dispatcher/AppDispatcher'; +import ActionTypes from '../constants/ActionTypes'; -export function getTransferData() { - return function(dispatch) { +const ClientActions = { + fetchTransferData: function() { return axios.get('/client/stats') .then((json = {}) => { return json.data; }) - .then(transferData => { - dispatch({ - type: 'CLIENT_RECEIVE_TRANSFER_DATA', - payload: transferData - }); - }) - .catch((error) => { - console.error('error', error); - }); - } -} - -export function getTorrents() { - return function(dispatch) { - dispatch({ - type: 'REQUEST_TORRENTS', - payload: { - text: 'Begin requesting torrents.' - } - }); - return axios.get('/client/list') - .then((json = {}) => { - return json.data; - }) - .then(torrents => { - dispatch({ - type: 'RECEIVE_TORRENTS', - payload: { - torrents + .then((transferData) => { + AppDispatcher.dispatchServerAction({ + type: ActionTypes.CLIENT_FETCH_TRANSFER_DATA_SUCCESS, + data: { + transferData } }); }) .catch((error) => { - console.error(error); - }); - } -} - -export function getTorrentDetails(hash) { - return function(dispatch) { - return axios.post('/client/torrent-details', { - hash - }) - .then((json = {}) => { - return json.data; - }) - .then(torrentDetails => { - dispatch({ - type: 'RECEIVE_TORRENT_DETAILS', - payload: { - hash, - torrentDetails + AppDispatcher.dispatchServerAction({ + type: ActionTypes.CLIENT_FETCH_TRANSFER_DATA_ERROR, + data: { + error } }); - }) - .catch((error) => { - console.error(error); }); } -} +}; -export function startTorrent(hashes) { - return function(dispatch) { - return axios.post('/client/start', { - hashes - }) - .then((json = {}) => { - return json.data; - }) - .then((response) => { - dispatch({ - type: 'START_TORRENT', - payload: { - response - } - }); - dispatch(getTorrents()); - }) - .catch((error) => { - console.error(error); - }); - } -} - -export function stopTorrent(hashes) { - return function(dispatch) { - return axios.post('/client/stop', { - hashes - }) - .then((json = {}) => { - return json.data; - }) - .then((response) => { - dispatch({ - type: 'STOP_TORRENT', - payload: { - response - } - }); - dispatch(getTorrents()); - }) - .catch((error) => { - console.error(error); - }); - } -} +export default ClientActions; diff --git a/client/source/scripts/actions/TorrentActions.js b/client/source/scripts/actions/TorrentActions.js new file mode 100644 index 00000000..0797b1e7 --- /dev/null +++ b/client/source/scripts/actions/TorrentActions.js @@ -0,0 +1,133 @@ +import axios from 'axios'; + +import AppDispatcher from '../dispatcher/AppDispatcher'; +import ActionTypes from '../constants/ActionTypes'; + +const TorrentActions = { + addTorrents: function(urls, destination) { + return axios.post('/client/add', { + urls, + destination + }) + .then((json = {}) => { + return json.data; + }) + .then((response) => { + AppDispatcher.dispatchServerAction({ + type: ActionTypes.CLIENT_ADD_TORRENT_SUCCESS, + data: { + response + } + }); + }) + .catch((error) => { + AppDispatcher.dispatchServerAction({ + type: ActionTypes.CLIENT_ADD_TORRENT_ERROR, + data: { + error + } + }); + }); + }, + + fetchTorrents: function () { + return axios.get('/client/list') + .then((json = {}) => { + return json.data; + }) + .then((torrents) => { + AppDispatcher.dispatchServerAction({ + type: ActionTypes.CLIENT_FETCH_TORRENTS_SUCCESS, + data: { + torrents + } + }); + }) + .catch((error) => { + AppDispatcher.dispatchServerAction({ + type: ActionTypes.CLIENT_FETCH_TORRENTS_ERROR, + data: { + error + } + }); + }); + }, + + fetchTorrentDetails: function(hash) { + return axios.post('/client/torrent-details', { + hash + }) + .then((json = {}) => { + return json.data; + }) + .then((torrentDetails) => { + AppDispatcher.dispatchServerAction({ + type: ActionTypes.CLIENT_FETCH_TORRENT_DETAILS_SUCCESS, + data: { + hash, + torrentDetails + } + }); + }) + .catch((error) => { + AppDispatcher.dispatchServerAction({ + type: ActionTypes.CLIENT_FETCH_TORRENT_DETAILS_ERROR, + data: { + hash + } + }); + }); + }, + + startTorrents: function(hashes) { + return axios.post('/client/start', { + hashes + }) + .then((json = {}) => { + return json.data; + }) + .then((response) => { + AppDispatcher.dispatchServerAction({ + type: ActionTypes.CLIENT_START_TORRENT_SUCCESS, + data: { + response + } + }); + }) + .catch((error) => { + AppDispatcher.dispatchServerAction({ + type: ActionTypes.CLIENT_START_TORRENT_ERROR, + data: { + error + } + }); + }); + }, + + stopTorrents: function(hashes) { + return axios.post('/client/stop', { + hashes + }) + .then((json = {}) => { + return json.data; + }) + .then((response) => { + AppDispatcher.dispatchServerAction({ + type: ActionTypes.CLIENT_STOP_TORRENT_SUCCESS, + data: { + response + } + }); + }) + .catch((error) => { + AppDispatcher.dispatchServerAction({ + type: ActionTypes.CLIENT_STOP_TORRENT_ERROR, + data: { + error + } + }); + }); + } +}; + +export default TorrentActions; diff --git a/client/source/scripts/actions/UIActions.js b/client/source/scripts/actions/UIActions.js index 95ecbcd6..349c2dc9 100644 --- a/client/source/scripts/actions/UIActions.js +++ b/client/source/scripts/actions/UIActions.js @@ -1,43 +1,55 @@ -export function displayModal(payload) { - return { - type: 'UI_DISPLAY_MODAL', - payload - }; -} +import AppDispatcher from '../dispatcher/AppDispatcher'; +import ActionTypes from '../constants/ActionTypes'; -export function dismissModal(payload) { - return { - type: 'UI_DISPLAY_MODAL', - payload: { - modal: null - } - }; -} +const UIActions = { + displayModal: function(data) { + AppDispatcher.dispatchUIAction({ + type: ActionTypes.UI_DISPLAY_MODAL, + data + }); + }, -export function handleTorrentClick(payload) { - return { - type: 'CLICK_TORRENT', - payload - }; -} + dismissModal: function(data) { + AppDispatcher.dispatchUIAction({ + type: ActionTypes.UI_DISPLAY_MODAL, + data: null + }); + }, -export function setTorrentsFilter(payload) { - return { - type: 'UI_FILTER_TORRENTS', - payload - }; -} + handleDetailsClick: function(data) { + AppDispatcher.dispatchUIAction({ + type: ActionTypes.UI_CLICK_TORRENT_DETAILS, + data + }); + }, -export function setTorrentsSearch(payload) { - return { - type: 'UI_SEARCH_TORRENTS', - payload - }; -} + handleTorrentClick: function(data) { + AppDispatcher.dispatchUIAction({ + type: ActionTypes.UI_CLICK_TORRENT, + data + }); + }, -export function setTorrentsSort(payload) { - return { - type: 'UI_SORT_TORRENTS', - payload - }; -} + setTorrentStatusFilter: function(data) { + AppDispatcher.dispatchUIAction({ + type: ActionTypes.UI_SET_TORRENT_STATUS_FILTER, + data + }); + }, + + setTorrentsSearchFilter: function(data) { + AppDispatcher.dispatchUIAction({ + type: ActionTypes.UI_SET_TORRENT_SEARCH_FILTER, + data + }); + }, + + setTorrentsSort: function(data) { + AppDispatcher.dispatchUIAction({ + type: ActionTypes.UI_SET_TORRENT_SORT, + data + }); + } +}; + +export default UIActions; diff --git a/client/source/scripts/app.js b/client/source/scripts/app.js index 4508d76f..306c1d08 100644 --- a/client/source/scripts/app.js +++ b/client/source/scripts/app.js @@ -1,18 +1,6 @@ -import {createStore, applyMiddleware} from 'redux'; -import {Provider} from 'react-redux'; import React from 'react'; import ReactDOM from 'react-dom'; -import thunkMiddleware from 'redux-thunk'; import App from './containers/App'; -import rootReducer from './reducers/rootReducer'; -const createStoreWithMiddleware = applyMiddleware(thunkMiddleware)(createStore); - -let store = createStoreWithMiddleware(rootReducer); - -ReactDOM.render( - - - , document.getElementById('app') -); +ReactDOM.render(, document.getElementById('app')); diff --git a/client/source/scripts/containers/ActionBar.js b/client/source/scripts/components/action-bar/ActionBar.js similarity index 55% rename from client/source/scripts/containers/ActionBar.js rename to client/source/scripts/components/action-bar/ActionBar.js index fa948861..f3a42843 100644 --- a/client/source/scripts/containers/ActionBar.js +++ b/client/source/scripts/components/action-bar/ActionBar.js @@ -1,27 +1,27 @@ -import {connect} from 'react-redux'; import React from 'react'; -import Action from '../components/action-bar/Action'; -import {addTorrent, startTorrent, stopTorrent} from '../actions/ClientActions'; -import {displayModal} from '../actions/UIActions'; -import {setTorrentsSort} from '../actions/UIActions'; -import SortDropdown from '../components/action-bar/SortDropdown'; -import uiSelector from '../selectors/uiSelector'; +import Action from './Action'; +import EventTypes from '../../constants/EventTypes'; +import SortDropdown from './SortDropdown'; +import TorrentActions from '../../actions/TorrentActions'; +import TorrentFilterStore from '../../stores/TorrentFilterStore'; +import TorrentStore from '../../stores/TorrentStore'; +import UIActions from '../../actions/UIActions'; const methodsToBind = [ 'handleAddTorrents', 'handleSortChange', 'handleStart', - 'handleStop' + 'handleStop', + 'onSortChange' ]; -class ActionBar extends React.Component { - +export default class ActionBar extends React.Component { constructor() { super(); this.state = { - selectedTorrents: [] + sortBy: TorrentFilterStore.getTorrentsSort() }; methodsToBind.forEach((method) => { @@ -29,24 +29,34 @@ class ActionBar extends React.Component { }); } + componentDidMount() { + TorrentFilterStore.listen(EventTypes.UI_TORRENTS_SORT_CHANGE, this.onSortChange); + } + + componentWillUnmount() { + TorrentFilterStore.unlisten(EventTypes.UI_TORRENTS_SORT_CHANGE, this.onSortChange); + } + handleAddTorrents() { - this.props.dispatch(displayModal({ - modal: 'add-torrents' - })); + UIActions.displayModal('add-torrents'); } handleSortChange(sortBy) { - this.props.dispatch(setTorrentsSort({ - sortBy - })); + UIActions.setTorrentsSort(sortBy); } handleStart() { - this.props.dispatch(startTorrent(this.props.torrentList.selected)); + TorrentActions.startTorrents(TorrentStore.getSelectedTorrents()); } handleStop() { - this.props.dispatch(stopTorrent(this.props.torrentList.selected)); + TorrentActions.stopTorrents(TorrentStore.getSelectedTorrents()); + } + + onSortChange() { + this.setState({ + sortBy: TorrentFilterStore.getTorrentsSort() + }); } render() { @@ -54,7 +64,7 @@ class ActionBar extends React.Component {