-
+
Whoopsies, your feed is empty! Click plus button to add post
@@ -48,4 +45,4 @@ const Posts = () => {
>
);
};
-export default React.memo(Posts);
+export default memo(Posts);
diff --git a/src/components/chat/components/SubmitMessage.tsx b/src/components/chat/components/SubmitMessage.tsx
index c88836c..7821b9d 100644
--- a/src/components/chat/components/SubmitMessage.tsx
+++ b/src/components/chat/components/SubmitMessage.tsx
@@ -1,13 +1,18 @@
import { useEffect, useRef, useState } from "react";
import { auth, db } from "../../../firebase-config";
import { useLocation } from "react-router-dom";
-import { FieldValue, addDoc, collection, serverTimestamp } from "firebase/firestore";
+import {
+ FieldValue,
+ addDoc,
+ collection,
+ serverTimestamp,
+} from "firebase/firestore";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { IHandleSubmitMessageParams } from "../../../types/Types";
import { format } from "date-fns";
type SubmitMessageProps = {
- username: string;
+ username?: string;
};
const SubmitMessage = ({ username }: SubmitMessageProps) => {
diff --git a/src/components/likedposts/LikedDesc.tsx b/src/components/likedposts/LikedDesc.tsx
index d334161..69b064a 100644
--- a/src/components/likedposts/LikedDesc.tsx
+++ b/src/components/likedposts/LikedDesc.tsx
@@ -2,9 +2,9 @@ import { memo } from "react";
import LoadingImage from "../LoadingStates/LoadingImage";
type DescProps = {
- username: string;
- status: string;
- userPicture: string;
+ username?: string;
+ status?: string;
+ userPicture?: string;
loading: boolean;
};
@@ -24,9 +24,9 @@ const LikedDesc = ({ userPicture, username, status, loading }: DescProps) => {
{loading ? (
-
Loading...
+ Loading...
) : (
- {username}
+ {username}
)}
{loading ? (
diff --git a/src/hooks/useQueryHooks/useUserData.tsx b/src/hooks/useQueryHooks/useUserData.tsx
index d11f85f..5f0e211 100644
--- a/src/hooks/useQueryHooks/useUserData.tsx
+++ b/src/hooks/useQueryHooks/useUserData.tsx
@@ -1,13 +1,14 @@
-import { useQuery } from "@tanstack/react-query";
-import { doc, getDoc } from "firebase/firestore";
-import { db } from "../../firebase-config";
+import { useQuery, UseQueryResult } from "@tanstack/react-query";
import { handleUserDataset } from "../../methods/methods";
+import { User } from "../../types/Types";
-const useUserData = () => {
- return useQuery({
+const useUserData = (): UseQueryResult
=> {
+ return useQuery({
queryFn: handleUserDataset,
queryKey: ["userdataset"],
});
};
+export type UseUserData = ReturnType;
+
export default useUserData;
diff --git a/src/methods/methods.ts b/src/methods/methods.ts
index f8f50b9..24400bf 100644
--- a/src/methods/methods.ts
+++ b/src/methods/methods.ts
@@ -1,6 +1,7 @@
import { db, auth } from "../firebase-config";
-import { doc, updateDoc, getDoc } from "firebase/firestore";
+import { doc, updateDoc, getDoc, DocumentData } from "firebase/firestore";
import {
+ User,
Post,
Comment,
AddCommentMutationProps,
@@ -8,6 +9,7 @@ import {
EditProfilePopupData,
AddPostData,
EditIconMutationProps,
+ HandleLikeMutationParams,
} from "../types/Types";
import format from "date-fns/format";
@@ -17,25 +19,48 @@ import format from "date-fns/format";
const uid = localStorage.getItem("uid")!;
export const handleLike = async (
- variables: DeletePostMutationProps,
+ variables: HandleLikeMutationParams,
): Promise => {
+ console.log(variables);
+ if (!variables.user_id)
+ throw new Error("Provide correct user_id for like mutation");
+ const newLikes = [...variables.target_post.likes, variables.like];
const newPosts: Post[] = variables.posts.map((post) =>
- post.id == variables.id ? { ...post, liked: !post.liked } : post,
+ post.id === variables.post_id ? { ...post, likes: newLikes } : post,
);
const newpostsdb = {
- newPosts: newPosts,
+ newPosts,
+ };
+ const userdoc = doc(db, "users", variables.user_id);
+ await updateDoc(userdoc, newpostsdb);
+};
+
+export const handleUnlike = async (
+ variables: HandleLikeMutationParams,
+): Promise => {
+ if (!variables.user_id)
+ throw new Error("Provide correct user_id for unlike mutation");
+ const newLikes = variables.target_post.likes.filter((like) => {
+ like.user_id === variables.like.user_id;
+ });
+ const newPosts: Post[] = variables.posts.map((post) =>
+ post.id === variables.post_id ? { ...post, likes: newLikes } : post,
+ );
+ const newpostsdb = {
+ newPosts,
};
- const userdoc = doc(db, "users", `${uid}`);
+ const userdoc = doc(db, "users", variables.user_id);
await updateDoc(userdoc, newpostsdb);
};
-export const handleUserDataset = async () => {
+export const handleUserDataset = async (): Promise => {
const userdoc = doc(db, "users", uid);
const dataSnap = await getDoc(userdoc);
- return dataSnap.data();
+ const user: DocumentData | undefined = dataSnap.data();
+ return user as User;
};
-export const handleNewPost = async (variables: AddPostData) => {
+export const handleNewPost = async (variables: AddPostData): Promise => {
const userdoc = doc(db, "users", uid);
const dataSnap = await getDoc(userdoc);
const dataset = dataSnap.data();
@@ -48,6 +73,7 @@ export const handleNewPost = async (variables: AddPostData) => {
imgsrc: variables.url,
liked: false,
comments: [],
+ likes: [],
};
const newPosts = [newPost, ...nposts];
@@ -68,7 +94,9 @@ export const handleDelete = async (
await updateDoc(userdoc, newpostsdb);
};
-export const handleProfileIcon = async (variables: EditIconMutationProps) => {
+export const handleProfileIcon = async (
+ variables: EditIconMutationProps,
+): Promise => {
const uid = localStorage.getItem("uid")!;
const userdoc = doc(db, "users", uid);
const updatedImage = {
@@ -82,7 +110,8 @@ export const handleAddComment = async ({
post,
id: userid,
}: AddCommentMutationProps): Promise => {
- if (typeof userid === "undefined") throw new Error("You cannot provide uid of type undefined")
+ if (typeof userid === "undefined")
+ throw new Error("You cannot provide uid of type undefined");
const userdoc = doc(db, "users", userid);
const dataSnap = await getDoc(userdoc);
const dataset = dataSnap.data();
@@ -119,7 +148,7 @@ export const fetchComments = async ({
}: {
uid: string | undefined;
post_id: number | undefined;
-}) => {
+}): Promise => {
if (!uid || !post_id)
throw new Error("Provide correct uid and post_id to fetch comments");
const userdoc = doc(db, "users", uid);
@@ -131,7 +160,9 @@ export const fetchComments = async ({
return post?.comments;
};
-export const handleEditProfile = async (variables: EditProfilePopupData) => {
+export const handleEditProfile = async (
+ variables: EditProfilePopupData,
+): Promise => {
const uid = localStorage.getItem("uid")!;
const newstatusdb = {
name: variables.username,
diff --git a/src/styles/index.scss b/src/styles/index.scss
index 52cabb0..848092e 100644
--- a/src/styles/index.scss
+++ b/src/styles/index.scss
@@ -622,6 +622,17 @@ body {
overflow: hidden;
white-space: nowrap;
}
+
+ &__like-count {
+ display: grid;
+ place-items: center;
+ text-align: center;
+ gap: 1px;
+ }
+
+ &__count {
+ font-size: 0.9rem;
+ }
}
@keyframes opacious {
@@ -937,6 +948,7 @@ body {
min-height: 4rem;
background-color: white;
border-radius: 1rem;
+ opacity: 0.3;
animation: opacious infinite 3s ease-in;
}
diff --git a/src/types/Types.tsx b/src/types/Types.tsx
index 9c6cd37..fb761e3 100644
--- a/src/types/Types.tsx
+++ b/src/types/Types.tsx
@@ -1,6 +1,20 @@
import { ReactElement } from "react";
import { FieldValue } from "firebase/firestore";
+// --------------------------------------
+// Mutation handlers params
+// --------------------------------------
+
+export type HandleLikeMutationParams = {
+ post_id: number;
+ user_id?: string;
+ target_post: Post;
+ posts: Post[];
+ like: Like;
+};
+
+// --------------------------------------
+
export type AddCommentMutationProps = {
comment: string;
post?: Post;
@@ -30,38 +44,15 @@ export type EditProfilePopupData = {
status: string;
};
-export type LikedPost = {
- city: string;
- creator: string;
- id: number;
- imgsrc: string;
-};
-
export interface IHandleSubmitMessageParams {
e: React.FormEvent;
- creator: string;
+ creator?: string;
image: string | null | undefined;
message: string;
timestamp: FieldValue;
userpair: string;
}
-export type Comment = {
- creator: string;
- message: string;
- createdAt: string;
- id: number;
- img: string;
-};
-
-export type Post = {
- city: string;
- id: number;
- imgsrc: string;
- liked: boolean;
- comments: Comment[];
-};
-
export type CommentsQuery = {
cuid: string;
postId: number;
@@ -87,3 +78,44 @@ export type SnapType = {
userpair: string;
id: string;
};
+
+/**
+ * Fundamental types
+ */
+
+export type Comment = {
+ creator: string;
+ message: string;
+ createdAt: string;
+ id: number;
+ img: string;
+};
+
+export type Post = {
+ city: string;
+ id: number;
+ imgsrc: string;
+ liked: boolean;
+ comments: Comment[];
+ likes: Like[];
+};
+
+export type LikedPost = {
+ city: string;
+ creator: string;
+ id: number;
+ imgsrc: string;
+};
+
+export type Like = {
+ user_id?: string;
+};
+
+export type User = {
+ id: string;
+ imgurl: string;
+ liked: LikedPost[];
+ name: string;
+ newPosts: Post[];
+ newStatus: string;
+};