add user controller
This commit is contained in:
parent
e84fb8db65
commit
8b561917b5
|
|
@ -1,20 +1,8 @@
|
||||||
import logo from '@assets/images/logo.svg';
|
import logo from '@assets/images/logo.svg';
|
||||||
import { useQuery } from '@tanstack/react-query';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
import { Cookies, withCookies, useCookies } from 'react-cookie';
|
import { useCookies } from 'react-cookie';
|
||||||
import Avatar from '@components/Avatar';
|
import Avatar from '@components/Avatar';
|
||||||
|
import { getMeInfo } from '@controllers/UserController';
|
||||||
const getMeInfo = async () => {
|
|
||||||
const token = new Cookies().get('token');
|
|
||||||
const response = await fetch('/api/me', {
|
|
||||||
method: 'GET',
|
|
||||||
mode: 'cors',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
Authorization: `Bearer ${token}`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
return response.json();
|
|
||||||
};
|
|
||||||
|
|
||||||
const AppHeader = () => {
|
const AppHeader = () => {
|
||||||
const meInfo = useQuery(['me'], getMeInfo, {
|
const meInfo = useQuery(['me'], getMeInfo, {
|
||||||
|
|
@ -58,4 +46,4 @@ const AppHeader = () => {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default withCookies(AppHeader);
|
export default AppHeader;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
import { FaThumbsUp } from 'react-icons/fa';
|
import { FaThumbsUp } from 'react-icons/fa';
|
||||||
import Avatar from '@components/Avatar';
|
import Avatar from '@components/Avatar';
|
||||||
import PopupMenu from './PopupMenu';
|
import PopupMenu from './PopupMenu';
|
||||||
|
import { getMeInfo } from '@controllers/UserController';
|
||||||
|
import { useQuery } from '@tanstack/react-query';
|
||||||
|
import { toastError } from '@controllers/Toasts';
|
||||||
|
|
||||||
const Image = ({ image }: { image: string }) => {
|
const Image = ({ image }: { image: string }) => {
|
||||||
if (image === '' || image === null) {
|
if (image === '' || image === null) {
|
||||||
|
|
@ -30,6 +33,15 @@ const Likes = ({ likes }: { likes: number }) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const Message = ({ message }: any) => {
|
const Message = ({ message }: any) => {
|
||||||
|
const me = useQuery(['me'], getMeInfo, {
|
||||||
|
onSuccess: (data) => {
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
onError: (error) => {
|
||||||
|
toastError(error as string);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div
|
<div
|
||||||
|
|
@ -42,7 +54,9 @@ const Message = ({ message }: any) => {
|
||||||
<div className="text-red-light text-xl username">
|
<div className="text-red-light text-xl username">
|
||||||
{message.author.firstName} {message.author.lastName}
|
{message.author.firstName} {message.author.lastName}
|
||||||
</div>
|
</div>
|
||||||
<PopupMenu message={message} />
|
{(me.data?.id === message.author.id) || (me.data?.role === 'ADMIN') ? (
|
||||||
|
<PopupMenu message={message} />
|
||||||
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
<Text text={message.content} />
|
<Text text={message.content} />
|
||||||
<Image image={message.image} />
|
<Image image={message.image} />
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,48 @@
|
||||||
|
import { Cookies } from 'react-cookie';
|
||||||
|
|
||||||
|
const getMeInfo = async () => {
|
||||||
|
const token = new Cookies().get('token');
|
||||||
|
const response = await fetch('/api/me', {
|
||||||
|
method: 'GET',
|
||||||
|
mode: 'cors',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return response.json();
|
||||||
|
};
|
||||||
|
|
||||||
|
const login = async (email: string, password: string) => {
|
||||||
|
const response = await fetch('/api/auth/login', {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ email, password }),
|
||||||
|
mode: 'cors',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const data = await response.json();
|
||||||
|
if (data.error) {
|
||||||
|
throw data.error;
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
|
const signup = async (email: string, password: string) => {
|
||||||
|
const response = await fetch('/api/auth/signup', {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ email, password }),
|
||||||
|
mode: 'cors',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const data = await response.json();
|
||||||
|
if (data.error) {
|
||||||
|
throw data.error;
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
|
export { getMeInfo, login, signup };
|
||||||
Loading…
Reference in New Issue