From 86854956eb8b29a5e90ba471e1e0acd1c9d97d75 Mon Sep 17 00:00:00 2001 From: kuwsh1n Date: Fri, 21 Jun 2024 19:39:42 +0300 Subject: [PATCH] actions by users --- .../bodyModal/editUserAdminPanel.module.scss | 42 ++++ .../components/defaultModal.module.scss | 7 + src/components/DefaultModal.jsx | 29 +++ src/components/MyInput.jsx | 2 +- .../bodyModal/EditUserAdminPanel.jsx | 133 ++++++++++++ src/hooks/api/adminApi.js | 26 ++- src/hooks/api/listFormsApi.js | 11 +- src/hooks/api/profileApi.js | 69 ++++++- src/hooks/services/profile.js | 20 ++ src/pages/AdminPanel.jsx | 191 ++++++++++++++---- src/pages/NewForm.jsx | 2 - src/pages/Profile.jsx | 8 +- 12 files changed, 492 insertions(+), 48 deletions(-) create mode 100644 src/assets/styles/components/bodyModal/editUserAdminPanel.module.scss create mode 100644 src/assets/styles/components/defaultModal.module.scss create mode 100644 src/components/DefaultModal.jsx create mode 100644 src/components/bodyModal/EditUserAdminPanel.jsx create mode 100644 src/hooks/services/profile.js diff --git a/src/assets/styles/components/bodyModal/editUserAdminPanel.module.scss b/src/assets/styles/components/bodyModal/editUserAdminPanel.module.scss new file mode 100644 index 0000000..6c7b426 --- /dev/null +++ b/src/assets/styles/components/bodyModal/editUserAdminPanel.module.scss @@ -0,0 +1,42 @@ +.main { + display: flex; + align-items: center; + flex-direction: column; + padding: 3%; + &__status { + display: flex; + justify-content: space-between; + align-items: center; + width: 100%; + margin-top: 5%; + &__admin { + display: flex; + justify-content: space-around; + align-items: center; + font-family: "Montserrat", sans-serif; + color: rgb(40, 40, 40); + font-size: 15px; + width: 50%; + border: 1px solid rgb(180, 180, 180); + border-radius: 5px 0 0 5px; + border-right: none + &__checkbox { + + } + } + &__teacher { + display: flex; + justify-content: space-around; + font-family: "Montserrat", sans-serif; + color: rgb(40, 40, 40); + font-size: 15px; + align-items: center; + width: 50%; + border: 1px solid rgb(180, 180, 180); + border-radius: 0 5px 5px 0; + &__checkbox { + + } + } + } +} \ No newline at end of file diff --git a/src/assets/styles/components/defaultModal.module.scss b/src/assets/styles/components/defaultModal.module.scss new file mode 100644 index 0000000..4803e05 --- /dev/null +++ b/src/assets/styles/components/defaultModal.module.scss @@ -0,0 +1,7 @@ +.main { + &__dialog { + &__content { + + } + } +} diff --git a/src/components/DefaultModal.jsx b/src/components/DefaultModal.jsx new file mode 100644 index 0000000..a839f27 --- /dev/null +++ b/src/components/DefaultModal.jsx @@ -0,0 +1,29 @@ +import React, { useState, useEffect, useContext } from "react"; +import classes from "../assets/styles/components/defaultModal.module.scss"; +import MyButton from "./MyButton.jsx"; + + +const DefaultModal = ({ + name, + postfix, + btn, + BodyModal +}) => { + + return ( + + ) +} + +export default DefaultModal; \ No newline at end of file diff --git a/src/components/MyInput.jsx b/src/components/MyInput.jsx index 60012c9..75e07ac 100644 --- a/src/components/MyInput.jsx +++ b/src/components/MyInput.jsx @@ -1,5 +1,5 @@ import React from "react"; -import classes from "../assets/styles/components/myInput.module.scss" +import classes from "../assets/styles/components/myInput.module.scss"; const MyInput = (props) => { return ( diff --git a/src/components/bodyModal/EditUserAdminPanel.jsx b/src/components/bodyModal/EditUserAdminPanel.jsx new file mode 100644 index 0000000..f4cfc8b --- /dev/null +++ b/src/components/bodyModal/EditUserAdminPanel.jsx @@ -0,0 +1,133 @@ +import React, {useState} from "react"; +import classes from "../../assets/styles/components/bodyModal/editUserAdminPanel.module.scss"; +import MyInput from "../../components/MyInput.jsx"; + + +const EditUserAdminPanel = ({ + id, + email, + firstName, + isAdmin, + isTeacher, + lastName, + login, + phone, + setEmail, + setFirstName, + setIsAdmin, + setIsTeacher, + setLastName, + setLogin, + setPhone, +}) => { + + console.log('228', id, isAdmin, isAdmin) + + return ( +
+ + + + + +
+
+
Администратор
+
+
+ setIsAdmin(true)} + /> + +
+
+ setIsAdmin(false)} + /> + +
+
+
+
+
Учитель
+
+
+ setIsTeacher(true)} + /> + +
+
+ setIsTeacher(false)} + /> + +
+
+
+
+ +
+ ) +} + +export default EditUserAdminPanel; \ No newline at end of file diff --git a/src/hooks/api/adminApi.js b/src/hooks/api/adminApi.js index 027cfa4..09afd91 100644 --- a/src/hooks/api/adminApi.js +++ b/src/hooks/api/adminApi.js @@ -15,4 +15,28 @@ async function listUsersApi(token) { } }; -export {listUsersApi} \ No newline at end of file +async function editUserApi(token, data) { + try { + const response = await axios.post(`https://api.minerva.krbl.ru/auth/manage/edit`, + { + "email": data.email, + "first_name": data.first_name, + "id": data.id, + "is_admin": data.is_admin, + "last_name": data.last_name, + "login": data.login, + "phone": data.phone + }, + { + headers: { + "Authorization": `Token ${token}` + } + }) + return response + } + catch (e) { + return e + } +}; + +export {listUsersApi, editUserApi} \ No newline at end of file diff --git a/src/hooks/api/listFormsApi.js b/src/hooks/api/listFormsApi.js index c8cac56..0695eda 100644 --- a/src/hooks/api/listFormsApi.js +++ b/src/hooks/api/listFormsApi.js @@ -88,6 +88,7 @@ async function listFormsByTokenApi(token, formId) { "Authorization": `Token ${token}` } }) + console.log(response) return response } catch (e) { @@ -113,4 +114,12 @@ async function removeTokenFormApi(token, formId, formToken) { } } -export { listFormsApi, createFormApi, removeFormApi, updateTitleFormApi, newFormTokenApi, listFormsByTokenApi, removeTokenFormApi }; \ No newline at end of file +export { + listFormsApi, + createFormApi, + removeFormApi, + updateTitleFormApi, + newFormTokenApi, + listFormsByTokenApi, + removeTokenFormApi +}; \ No newline at end of file diff --git a/src/hooks/api/profileApi.js b/src/hooks/api/profileApi.js index 68e1e04..e63408b 100644 --- a/src/hooks/api/profileApi.js +++ b/src/hooks/api/profileApi.js @@ -6,8 +6,9 @@ async function editUserApi(token, data) { { "email": data.email, "first_name": data.first_name, - "id": "6617fec3f603c43e1719d3c8", + "id": data.id, "is_admin": data.is_admin, + "is_teacher": data.is_teacher, "last_name": data.last_name, "login": data.login, "phone": data.phone @@ -24,8 +25,68 @@ async function editUserApi(token, data) { catch(e) { console.log(e) return e - } - // return false + } +}; + + +// async function getListUserApi(token) { +// try { +// const response = await axios.get(`https://api.minerva.krbl.ru/auth/manage/users`, +// { +// headers: { +// "Authorization": `Token ${token}` +// } +// }) +// return response +// } +// catch (e) { +// return e +// } +// }; + + +async function getListUserApi(token) { + try { + const response = await axios.get(`https://api.minerva.krbl.ru/auth/manage/users`, + { + headers: { + "Authorization": `Token ${token}` + } + }) + return response + } + catch (e) { + return e + } +}; + +async function addUserApi(token, data) { + try { + const response = await axios.post(`https://api.minerva.krbl.ru/auth/manage/add`, + { + "email": data.email, + "first_name": data.first_name, + // "id": data.id, + "is_admin": data.is_admin, + "is_teacher": data.is_teacher, + "last_name": data.last_name, + "login": data.login, + "phone": data.phone + }, + { + headers: { + "Authorization": `Token ${token}` + } + }) + return response + } + catch (e) { + return e + } } -export {editUserApi} \ No newline at end of file +export { + editUserApi, + getListUserApi, + addUserApi +} \ No newline at end of file diff --git a/src/hooks/services/profile.js b/src/hooks/services/profile.js new file mode 100644 index 0000000..44cb9ae --- /dev/null +++ b/src/hooks/services/profile.js @@ -0,0 +1,20 @@ +import { verifyUserApi } from "../api/enterAccountApi"; + + +async function verifyUser(cookies, user, setUser) { + const response = await verifyUserApi(cookies.token); + + if (response) { + if (response.status === 200) { + setUser(response.data); + return response + } + else { + console.log(response) + } + } +} + +export { + verifyUser +} \ No newline at end of file diff --git a/src/pages/AdminPanel.jsx b/src/pages/AdminPanel.jsx index c6acd8a..a5eeea6 100644 --- a/src/pages/AdminPanel.jsx +++ b/src/pages/AdminPanel.jsx @@ -1,40 +1,110 @@ -import React, { useState } from "react"; +import React, { useState, useEffect, useContext } from "react"; +import { useCookies } from "react-cookie"; import { useNavigate } from "react-router-dom"; import classes from "../assets/styles/adminPanel.module.scss"; +import { UserData } from "../context"; +import { getListUserApi, editUserApi, addUserApi } from "../hooks/api/profileApi.js"; +import { verifyUser } from "../hooks/services/profile.js"; +import DefaultModal from "../components/DefaultModal.jsx"; +import EditUserAdminPanel from "../components/bodyModal/EditUserAdminPanel.jsx"; +import MyButton from "../components/MyButton.jsx"; + const AdminPanel = () => { - const [token, setToken] = useState(""); const navigate = useNavigate(); + const [cookies, setCookies] = useCookies(["user"]); + const {user, setUser} = useContext(UserData); + const [listUser, setListUser] = useState([]); - const [users, setUsers] = useState([ - { - "email": "senya.bogachev@mail.ru", - "first_name": "Арсений", - "id": "1", - "is_admin": false, - "last_name": "Богачев", - "login": "Ars", - "phone": "89110128244" - }, - { - "email": "dasha@mail.ru", - "first_name": "Дарья", - "id": "1", - "is_admin": false, - "last_name": "Утешева", - "login": "dasha", - "phone": "89111032339" - }, - { - "email": "kirik@mail.ru", - "first_name": "Кирилл", - "id": "string", - "is_admin": false, - "last_name": "Жердев", - "login": "Kirik", - "phone": "89110045673" + const [email, setEmail] = useState("") + const [firstName, setFirstName] = useState("") + const [id, setId] = useState(false) + const [isAdmin, setIsAdmin] = useState(false) + const [isTeacher, setIsTeacher] = useState(false) + const [lastName, setLastName] = useState("") + const [login, setLogin] = useState("") + const [phone, setPhone] = useState("") + + useEffect(() => { + async function getListUser() { + const responseListUser = await getListUserApi(cookies.token); + const responseCurrentUser = await verifyUser(cookies, user, setUser) + + if ( + responseListUser.status === 200 && + responseCurrentUser.status === 200 && + responseCurrentUser.data.is_admin + ) { + setListUser([...responseListUser.data].filter(item => item.id !== responseCurrentUser.data.id)) + } + else { + console.log('responseListUser', responseListUser) + console.log('responseCurrentUser', responseCurrentUser) + } } - ]) + + getListUser() + }, []); + + function clearState() { + setEmail("") + setFirstName("") + setId("") + setIsAdmin("") + setIsTeacher("") + setLastName("") + setLogin("") + setPhone("") + } + + async function addUser() { + const currentState = { + email: email, + first_name: firstName, + id: id, + is_admin: isAdmin, + is_teacher: isTeacher, + last_name: lastName, + login: login, + phone: phone + } + const response = await addUserApi(cookies.token, currentState) + + if (response.status === 200) { + setListUser([...listUser, currentState]) + } + clearState() + } + + async function editUser() { + const currentState = { + email: email, + first_name: firstName, + id: id, + is_admin: isAdmin, + is_teacher: isTeacher, + last_name: lastName, + login: login, + phone: phone + } + const response = await editUserApi(cookies.token, currentState) + + if (response.status === 200) { + setListUser(listUser.map(item => item.id === id ? currentState : item)) + } + clearState() + } + + function openUser(user) { + setEmail(user.email) + setFirstName(user.first_name) + setId(user.id) + setIsAdmin(user.is_admin) + setIsTeacher(user.is_teacher) + setLastName(user.last_name) + setLogin(user.login) + setPhone(user.phone) + } return (
@@ -46,22 +116,71 @@ const AdminPanel = () => {

Пользователи

- +
Логин
Фамилия
Email
+
Учитель
+
Администратор
- {users.map(item => -
-
{item.login}
-
{item.last_name}
-
{item.email}
+ {listUser.map(item => +
+
openUser(item)} + > + {item.login} +
+
+ {item.last_name ? item.last_name : '-'} +
+
+ {item.email ? item.email : '-'} +
+
+ {item.is_teacher ? 'Да' : 'Нет'} +
+
+ {item.admin ? 'Да' : 'Нет'} +
)} + + } + btn={[ + {text: id ? "Изменить" : "Сохранить", dismiss: "modal", class: "main__green", action: id ? () => editUser() : () => addUser()}, + {text: "Отмена", dismiss: "modal", class: "main__white", action: () => clearState()} + ]} + />
diff --git a/src/pages/NewForm.jsx b/src/pages/NewForm.jsx index 4df511a..198491b 100644 --- a/src/pages/NewForm.jsx +++ b/src/pages/NewForm.jsx @@ -112,8 +112,6 @@ const NewForm = () => { }; async function updateBlock() { - console.log(optionAnswer) - const data = { question: question, comment: comment, diff --git a/src/pages/Profile.jsx b/src/pages/Profile.jsx index b2aad0b..b5e01c1 100644 --- a/src/pages/Profile.jsx +++ b/src/pages/Profile.jsx @@ -68,7 +68,9 @@ const Profile = () => { last_name: last_name, email: email, phone: phone, - is_admin: user.is_admin + is_admin: user.is_admin, + is_teacher: user.is_teacher, + id: user.id }) if (response.status === 200) { @@ -95,9 +97,9 @@ const Profile = () => {
- {/* {user.is_admin ?
+ {user.is_admin ?
navigate("/admin")}>Админ панель -
:
} */} +
:
}

Ваши данные