-
-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #602 from VeliovGroup/dev
v1.9.10
- Loading branch information
Showing
6 changed files
with
111 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[email protected] | ||
[email protected].3 | ||
[email protected].6 | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
|
@@ -12,7 +12,7 @@ [email protected] | |
[email protected] | ||
[email protected] | ||
[email protected] | ||
[email protected].0 | ||
[email protected].6 | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
|
@@ -31,7 +31,7 @@ [email protected] | |
[email protected] | ||
[email protected] | ||
ostrio:[email protected] | ||
ostrio:[email protected].9 | ||
ostrio:[email protected].10 | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,52 @@ | ||
*This example is for the front-end UI only. The server side [methods](https://github.com/VeliovGroup/Meteor-Files/wiki#api) and [publications](https://github.com/VeliovGroup/Meteor-Files/wiki/collection) are the same.* | ||
|
||
## Brief: | ||
In this example two components is used. First - to handle the uploads, adds a file input box and progress bar. Second - to show the file details (`FileIndividualFile.jsx`). | ||
In this example two components is used. First - to handle the uploads, adds a file input box and progress bar. Second - to show the file details (`FileIndividualFile.js`). | ||
|
||
- The individual file component allows to delete, rename, and view the files. Twitter Bootstrap is used for styling; | ||
- Tested with `[email protected]`; | ||
- Currently uses mixins to access the meteor data. | ||
- Tested with `[email protected]` and `React16`; | ||
- Uses the latest `withTracker` access the meteor data. | ||
- Uses React Component (rather than deprecated createClass) | ||
|
||
## Assumptions | ||
- You have Meteor methods for `RemoveFile` and `RemoveFile` | ||
- You have a publication called `files.all` | ||
which is a FilesCollection, declared something like this: | ||
|
||
## FileUpload.jsx: | ||
```js | ||
import { FilesCollection } from 'meteor/ostrio:files'; | ||
|
||
export const UserFiles = new FilesCollection({collectionName: 'userfiles'}); | ||
// optionally attach a schema | ||
UserFiles.attachSchema(FilesCollection.schema); | ||
``` | ||
|
||
## FileUpload.js: | ||
|
||
```jsx | ||
import { ReactMeteorData } from 'meteor/react-meteor-data'; | ||
import React from 'react'; | ||
import { withTracker } from 'meteor/react-meteor-data'; | ||
import { Meteor } from 'meteor/meteor'; | ||
import IndividualFile from './FileIndividualFile.jsx'; | ||
import { _ } from 'meteor/underscore'; | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import IndividualFile from './FileIndividualFile.js'; | ||
|
||
const FileUploadComponent = React.createClass({ | ||
mixins: [ReactMeteorData], | ||
const debug = require('debug')('demo:file'); | ||
|
||
getInitialState(){ | ||
return { | ||
class FileUploadComponent extends Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
uploading: [], | ||
progress: 0, | ||
inProgress: false | ||
} | ||
}, | ||
|
||
getMeteorData() { | ||
var handle = Meteor.subscribe('files.all'); | ||
return { | ||
docsReadyYet: handle.ready(), | ||
docs: UserFiles.find().fetch() // Collection is UserFiles | ||
}; | ||
}, | ||
|
||
uploadIt(e){ | ||
"use strict"; | ||
this.uploadIt = this.uploadIt.bind(this); | ||
} | ||
|
||
uploadIt(e) { | ||
e.preventDefault(); | ||
|
||
let self = this; | ||
|
@@ -57,7 +66,7 @@ const FileUploadComponent = React.createClass({ | |
streams: 'dynamic', | ||
chunkSize: 'dynamic', | ||
allowWebWorkers: true // If you see issues with uploads, change this to false | ||
}, false); | ||
}, false) | ||
|
||
self.setState({ | ||
uploading: uploadInstance, // Keep track of this instance to use below | ||
|
@@ -67,11 +76,11 @@ const FileUploadComponent = React.createClass({ | |
// These are the event functions, don't need most of them, it shows where we are in the process | ||
uploadInstance.on('start', function () { | ||
console.log('Starting'); | ||
}); | ||
}) | ||
|
||
uploadInstance.on('end', function (error, fileObj) { | ||
console.log('On end File Object: ', fileObj); | ||
}); | ||
}) | ||
|
||
uploadInstance.on('uploaded', function (error, fileObj) { | ||
console.log('uploaded: ', fileObj); | ||
|
@@ -85,24 +94,24 @@ const FileUploadComponent = React.createClass({ | |
progress: 0, | ||
inProgress: false | ||
}); | ||
}); | ||
}) | ||
|
||
uploadInstance.on('error', function (error, fileObj) { | ||
console.log('Error during upload: ' + error); | ||
console.log('Error during upload: ' + error) | ||
}); | ||
|
||
uploadInstance.on('progress', function (progress, fileObj) { | ||
console.log('Upload Percentage: ' + progress); | ||
console.log('Upload Percentage: ' + progress) | ||
// Update our progress bar | ||
self.setState({ | ||
progress: progress | ||
}) | ||
}); | ||
}); | ||
|
||
uploadInstance.start(); // Must manually start the upload | ||
} | ||
} | ||
}, | ||
} | ||
|
||
// This is our progress bar, bootstrap styled | ||
// Remove this function if not needed | ||
|
@@ -124,18 +133,18 @@ const FileUploadComponent = React.createClass({ | |
</div> | ||
</div> | ||
} | ||
}, | ||
} | ||
|
||
render() { | ||
if (this.data.docsReadyYet) { | ||
'use strict'; | ||
debug("Rendering FileUpload",this.props.docsReadyYet); | ||
if (this.props.files && this.props.docsReadyYet) { | ||
|
||
let fileCursors = this.data.docs; | ||
let fileCursors = this.props.files; | ||
|
||
// Run through each file that the user has stored | ||
// (make sure the subscription only sends files owned by this user) | ||
let display = fileCursors.map((aFile, key) => { | ||
// console.log('A file: ', aFile.link(), aFile.get('name')); | ||
// console.log('A file: ', aFile.link(), aFile.get('name')) | ||
let link = UserFiles.findOne({_id: aFile._id}).link(); //The "view/download" link | ||
|
||
// Send out components that show details of each file | ||
|
@@ -147,7 +156,7 @@ const FileUploadComponent = React.createClass({ | |
fileSize={aFile.size} | ||
/> | ||
</div> | ||
}); | ||
}) | ||
|
||
return <div> | ||
<div className="row"> | ||
|
@@ -172,38 +181,62 @@ const FileUploadComponent = React.createClass({ | |
|
||
</div> | ||
} | ||
else return <div></div> | ||
else return <div>Loading file list</div>; | ||
} | ||
}); | ||
|
||
export default FileUploadComponent; | ||
} | ||
|
||
// | ||
// This is the HOC - included in this file just for convenience, but usually kept | ||
// in a separate file to provide separation of concerns. | ||
// | ||
export default withTracker( ( props ) => { | ||
const filesHandle = Meteor.subscribe('files.all'); | ||
const docsReadyYet = filesHandle.ready(); | ||
const files = UserFiles.find({}, {sort: {name: 1}}).fetch(); | ||
|
||
return { | ||
docsReadyYet, | ||
files, | ||
}; | ||
})(FileUploadComponent); | ||
``` | ||
|
||
## Second Component: FileIndividualFile.jsx | ||
## Second Component: FileIndividualFile.js | ||
|
||
```jsx | ||
import React from 'react'; | ||
const IndividualFile = React.createClass({ | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
class IndividualFile extends Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
}; | ||
|
||
this.removeFile = this.removeFile.bind(this); | ||
this.renameFile = this.renameFile.bind(this); | ||
|
||
} | ||
|
||
propTypes: { | ||
fileName: React.PropTypes.string.isRequired, | ||
fileSize: React.PropTypes.number.isRequired, | ||
fileUrl: React.PropTypes.string, | ||
fileId: React.PropTypes.string.isRequired | ||
}, | ||
fileName: PropTypes.string.isRequired, | ||
fileSize: PropTypes.number.isRequired, | ||
fileUrl: PropTypes.string, | ||
fileId: PropTypes.string.isRequired | ||
} | ||
|
||
removeFile(){ | ||
"use strict"; | ||
let conf = confirm('Are you sure you want to delete the file?') || false; | ||
if (conf == true) { | ||
Meteor.call('RemoveFile', this.props.fileId, function (err, res) { | ||
if (err) | ||
console.log(err); | ||
}); | ||
}) | ||
} | ||
}, | ||
} | ||
|
||
renameFile(){ | ||
"use strict"; | ||
|
||
let validName = /[^a-zA-Z0-9 \.:\+()\-_%!&]/gi; | ||
let prompt = window.prompt('New file name?', this.props.fileName); | ||
|
@@ -218,9 +251,9 @@ const IndividualFile = React.createClass({ | |
Meteor.call('RenameFile', this.props.fileId, prompt, function (err, res) { | ||
if (err) | ||
console.log(err); | ||
}); | ||
}) | ||
} | ||
}, | ||
} | ||
|
||
render() { | ||
return <div className="m-t-sm"> | ||
|
@@ -257,6 +290,6 @@ const IndividualFile = React.createClass({ | |
</div> | ||
</div> | ||
} | ||
}); | ||
} | ||
export default IndividualFile; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters