Skip to content

Commit 999db57

Browse files
author
Walker Leite
committed
feat(component): add upload funcionality
1 parent 0179dfc commit 999db57

3 files changed

Lines changed: 79 additions & 14 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ function getFileName(file) {
5757
return file.name + Math.floor(Math.random() * 1000)
5858
}
5959

60-
function onUpload(fullPath, doUpload) {
60+
function onUpload({fullPath, doUpload}) {
6161
doUpload();
6262
}
6363

64-
function onDelete(fullPath, doDelete) {
64+
function onDelete({fullPath, doDelete}) {
6565
doDelete();
6666
}
6767
```

docs/Example with Firebase.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,41 @@ const config = {
1212
messagingSenderId: "526916383818"
1313
}
1414

15+
const app = firebase.initializeApp(config)
16+
window.unload = app.delete
17+
1518
function getFileName(file) {
16-
return file.name + Math.floor(Math.random() * 1000)
19+
return Math.floor(Math.random() * 1000) + file.name
1720
}
1821

1922
function getStorage() {
20-
return firebase.initializeApp(config).storage()
23+
return app.storage()
2124
}
2225

23-
function onUpload(fullPath, doUpload) {
26+
function onUpload({fullPath, doUpload}) {
2427
doUpload();
2528
}
2629

27-
function onDelete(fullPath, doDelete) {
30+
function onDelete({fullPath, doDelete}) {
2831
doDelete();
2932
}
3033

31-
function onClick(fullPath, downloadUrl) {
34+
function onClick({fullPath, downloadUrl}) {
3235
window.open(downloadUrl)
3336
}
3437

38+
function onError(err) {
39+
console.error(err)
40+
}
41+
3542
<ic-firebase-uploader
3643
path="images"
3744
max-files="3"
3845
:getFileName="getFileName"
3946
:storage="getStorage()"
4047
@upload="onUpload"
4148
@delete="onDelete"
42-
@click="onClick">
49+
@click="onClick"
50+
@error="onError">
4351
</ic-firebase-uploader>
4452
```
Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
<template>
22
<div class="ic-fb_uploader">
33
<ul class="ic-fb_uploader-files">
4-
<li>Lorem Ipsum1.jpg</li>
5-
<li>Lorem Ipsum2.jpg</li>
6-
<li>Lorem Ipsum3.jpg</li>
4+
<li v-for="fileRef in sentFiles" :key="fileRef.fullPath">
5+
{{fileRef.name}}
6+
</li>
77
</ul>
8-
9-
<button class="ic-fb_uploader-btn">
8+
<input
9+
type="file"
10+
class="ic-fb_uploader-loader"
11+
ref="loader"
12+
multiple
13+
@change="onChangeLoader">
14+
<button class="ic-fb_uploader-btn" @click="onBtnClick">
1015
UPLOAD
1116
</button>
1217
</div>
@@ -16,27 +21,79 @@
1621
export default {
1722
name: 'ic-firebase-uploader',
1823
props: {
24+
/**
25+
* Maximum files
26+
*/
1927
maxFiles: {
2028
type: [Number, String],
2129
required: true,
2230
},
31+
/**
32+
* Path to save files on firebase
33+
*/
2334
path: {
2435
type: String,
2536
required: true,
2637
},
38+
/**
39+
* This function is called for each user submitted file
40+
* with the associated File object, the return must be a string
41+
* containing the file name to be saved remotely
42+
*/
2743
getFileName: {
2844
type: Function,
2945
required: true,
3046
},
47+
/**
48+
* Firebase storage object
49+
* {@link https://firebase.google.com/docs/reference/js/firebase.storage | firebase.storage}
50+
*/
3151
storage: {
3252
type: Object,
3353
required: true,
3454
}
55+
},
56+
data: () => ({
57+
sentFiles: [],
58+
}),
59+
methods: {
60+
onBtnClick() {
61+
this.$refs.loader.click()
62+
},
63+
onChangeLoader(event) {
64+
const files = event.target.files
65+
const curFilesLength = this.sentFiles.length + files.length
66+
if (curFilesLength > Number(this.maxFiles)) {
67+
this.$emit('error', new Error('maximum files reached'))
68+
}
69+
const cFiles = Array.prototype.slice.call(files, 0, 3)
70+
cFiles.forEach(this.uploadFile)
71+
},
72+
uploadFile(file, index, files) {
73+
const fileName = this.getFileName(file)
74+
const rootRef = this.storage.ref()
75+
const fileRef = rootRef.child(this.path + '/' + fileName)
76+
this.$emit('upload', {
77+
fullPath: fileRef.fullPath,
78+
remainingFiles: files.length - index,
79+
file,
80+
doUpload: this.getUploadFn(fileRef, file)
81+
})
82+
},
83+
getUploadFn(fileRef, file) {
84+
return () => {
85+
fileRef.put(file)
86+
.then(() => this.sentFiles.push(fileRef))
87+
.catch(err => this.$emit('error', err))
88+
}
89+
}
3590
}
3691
}
3792
</script>
3893

3994
<style lang="css" scoped>
40-
95+
.ic-fb_uploader .ic-fb_uploader-loader {
96+
display: none;
97+
}
4198
</style>
4299

0 commit comments

Comments
 (0)