Created
November 13, 2018 15:47
-
-
Save matisiekpl/55728bba5733884c133ea7510c13c387 to your computer and use it in GitHub Desktop.
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
let imageUrl; | |
let key; | |
let uploadImg = player.img; | |
const newPlayer = { | |
name: player.name, | |
settlement: player.settlement, | |
settlementId: player.settlementId, | |
extension: player.img === undefined ? "" : player.img.name.slice(player.img.name.lastIndexOf('.')) | |
}; | |
const data = await firebase.database().ref("players").push(newPlayer); | |
const key = data.key; | |
if(uploadImg !== undefined) | |
{ | |
const storageRef = firebase.storage().ref(); | |
uploadImg = await storageRef.child(`players/${key}${newPlayer.extension}`).put(uploadImg); | |
} | |
else | |
{ | |
await firebase.database().ref('players').child(key).update({key: key}); | |
commit('addPlayer', { | |
...newPlayer, | |
id: key, | |
}); | |
} | |
if(uploadImg !== undefined) | |
{ | |
uploadImg.on('state_changed', snapshot => { | |
}, error => { | |
console.log(error) | |
}, () => { | |
const downloadURL = await uploadImg.snapshot.ref.getDownloadURL(); | |
imageUrl = downloadURL; | |
await firebase.database().ref('players').child(key).update({imageUrl: imageUrl}); | |
commit('addPlayer', { | |
...newPlayer, | |
imageUrl: imageUrl, | |
id: key, | |
}); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment