function to give admin right
This commit is contained in:
parent
84bd68477a
commit
9d69d906d8
|
|
@ -1,5 +1,7 @@
|
||||||
import { useEffect, useState } from 'react';
|
import { useState } from 'react';
|
||||||
import Modal from './Modal';
|
import Modal from './Modal';
|
||||||
|
import { giveUserRights } from '@controllers/UserController';
|
||||||
|
import { toastError, toastSuccess } from '@controllers/Toasts';
|
||||||
|
|
||||||
const User = ({ author }: any) => {
|
const User = ({ author }: any) => {
|
||||||
const [show, setShow] = useState(false);
|
const [show, setShow] = useState(false);
|
||||||
|
|
@ -29,9 +31,18 @@ const User = ({ author }: any) => {
|
||||||
const handleRightClick = (e: any) => {
|
const handleRightClick = (e: any) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
setMessageId(e.target.closest('.message').id.slice(9));
|
setMessageId(e.target.closest('.message').id.slice(9));
|
||||||
|
|
||||||
setPopupPos({ posX: e.clientX, posY: e.clientY });
|
setPopupPos({ posX: e.clientX, posY: e.clientY });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
async function changeRights() {
|
||||||
|
setPopupPos({ posX: 0, posY: 0 });
|
||||||
|
setMessageId('0');
|
||||||
|
const response = await giveUserRights(author.id, 'ADMIN');
|
||||||
|
if (response.error) {
|
||||||
|
return toastError(response.error);
|
||||||
|
}
|
||||||
|
toastSuccess('User rights changed');
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="user">
|
<div className="user">
|
||||||
|
|
@ -51,9 +62,7 @@ const User = ({ author }: any) => {
|
||||||
<div className="popup-content space-y-2">
|
<div className="popup-content space-y-2">
|
||||||
<button
|
<button
|
||||||
className="popup-item block text-white rounded-xl p-2 transition-all hover:cursor-pointer hover:bg-grey-light hover:text-grey-dark"
|
className="popup-item block text-white rounded-xl p-2 transition-all hover:cursor-pointer hover:bg-grey-light hover:text-grey-dark"
|
||||||
onClick={() => {
|
onClick={changeRights}
|
||||||
setPopupPos({ posX: 0, posY: 0 });
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
Donner le role d'admin
|
Donner le role d'admin
|
||||||
</button>
|
</button>
|
||||||
|
|
|
||||||
|
|
@ -1,7 +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',
|
||||||
|
|
@ -13,7 +14,7 @@ const getMeInfo = async () => {
|
||||||
return response.json();
|
return response.json();
|
||||||
};
|
};
|
||||||
|
|
||||||
const login = async ({email, password}: {email: string, password: string}) => {
|
const login = async ({ email, password }: { email: string; password: string }) => {
|
||||||
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 }),
|
||||||
|
|
@ -33,15 +34,15 @@ const signup = async (formData: FormData) => {
|
||||||
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,
|
||||||
"password-confirm": formData.get('password-confirm') as string,
|
'password-confirm': formData.get('password-confirm') as string,
|
||||||
firstName: formData.get('firstName') as string,
|
firstName: formData.get('firstName') as string,
|
||||||
lastName: formData.get('lastName') as string,
|
lastName: formData.get('lastName') as string,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (form.password !== form["password-confirm"]) {
|
if (form.password !== form['password-confirm']) {
|
||||||
throw "Passwords do not match";
|
throw 'Passwords do not match';
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await fetch('/api/auth/signup', {
|
const response = await fetch('/api/auth/signup', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify(form),
|
body: JSON.stringify(form),
|
||||||
|
|
@ -57,4 +58,23 @@ const signup = async (formData: FormData) => {
|
||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const giveUserRights = async (userId: string, right: string) => {
|
||||||
|
const response = await fetch(`/api/users/${userId}/rights`, {
|
||||||
|
method: 'POST',
|
||||||
|
mode: 'cors',
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ right }),
|
||||||
|
});
|
||||||
|
if (!response.ok) {
|
||||||
|
return {error: response.statusText};
|
||||||
|
}
|
||||||
|
const data = await response.json();
|
||||||
|
if (data.error) {
|
||||||
|
return {error: data.error};
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
export { getMeInfo, login, signup };
|
export { getMeInfo, login, signup };
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue