Skip to content

Instantly share code, notes, and snippets.

@mflisikowski
Last active February 21, 2018 22:09
Show Gist options
  • Save mflisikowski/75e19d1e0868480c8563cc7f85e43be3 to your computer and use it in GitHub Desktop.
Save mflisikowski/75e19d1e0868480c8563cc7f85e43be3 to your computer and use it in GitHub Desktop.
Vuejs: Adding and Deleting images from Firebase
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