Skip to content

Instantly share code, notes, and snippets.

@felipe-pita
Created September 5, 2019 16:44
Show Gist options
  • Save felipe-pita/4c72c5428d0d5bbae65aab092dafb64f to your computer and use it in GitHub Desktop.
Save felipe-pita/4c72c5428d0d5bbae65aab092dafb64f to your computer and use it in GitHub Desktop.
import { firestore } from '../firebase/firebase.utils'
import moment from 'moment'
export function usePost() {
const createPost = async (post) => {
const { type, category, displayName, link, quote, sponsored, sponsoredclicks, target, title, userId } = post
let postAdd = {
type,
actionType: "Add",
active: true,
category,
collaborators: [],
comments: [],
createdAt: moment().toISOString(),
displayName,
likes: [],
link,
message: "",
published: false,
quote,
reported: false,
sponsored,
sponsoredclicks,
target,
title,
totalClicks: 0,
totalComments: 0,
totalLikes: 0,
totalShared: 0,
userId
}
try {
const ref = await firestore.collection('posts').add(postAdd)
console.log(ref)
return ref.id;
} catch (error) {
console.log(error)
}
}
const myFeed = (callback) => {
let items = [];
firestore.collection('posts')
.where('reported', '==', false)
.where('active', '==', true)
.orderBy('createdAt', 'desc')
.get()
.then((snap) => {
snap.forEach((value) => {
let item = value.data();
item['key'] = value.ref.id;
items.push(item);
})
callback(items)
})
}
const feedDecks = (callback) => {
let items = [];
firestore.collection('posts')
.where('reported', '==', false)
.where('active', '==', true)
.where('type', '==', 'deck')
.orderBy('createdAt', 'desc')
.get()
.then((snap) => {
snap.forEach((value) => {
let item = value.data();
item['key'] = value.ref.id;
items.push(item);
})
callback(items)
})
}
const feedDecksSuggested = (callback) => {
let items = [];
firestore.collection('posts')
.where('reported', '==', false)
.where('active', '==', true)
.where('type', '==', 'deck')
.orderBy('createdAt', 'desc')
.get()
.then((snap) => {
snap.forEach((value) => {
let item = value.data();
item['key'] = value.ref.id;
items.push(item);
})
callback(items)
})
}
const feedDecksInvited = (callback) => {
let items = [];
firestore.collection('posts')
.where('reported', '==', false)
.where('active', '==', true)
.where('type', '==', 'deck')
.orderBy('createdAt', 'desc')
.get()
.then((snap) => {
snap.forEach((value) => {
let item = value.data();
item['key'] = value.ref.id;
items.push(item);
})
callback(items)
})
}
return { createPost, myFeed, feedDecks, feedDecksSuggested, feedDecksInvited }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment