Ivan - Add file download component [skip ci]

This commit is contained in:
Ivan Diaz 2017-01-14 21:44:20 -03:00
parent d575882bf2
commit 35d9a166cf
9 changed files with 48 additions and 18 deletions

View File

@ -2,6 +2,7 @@ import React from 'react';
import classNames from 'classnames'; import classNames from 'classnames';
import i18n from 'lib-app/i18n'; import i18n from 'lib-app/i18n';
import API from 'lib-app/api-call';
import DateTransformer from 'lib-core/date-transformer'; import DateTransformer from 'lib-core/date-transformer';
import Icon from 'core-components/icon'; import Icon from 'core-components/icon';
@ -161,7 +162,7 @@ class TicketEvent extends React.Component {
} }
return ( return (
<div className="ticket-viewer__file"> <div className="ticket-event__file">
{node} {node}
</div> </div>
) )
@ -222,9 +223,26 @@ class TicketEvent extends React.Component {
const fileName = filePath.replace(/^.*[\\\/]/, ''); const fileName = filePath.replace(/^.*[\\\/]/, '');
return ( return (
<a href={filePath} target="_blank">{fileName}</a> <span onClick={this.onFileClick.bind(this, filePath)}>{fileName}</span>
) )
} }
onFileClick(filePath) {
API.call({
path: '/system/download',
plain: true,
data: {
file: filePath
}
}).then((result) => {
let contentType = 'application/octet-stream';
let link = document.createElement('a');
let blob = new Blob([result], {'type': contentType});
link.href = window.URL.createObjectURL(blob);
link.download = filePath;
link.click();
});
}
} }
export default TicketEvent; export default TicketEvent;

View File

@ -91,6 +91,14 @@
} }
} }
&__file {
background-color: $very-light-grey;
cursor: pointer;
text-align: right;
padding: 5px 10px;
font-size: 12px;
}
&_staff { &_staff {
.ticket-event__icon { .ticket-event__icon {
background-color: $primary-blue; background-color: $primary-blue;

View File

@ -190,6 +190,7 @@ class TicketViewer extends React.Component {
<div className="ticket-viewer__response-field row"> <div className="ticket-viewer__response-field row">
<Form {...this.getCommentFormProps()}> <Form {...this.getCommentFormProps()}>
<FormField name="content" validation="TEXT_AREA" required field="textarea" /> <FormField name="content" validation="TEXT_AREA" required field="textarea" />
<FormField name="file" field="file"/>
<SubmitButton>{i18n('RESPOND_TICKET')}</SubmitButton> <SubmitButton>{i18n('RESPOND_TICKET')}</SubmitButton>
</Form> </Form>
</div> </div>
@ -234,10 +235,12 @@ class TicketViewer extends React.Component {
loading: this.state.loading, loading: this.state.loading,
onChange: (formState) => {this.setState({ onChange: (formState) => {this.setState({
commentValue: formState.content, commentValue: formState.content,
commentFile: formState.file,
commentEdited: true commentEdited: true
})}, })},
values: { values: {
'content': this.state.commentValue 'content': this.state.commentValue,
'file': this.state.commentFile
} }
}; };
} }

View File

@ -48,13 +48,6 @@
margin-top: 10px; margin-top: 10px;
} }
&__file {
background-color: $very-light-grey;
text-align: right;
padding: 5px 10px;
font-size: 12px;
}
&__comments { &__comments {
position: relative; position: relative;
} }

View File

@ -110,6 +110,14 @@ module.exports = [
}; };
} }
}, },
{
path: '/system/download',
time: 100,
contentType: 'application/octet-stream',
response: function () {
return 'text content';
}
},
{ {
path: '/system/get-mail-templates', path: '/system/get-mail-templates',
time: 100, time: 100,

View File

@ -166,7 +166,7 @@ module.exports = [
name: 'Sales Support' name: 'Sales Support'
}, },
date: '201504090001', date: '201504090001',
file: 'http://www.opensupports.com/some_file.zip', file: 'some_file.txt',
language: 'en', language: 'en',
unread: false, unread: false,
closed: false, closed: false,
@ -385,7 +385,7 @@ module.exports = [
name: 'Technical Issues' name: 'Technical Issues'
}, },
date: '201604161427', date: '201604161427',
file: 'http://www.opensupports.com/some_file.zip', file: 'some_file.txt',
language: 'en', language: 'en',
unread: true, unread: true,
closed: false, closed: false,
@ -504,7 +504,7 @@ module.exports = [
name: 'Sales Support' name: 'Sales Support'
}, },
date: '201604150849', date: '201604150849',
file: 'http://www.opensupports.com/some_file.zip', file: 'some_file.txt',
language: 'en', language: 'en',
unread: false, unread: false,
closed: false, closed: false,
@ -607,7 +607,7 @@ module.exports = [
name: 'Sales Support' name: 'Sales Support'
}, },
date: '201504091032', date: '201504091032',
file: 'http://www.opensupports.com/some_file.zip', file: 'some_file.txt',
language: 'en', language: 'en',
unread: false, unread: false,
closed: false, closed: false,

View File

@ -12,13 +12,13 @@ function processData (data) {
} }
module.exports = { module.exports = {
call: function ({path, data}) { call: function ({path, data, plain}) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
APIUtils.post(root + path, processData(data)) APIUtils.post(root + path, processData(data))
.then(function (result) { .then(function (result) {
console.log(result); console.log(result);
if (result.status === 'success') { if (plain || result.status === 'success') {
resolve(result); resolve(result);
} else if (reject) { } else if (reject) {
reject(result); reject(result);

View File

@ -24,7 +24,7 @@ fixtures.add(require('data/fixtures/article-fixtures'));
_.each(fixtures.getAll(), function (fixture) { _.each(fixtures.getAll(), function (fixture) {
mockjax({ mockjax({
contentType: 'application/json', contentType: fixture.contentType || 'application/json',
url: 'http://localhost:3000/api' + fixture.path, url: 'http://localhost:3000/api' + fixture.path,
responseTime: fixture.time || 500, responseTime: fixture.time || 500,
response: function (settings) { response: function (settings) {

View File

@ -10,7 +10,7 @@ class DownloadController extends Controller {
'permission' => 'user', 'permission' => 'user',
'requestData' => [ 'requestData' => [
'file' => [ 'file' => [
'validation' => DataValidator::alnum('_.')->noWhitespace(), 'validation' => DataValidator::alnum('_.-')->noWhitespace(),
'error' => ERRORS::INVALID_FILE 'error' => ERRORS::INVALID_FILE
] ]
] ]