Last active
February 21, 2018 22:09
-
-
Save mflisikowski/75e19d1e0868480c8563cc7f85e43be3 to your computer and use it in GitHub Desktop.
Vuejs: Adding and Deleting images from Firebase
This file contains hidden or 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
export default { | |
data() { | |
return { | |
images: [] | |
} | |
}, | |
created() { | |
this.getImages() | |
}, | |
computed: { | |
reverseImages() { | |
return this.images.slice().reverse() | |
} | |
}, | |
watch: { | |
images() { | |
this.getImages() | |
} | |
}, | |
methods: { | |
cleanUpImagesArray() { | |
this.images = [] | |
}, | |
imageRefDel(name) { | |
return this.$storage.child(`images/${name}`).delete() | |
}, | |
async deleteImages(image) { | |
const original = await this.imageRefDel(image.oryginal) | |
const large = await this.imageRefDel(image.large) | |
const small = await this.imageRefDel(image.small) | |
const icon = await this.imageRefDel(image.icon) | |
return Promise.all([ original, large, small, icon ]) | |
.then(() => { | |
this.$database.ref(this.databaseRef) | |
.child(this.removeItemId) | |
.remove((err) => { | |
if (err) console.error(err) | |
}) | |
}) | |
.catch(err => console.log('deleteImages error: ', err)) | |
}, | |
async getImages() { | |
const snapshot = await this.$database.ref('images').once('value') | |
const snapshotData = snapshot.val() || {} | |
this.cleanUpImagesArray() | |
Object | |
.entries(snapshotData) | |
.map((data) => { | |
const image = Object.assign({}, {id: data[0]}, data[1]) | |
this.images.push(image) | |
}) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment