Skip to content

Instantly share code, notes, and snippets.

@matisiekpl
Created November 13, 2018 15:47
Show Gist options
  • Save matisiekpl/55728bba5733884c133ea7510c13c387 to your computer and use it in GitHub Desktop.
Save matisiekpl/55728bba5733884c133ea7510c13c387 to your computer and use it in GitHub Desktop.
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