-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
83b62bd
commit 4e92a10
Showing
16 changed files
with
877 additions
and
177 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Import the functions you need from the SDKs you need | ||
import { initializeApp } from "firebase/app"; | ||
// import { getAnalytics } from "firebase/analytics"; | ||
import { getFirestore } from "firebase/firestore"; | ||
|
||
// Your web app's Firebase configuration | ||
const firebaseConfig = { | ||
apiKey: "AIzaSyCHZ_KT-ewZloyK0M9x0vr1zVXdnJSYBew", | ||
authDomain: "facebook-clone-uo276213.firebaseapp.com", | ||
projectId: "facebook-clone-uo276213", | ||
storageBucket: "facebook-clone-uo276213.appspot.com", | ||
messagingSenderId: "73250121069", | ||
appId: "1:73250121069:web:dda0c0b09b9c4d1b816664", | ||
measurementId: "G-BCVMQBQL8W", | ||
}; | ||
|
||
// Initialize Firebase | ||
const app = initializeApp(firebaseConfig); | ||
// const analytics = getAnalytics(app); | ||
export const db = getFirestore(app); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type { Post, PostComment } from "@/types/posts"; | ||
import { | ||
doc, | ||
query, | ||
getDoc, | ||
getDocs, | ||
collection, | ||
addDoc, | ||
orderBy | ||
} from "firebase/firestore"; | ||
import { db } from "../client"; | ||
|
||
const postsCollection = collection(db, "posts"); | ||
|
||
export const getPost = async (id: string) => { | ||
const ref = doc(db, "posts", id); | ||
return getDoc(ref); | ||
}; | ||
|
||
export const getPosts = async () => { | ||
return getDocs(query(postsCollection, orderBy("created_at", "desc"))).then(({ docs }) => | ||
docs.map((doc) => { | ||
|
||
const { user: userRef, created_at } = doc.data(); | ||
const postOwnerID = userRef.id; | ||
const likes = doc.data().feedback.likes.map(({ id }: any) => id); | ||
const comments: PostComment[] = doc | ||
.data() | ||
.feedback.comments.map((data: any) => ({ | ||
id: data.id, | ||
userID: data.user.id, | ||
text: data.text, | ||
created_at: data.created_at.toDate(), | ||
})); | ||
|
||
const post: Post = { | ||
id: doc.id, | ||
userID: postOwnerID, | ||
created_at: created_at.toDate(), | ||
body: doc.data().body, | ||
feedback: { | ||
likes, | ||
comments, | ||
}, | ||
}; | ||
return post; | ||
}) | ||
); | ||
}; | ||
|
||
export const addPost = async (post: Post) => { | ||
const { userID, created_at, ...rest } = post; | ||
const docRef = await addDoc(postsCollection, { | ||
user: doc(db, "/users/" + userID), | ||
created_at: new Date(post.created_at), | ||
...rest, | ||
}); | ||
return docRef.id; | ||
}; |
Oops, something went wrong.