From 9cc37e6918fcbf53744612704ab53bce7290c7fd Mon Sep 17 00:00:00 2001 From: Carlos Tadeu Panato Junior Date: Mon, 23 Jan 2017 15:23:14 +0100 Subject: Move instances of Client.uploadFile() in components to an action (#5165) --- webapp/actions/file_actions.jsx | 26 ++++++++++++++++++++++++++ webapp/components/file_upload.jsx | 21 ++++++++++++--------- 2 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 webapp/actions/file_actions.jsx diff --git a/webapp/actions/file_actions.jsx b/webapp/actions/file_actions.jsx new file mode 100644 index 000000000..0399a2c28 --- /dev/null +++ b/webapp/actions/file_actions.jsx @@ -0,0 +1,26 @@ +// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +import * as AsyncClient from 'utils/async_client.jsx'; +import Client from 'client/web_client.jsx'; + +export function uploadFile(file, name, channelId, clientId, success, error) { + Client.uploadFile( + file, + name, + channelId, + clientId, + (data) => { + if (success) { + success(data); + } + }, + (err) => { + AsyncClient.dispatchError(err, 'uploadFile'); + + if (error) { + error(err); + } + } + ); +} diff --git a/webapp/components/file_upload.jsx b/webapp/components/file_upload.jsx index 36beb8c91..a821fedab 100644 --- a/webapp/components/file_upload.jsx +++ b/webapp/components/file_upload.jsx @@ -4,7 +4,6 @@ import $ from 'jquery'; import 'jquery-dragster/jquery.dragster.js'; import ReactDOM from 'react-dom'; -import Client from 'client/web_client.jsx'; import Constants from 'utils/constants.jsx'; import ChannelStore from 'stores/channel_store.jsx'; import DelayedAction from 'utils/delayed_action.jsx'; @@ -13,6 +12,8 @@ import * as Utils from 'utils/utils.jsx'; import {intlShape, injectIntl, defineMessages} from 'react-intl'; +import {uploadFile} from 'actions/file_actions.jsx'; + const holders = defineMessages({ limited: { id: 'file_upload.limited', @@ -89,13 +90,14 @@ class FileUpload extends React.Component { // generate a unique id that can be used by other components to refer back to this upload const clientId = Utils.generateId(); - const request = Client.uploadFile(files[i], - files[i].name, - channelId, - clientId, - this.fileUploadSuccess.bind(this, channelId), - this.fileUploadFail.bind(this, clientId, channelId) - ); + const request = uploadFile( + files[i], + files[i].name, + channelId, + clientId, + this.fileUploadSuccess.bind(this, channelId), + this.fileUploadFail.bind(this, clientId) + ); const requests = this.state.requests; requests[clientId] = request; @@ -271,7 +273,8 @@ class FileUpload extends React.Component { const name = formatMessage(holders.pasted) + d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + hour + '-' + min + '.' + ext; - const request = Client.uploadFile(file, + const request = uploadFile( + file, name, channelId, clientId, -- cgit v1.2.3-1-g7c22