fix some problems with like annd bad usage of state
This commit is contained in:
parent
504e1352f6
commit
8214878868
|
|
@ -30,7 +30,7 @@ const AppHeader = () => {
|
||||||
</div>
|
</div>
|
||||||
<div className="right">
|
<div className="right">
|
||||||
<div className="flex items-center gap-2 sm:gap-4">
|
<div className="flex items-center gap-2 sm:gap-4">
|
||||||
{meInfo.data && <Avatar user={meInfo.data} />}
|
{meInfo.data ? <Avatar user={meInfo.data} /> : null}
|
||||||
<div className="app-header__user__name">
|
<div className="app-header__user__name">
|
||||||
{meInfo.data && <User author={meInfo.data} />}
|
{meInfo.data && <User author={meInfo.data} />}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,12 @@ import gravatar from 'gravatar';
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
|
|
||||||
const Avatar = ({ user }: any) => {
|
const Avatar = ({ user }: any) => {
|
||||||
|
// console.log(user);
|
||||||
|
|
||||||
const initials = user.firstName[0] + user.lastName[0];
|
const initials = user.firstName[0] + user.lastName[0];
|
||||||
const gravatarUrl = gravatar.url(user.email, { s: '64', r: 'x', d: '404' }, true);
|
const gravatarUrl = gravatar.url(user.email, { s: '64', r: 'x', d: '404' }, true);
|
||||||
const avatarUi = `https://ui-avatars.com/api/?name=${initials}&background=0D8ABC&color=fff&size=64`;
|
const avatarUi = `https://ui-avatars.com/api/?name=${initials}&background=0D8ABC&color=fff&size=64`;
|
||||||
|
// const avatarUi = `https://ui-avatars.com/api/?name=GD&background=0D8ABC&color=fff&size=64`;
|
||||||
const [avatar, setAvatar] = useState(avatarUi);
|
const [avatar, setAvatar] = useState(avatarUi);
|
||||||
const [firstLoad, setFirstLoad] = useState(true);
|
const [firstLoad, setFirstLoad] = useState(true);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ const Message = ({ message }: any) => {
|
||||||
toastError(error as string);
|
toastError(error as string);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div
|
<div
|
||||||
|
|
@ -45,7 +45,7 @@ const Message = ({ message }: any) => {
|
||||||
<div className="flex flex-col gap-2 relative flex-grow">
|
<div className="flex flex-col gap-2 relative flex-grow">
|
||||||
<div className="flex justify-between">
|
<div className="flex justify-between">
|
||||||
{message.author && <User author={message.author} />}
|
{message.author && <User author={message.author} />}
|
||||||
{(me.data?.id === message.author.id) || (me.data?.role === 'ADMIN') ? (
|
{me.data?.id === message.author.id || me.data?.role === 'ADMIN' || me.data?.role === 'CREATOR' ? (
|
||||||
<PopupMenu message={message} />
|
<PopupMenu message={message} />
|
||||||
) : null}
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -60,7 +60,14 @@ const Message = ({ message }: any) => {
|
||||||
minute: 'numeric',
|
minute: 'numeric',
|
||||||
})}
|
})}
|
||||||
</div>
|
</div>
|
||||||
<Like messageId={message.id} isLiked={(message.likes > 0 && message.likedBy.find((like: any) => like.userId === me.data?.id)) ? true : false} />
|
{me.data?.id === message.author.id ? null : (
|
||||||
|
<Like
|
||||||
|
messageId={message.id}
|
||||||
|
isLiked={
|
||||||
|
message.likes > 0 && message.likedBy.find((like: any) => like.userId === me.data?.id) ? true : false
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</>
|
</>
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,11 @@
|
||||||
import { ReactNode } from "react";
|
import { ReactNode } from 'react';
|
||||||
|
|
||||||
const Modal = ({ children, show, className = '' }: { children: ReactNode; show: boolean, className?: string }) => {
|
const Modal = ({ children, show, className = '' }: { children: ReactNode; show: boolean; className?: string }) => {
|
||||||
|
if (!show) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
<div
|
<div className="modal overflow-y-auto overflow-x-hidden fixed top-0 right-0 left-0 bottom-0 w-full h-full z-1000 flex justify-center items-center">
|
||||||
className={
|
|
||||||
`modal overflow-y-auto overflow-x-hidden fixed top-0 right-0 left-0 bottom-0 w-full h-full z-1000 flex justify-center items-center` +
|
|
||||||
(show ? '' : ' hidden')
|
|
||||||
}
|
|
||||||
>
|
|
||||||
<div className="absolute w-full h-full top-0 left-0 bg-gray-900 opacity-70"></div>
|
<div className="absolute w-full h-full top-0 left-0 bg-gray-900 opacity-70"></div>
|
||||||
<div className={`relative rounded-lg shadow dark:bg-grey-dark p-4 ${className}`}>{children}</div>
|
<div className={`relative rounded-lg shadow dark:bg-grey-dark p-4 ${className}`}>{children}</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ const User = ({ author }: any) => {
|
||||||
return (
|
return (
|
||||||
<div className="user">
|
<div className="user">
|
||||||
<button
|
<button
|
||||||
className="text-red-light text-xl username"
|
className="text-red-light text-xl username text-left"
|
||||||
onClick={() => setShow(true)}
|
onClick={() => setShow(true)}
|
||||||
onContextMenu={(e) => {
|
onContextMenu={(e) => {
|
||||||
if (author.role === 'CREATOR') {
|
if (author.role === 'CREATOR') {
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
import { Cookies } from 'react-cookie';
|
import { Cookies } from 'react-cookie';
|
||||||
|
|
||||||
const token = new Cookies().get('token');
|
|
||||||
|
|
||||||
const getMeInfo = async () => {
|
const getMeInfo = async () => {
|
||||||
|
const token = new Cookies().get('token');
|
||||||
|
|
||||||
const response = await fetch('/api/me', {
|
const response = await fetch('/api/me', {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
mode: 'cors',
|
mode: 'cors',
|
||||||
|
|
@ -11,10 +11,16 @@ const getMeInfo = async () => {
|
||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${token}`,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
return response.json();
|
const data = await response.json();
|
||||||
|
if (data.error) {
|
||||||
|
throw data.error;
|
||||||
|
}
|
||||||
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
const login = async ({ email, password }: { email: string; password: string }) => {
|
const login = async ({ email, password }: { email: string; password: string }) => {
|
||||||
|
const token = new Cookies().get('token');
|
||||||
|
|
||||||
const response = await fetch('/api/auth/login', {
|
const response = await fetch('/api/auth/login', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify({ email, password }),
|
body: JSON.stringify({ email, password }),
|
||||||
|
|
@ -31,6 +37,8 @@ const login = async ({ email, password }: { email: string; password: string }) =
|
||||||
};
|
};
|
||||||
|
|
||||||
const signup = async (formData: FormData) => {
|
const signup = async (formData: FormData) => {
|
||||||
|
const token = new Cookies().get('token');
|
||||||
|
|
||||||
const form = {
|
const form = {
|
||||||
email: formData.get('email') as string,
|
email: formData.get('email') as string,
|
||||||
password: formData.get('password') as string,
|
password: formData.get('password') as string,
|
||||||
|
|
@ -59,6 +67,8 @@ const signup = async (formData: FormData) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
export const giveUserRights = async (userId: string, role: string) => {
|
export const giveUserRights = async (userId: string, role: string) => {
|
||||||
|
const token = new Cookies().get('token');
|
||||||
|
|
||||||
const response = await fetch(`/api/users/${userId}/roles`, {
|
const response = await fetch(`/api/users/${userId}/roles`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
mode: 'cors',
|
mode: 'cors',
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ export default async (req: Request, res: Response) => {
|
||||||
const id = parseInt(req.params.id);
|
const id = parseInt(req.params.id);
|
||||||
const likedPost = await likePost(id, req.userId);
|
const likedPost = await likePost(id, req.userId);
|
||||||
if (likedPost instanceof Error) {
|
if (likedPost instanceof Error) {
|
||||||
return res.status(403).send(likedPost.message);
|
return res.status(403).send({error: likedPost.message});
|
||||||
}
|
}
|
||||||
return res.status(200).send({ message: 'Post liked' });
|
return res.status(200).send({ message: 'Post liked' });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
|
||||||
|
|
@ -159,9 +159,9 @@ const likePost = async (id: number, userId: number): Promise<PrismaPost | Error>
|
||||||
if (post === null) {
|
if (post === null) {
|
||||||
return new Error('Post not found');
|
return new Error('Post not found');
|
||||||
}
|
}
|
||||||
// if (post.authorId === userId) {
|
if (post.authorId === userId) {
|
||||||
// return new Error('User cannot like their own post');
|
return new Error('User cannot like their own post');
|
||||||
// }
|
}
|
||||||
if (post.likedBy.some((like) => like.userId === userId)) {
|
if (post.likedBy.some((like) => like.userId === userId)) {
|
||||||
return new Error('Post already liked');
|
return new Error('Post already liked');
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue