diff --git a/src/Firebase/CryptoKitties/KittiesService.js b/src/Firebase/CryptoKitties/KittiesService.js index 1f179d7..e992da1 100644 --- a/src/Firebase/CryptoKitties/KittiesService.js +++ b/src/Firebase/CryptoKitties/KittiesService.js @@ -3,7 +3,7 @@ import {db} from '../index'; export default new class KittiesService { async getAllKittiesForAddress(network, address) { - return await db.collection('kitties') + return db.collection('kitties') .doc('network') .collection(network) .where('owner', '==', address) @@ -17,7 +17,7 @@ export default new class KittiesService { } async getAllSwipeRightsForKittie(network, kittieId) { - return await db.collection('kitties') + return db.collection('kitties') .doc('network') .collection(network) .doc(kittieId) @@ -31,6 +31,25 @@ export default new class KittiesService { }); } + async getAllKittiesWithSwipeRightsForAddress(network, address) { + return db.collection('kitties') + .doc('network') + .collection(network) + .where('owner', '==', address) + .get() + .then(snapshots => { + if (snapshots.empty) { + return []; + } + return Promise.all(snapshots.docs + .map(doc => doc.id) + .map(async kittieId => ({ + kittieId, + swipeRights: await this.getAllSwipeRightsForKittie(network, kittieId) + }))); + }); + } + async poke(network, pokeId, kittieId, message, from, studId) { return db.collection('kitties') .doc('network') diff --git a/src/components/Chat.vue b/src/components/Chat.vue index 4068533..909b084 100644 --- a/src/components/Chat.vue +++ b/src/components/Chat.vue @@ -89,20 +89,8 @@ this.kitties.other = await KittiesService.getAllKittiesForAddress('mainnet', this.accounts.other); }, getUserKitties: async function() { - this.kitties.user = await KittiesService.getAllKittiesForAddress('mainnet', this.accounts.user); - this.getAllSwipeRightsForKittie(); + this.kitties.user = await KittiesService.getAllKittiesWithSwipeRightsForAddress('mainnet', this.accounts.user); }, - getAllSwipeRightsForKittie: async function() { - // todo - move this into the Kitties Service - const kittiesWithSwipeRights = await Promise.all(this.kitties.user.map(async kittie => - ({ - kittieId: kittie, - swipeRights: await KittiesService.getAllSwipeRightsForKittie('mainnet', kittie) - }) - )); - this.kitties.user = kittiesWithSwipeRights; - console.log('swipeRightsForAllKitties', kittiesWithSwipeRights); - } }, async mounted() { messaging.onMessage((payload) => {