From 8c0b3171cc5b3f8b7f0424ca2ed49ddc5fa3ef70 Mon Sep 17 00:00:00 2001 From: adbusnel Date: Fri, 11 Oct 2024 13:03:04 +0200 Subject: [PATCH 01/53] [MOD] fixed help number modification, changed popup delete alert, modify account class --- .../Accounts/SchoolAdm/schoolAccountsTable.js | 104 ++++++++++++------ src/Components/Popup/alertDeletion.jsx | 15 +++ src/Components/Popup/alertModification.jsx | 4 +- src/Components/Popup/categoryCreation.jsx | 32 +----- .../helpNumberAndCategoryEditPopupContent.jsx | 42 +++++-- src/Components/Popup/helpNumberCreation.jsx | 23 ++-- src/Components/Popup/moodFormCreation.jsx | 12 +- src/Components/Popup/reportCreation.jsx | 11 +- .../Popup/singleAccountCreation.jsx | 48 +++----- src/Components/Sidebar/sidebar.jsx | 8 +- src/Users/Shared/alertsPage.jsx | 73 ++++++------ src/Users/Shared/helpPage.jsx | 4 +- src/Users/Student/dashboardStudent.jsx | 4 +- src/Users/Teacher/dashboardTeacher.jsx | 2 +- src/css/Components/Popup/popup.scss | 43 +++++++- 15 files changed, 249 insertions(+), 176 deletions(-) create mode 100644 src/Components/Popup/alertDeletion.jsx diff --git a/src/Components/Accounts/SchoolAdm/schoolAccountsTable.js b/src/Components/Accounts/SchoolAdm/schoolAccountsTable.js index ce2d2488..bc57e613 100644 --- a/src/Components/Accounts/SchoolAdm/schoolAccountsTable.js +++ b/src/Components/Accounts/SchoolAdm/schoolAccountsTable.js @@ -7,8 +7,9 @@ import DeleteAccountPopupContent from '../../Popup/deleteAccount' import Popup from 'reactjs-popup' import cross from '../../../assets/Cross.png' import minusButton from '../../../assets/minus-button.png' +import Select from 'react-select' -export default function SchoolAccountsTable () { +export default function SchoolAccountsTable ({ rolesList }) { const [teacherList, setTeacherList] = useState([]) const [studentList, setStudentList] = useState([]) const [selectedUser, setSelectedUser] = useState(null) @@ -16,11 +17,15 @@ export default function SchoolAccountsTable () { const [isPopupOpen, setIsPopupOpen] = useState(false) const [fileImage, setFileImage] = useState(null) const [userId, setUserId] = useState('') + const [isMultiStatus, setIsMultiStatus] = useState(true) + const [classesList, setClassesList] = useState([]) const [updatedUser, setUpdatedUser] = useState({ firstname: '', lastname: '', email: '', - picture: null + classes: [], + picture: null, + role: '' }) async function getAccountList () { @@ -46,6 +51,24 @@ export default function SchoolAccountsTable () { setTeacherList(teacherAccounts) setStudentList(studentAccounts) } + + fetch(process.env.REACT_APP_BACKEND_URL + '/shared/classes', { + method: 'GET', + headers: { + 'x-auth-token': sessionStorage.getItem('token'), + 'Content-Type': 'application/json' + } + }) + .then((response) => { + if (response.status === 401) { + disconnect() + } + return response.json() + }) + .then((data) => setClassesList(data)) + .catch((error) => { + toast.error(error.message) + }) } const showClasses = (classes) => { @@ -71,8 +94,11 @@ export default function SchoolAccountsTable () { firstname: user.firstname, lastname: user.lastname, email: user.email, - picture: user.picture + classes: user.classes, + picture: user.picture, + role: user.role._id }) + setIsMultiStatus(user.role.name === "teacher") setIsEditing(true) } @@ -84,27 +110,20 @@ export default function SchoolAccountsTable () { })) } + const handleClassChange = (e) => { + console.log(e) + setUpdatedUser(prevState => ({ + ...prevState, + ["classes"]: e + })) + } + const handleFileChange = (e) => { setFileImage(e.target.files[0]) setUpdatedUser(prevState => ({ ...prevState, picture: e.target.files[0] })) - // const selectedFile = e.target.files[0] - // if (selectedFile) { - // const reader = new FileReader() - // reader.readAsDataURL(selectedFile) - // reader.onload = () => { - // const base64Image = reader.result - // setUpdatedUser(prevState => ({ - // ...prevState, - // picture: base64Image - // })) - // } - // reader.onerror = (error) => { - // console.error('Error occurred while reading the file:', error) - // } - // } } const handleUpdate = async (e) => { @@ -113,11 +132,16 @@ export default function SchoolAccountsTable () { const formData = new FormData() formData.append('firstname', updatedUser.firstname) formData.append('lastname', updatedUser.lastname) + formData.append('role', updatedUser.role) formData.append('email', updatedUser.email) - // if (updatedUser.picture) { - // formData.append('file', updatedUser.picture) - // } - console.log(fileImage) + if (isMultiStatus) { + formData.append('classes', JSON.stringify(updatedUser.classes)) + } else { + const arrayClass = [] + arrayClass.push(updatedUser.classes) + formData.append('classes', JSON.stringify(arrayClass)) + } + if (fileImage) { formData.append('file', fileImage) } @@ -133,13 +157,6 @@ export default function SchoolAccountsTable () { if (response.status === 401) { disconnect() } else if (response.ok) { - // setSelectedUser(null) - // setUpdatedUser({ - // firstname: '', - // lastname: '', - // email: '', - // picture: null - // }) setFileImage(null) toast.success('Le profil a été mis à jour avec succès.') setIsEditing(false) @@ -207,9 +224,9 @@ export default function SchoolAccountsTable () {

Modifier Profil

-
+
- +
- +
- + +
+
+
diff --git a/src/Components/Popup/alertDeletion.jsx b/src/Components/Popup/alertDeletion.jsx new file mode 100644 index 00000000..3b5da800 --- /dev/null +++ b/src/Components/Popup/alertDeletion.jsx @@ -0,0 +1,15 @@ +import React, { useState, useEffect } from 'react' +import '../../css/Components/Popup/popup.scss' +import '../../css/pages/createAlerts.scss' + +const AlertDeletionPopupContent = ({ onClose, chosenAlert, handleDeleteAlert }) => { + return ( + <> +

Êtes-vous certain(e) de vouloir supprimer cette alerte ?

+

Cette action sera irréversible.

+ + + ) +} + +export default AlertDeletionPopupContent diff --git a/src/Components/Popup/alertModification.jsx b/src/Components/Popup/alertModification.jsx index f4cc7841..1c1ad59e 100644 --- a/src/Components/Popup/alertModification.jsx +++ b/src/Components/Popup/alertModification.jsx @@ -2,7 +2,7 @@ import React, { useState, useEffect } from 'react' import '../../css/Components/Popup/popup.scss' import '../../css/pages/createAlerts.scss' -const AlertModificationPopupContent = ({ chosenAlert, handleEditAlert, errMessage }) => { +const AlertModificationPopupContent = ({ onClose, chosenAlert, handleEditAlert, errMessage }) => { const [editedAlert, setEditedAlert] = useState(chosenAlert) useEffect(() => { @@ -28,7 +28,7 @@ const AlertModificationPopupContent = ({ chosenAlert, handleEditAlert, errMessag setFile(e.target.files[0])} /> */} {errMessage ? {errMessage} : ''} - + ) } diff --git a/src/Components/Popup/categoryCreation.jsx b/src/Components/Popup/categoryCreation.jsx index 72ce729d..01901e19 100644 --- a/src/Components/Popup/categoryCreation.jsx +++ b/src/Components/Popup/categoryCreation.jsx @@ -1,23 +1,15 @@ import React, { useState } from 'react' import '../../css/Components/Popup/popup.scss' import { disconnect } from '../../functions/disconnect' +import {toast} from "react-toastify" -const CategoryCreationPopupContent = () => { +const CategoryCreationPopupContent = ({onClose}) => { const [name, setName] = useState('') - const [errMessage, setErrMessage] = useState('') - const [notification, setNotification] = useState({ visible: false, message: '', type: '' }) const handleNameChange = (event) => { setName(event.target.value) } - const openNotification = (message, type) => { - setNotification({ visible: true, message, type }) - setTimeout(() => { - setNotification({ visible: false, message: '', type: '' }) - }, 3000) // La notification sera visible pendant 3 secondes - } - const fetchCategoryRegister = async () => { const categoryRegisterUrl = process.env.REACT_APP_BACKEND_URL + '/adm/helpNumbersCategory/register' @@ -36,24 +28,17 @@ const CategoryCreationPopupContent = () => { disconnect() } if (response.ok) { - setErrMessage('Catégorie créée avec succès.') - openNotification('Catégorie créée avec succès.', 'success') - setTimeout(() => { - window.location.reload() - }, 2000) + toast.success('Catégorie créée avec succès.') } else { const data = await response.json() - setErrMessage(data.message) - openNotification(data.message, 'error') + toast.error(data.message) } }) .catch((error) => { - setErrMessage(error.message) - openNotification(error.message, 'error') + toast.error(error.message) }) } else { - setErrMessage('La catégorie est vide.') - openNotification('La catégorie est vide.', 'error') + toast.error('La catégorie est vide.') } } @@ -63,11 +48,6 @@ const CategoryCreationPopupContent = () => { Catégorie * - {errMessage ? {errMessage} : ''} - {notification.visible && -
- {notification.message} -
} ) diff --git a/src/Components/Popup/helpNumberAndCategoryEditPopupContent.jsx b/src/Components/Popup/helpNumberAndCategoryEditPopupContent.jsx index 6ac29225..f9eb686b 100644 --- a/src/Components/Popup/helpNumberAndCategoryEditPopupContent.jsx +++ b/src/Components/Popup/helpNumberAndCategoryEditPopupContent.jsx @@ -9,6 +9,7 @@ const HelpNumberAndCategoryEditPopupContent = ({ type, onClose }) => { const [selectedItem, setSelectedItem] = useState(null) const [loading, setLoading] = useState(true) const [error, setError] = useState('') + const [categoryList, setCategoryList] = useState([]) useEffect(() => { const fetchItems = async () => { @@ -19,8 +20,6 @@ const HelpNumberAndCategoryEditPopupContent = ({ type, onClose }) => { const numberUrl = process.env.REACT_APP_BACKEND_URL + '/user/helpNumbers' const url = type === 'number' ? numberUrl : categoryUrl - console.log('Fetching data from:', url) - try { const response = await fetch(url, { headers: { @@ -31,18 +30,41 @@ const HelpNumberAndCategoryEditPopupContent = ({ type, onClose }) => { if (response.status === 401) { disconnect() - throw new Error('Unauthorized access') + toast.error('Unauthorized access') } if (!response.ok) { - throw new Error(`Error ${response.status}: ${response.statusText}`) + toast.error(`Error ${response.status}: ${response.statusText}`) } const data = await response.json() setItems(data) + if (type === "number") { + try { + const response = await fetch(categoryUrl, { + headers: { + 'x-auth-token': sessionStorage.getItem('token'), + 'Content-Type': 'application/json' + } + }) + + if (response.status === 401) { + disconnect() + toast.error('Unauthorized access') + } + + if (!response.ok) { + toast.error(`Error ${response.status}: ${response.statusText}`) + } + + const data = await response.json() + setCategoryList(data) + } catch (error) { + toast.error('Error fetching data:', error) + } + } } catch (error) { - console.error('Error fetching data:', error) - setError(error.message) + toast.error('Error fetching data:', error) } finally { setLoading(false) } @@ -71,10 +93,8 @@ const HelpNumberAndCategoryEditPopupContent = ({ type, onClose }) => { if (!response.ok) { const errorData = await response.json() - console.log('Erreur renvoyée par le serveur:', errorData) - throw new Error(`Error ${response.status}: ${errorData.message || 'Unknown error'}`) + toast.error(`Error ${response.status}: ${errorData.message || 'Unknown error'}`) } - toast.success(`${type === 'number' ? 'Numéro' : 'Catégorie'} supprimé avec succès !`) onClose() window.location.reload() // Rafraîchir la page après suppression @@ -89,7 +109,7 @@ const HelpNumberAndCategoryEditPopupContent = ({ type, onClose }) => { const item = items.find((item) => item._id === selectedId) setSelectedItem(item) if (item && type === 'number') { - setFormData({ name: item.name, telephone: item.telephone || '' }) + setFormData({ name: item.name, telephone: item.telephone || '', helpNumbersCategory: item.helpNumbersCategory }) } else { setFormData({ name: item.name }) } @@ -175,7 +195,7 @@ const HelpNumberAndCategoryEditPopupContent = ({ type, onClose }) => {