function to give admin right

This commit is contained in:
Guillaume Dorce 2022-10-14 12:37:00 +02:00
parent 84bd68477a
commit 9d69d906d8
2 changed files with 41 additions and 12 deletions

View File

@ -1,5 +1,7 @@
import { useEffect, useState } from 'react';
import { useState } from 'react';
import Modal from './Modal';
import { giveUserRights } from '@controllers/UserController';
import { toastError, toastSuccess } from '@controllers/Toasts';
const User = ({ author }: any) => {
const [show, setShow] = useState(false);
@ -29,10 +31,19 @@ const User = ({ author }: any) => {
const handleRightClick = (e: any) => {
e.preventDefault();
setMessageId(e.target.closest('.message').id.slice(9));
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 (
<div className="user">
<button
@ -51,9 +62,7 @@ const User = ({ author }: any) => {
<div className="popup-content space-y-2">
<button
className="popup-item block text-white rounded-xl p-2 transition-all hover:cursor-pointer hover:bg-grey-light hover:text-grey-dark"
onClick={() => {
setPopupPos({ posX: 0, posY: 0 });
}}
onClick={changeRights}
>
Donner le role d'admin
</button>

View File

@ -1,7 +1,8 @@
import { Cookies } from 'react-cookie';
const getMeInfo = async () => {
const token = new Cookies().get('token');
const getMeInfo = async () => {
const response = await fetch('/api/me', {
method: 'GET',
mode: 'cors',
@ -13,7 +14,7 @@ const getMeInfo = async () => {
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', {
method: 'POST',
body: JSON.stringify({ email, password }),
@ -33,13 +34,13 @@ const signup = async (formData: FormData) => {
const form = {
email: formData.get('email') 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,
lastName: formData.get('lastName') as string,
};
if (form.password !== form["password-confirm"]) {
throw "Passwords do not match";
if (form.password !== form['password-confirm']) {
throw 'Passwords do not match';
}
const response = await fetch('/api/auth/signup', {
@ -57,4 +58,23 @@ const signup = async (formData: FormData) => {
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 };