Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

v1.3.0 #46

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/src/pages/PF/ClassEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,9 @@ function ClassEdit() {
})
}else{
withReactContent(Swal).fire({
title: "Error!",
icon: "error",
text: responseData["msg"]
title: "Error!",
icon: "error",
text: responseData["msg"]
})
}
} catch (error) {
Expand Down
262 changes: 173 additions & 89 deletions src/src/pages/PF/StudentList.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import Swal from 'sweetalert2'
import withReactContent from 'sweetalert2-react-content';

import React, { useState, useEffect } from 'react';
import Navbar from '../../components/Navbar';
import { useNavigate } from 'react-router-dom';
import Cookies from 'js-cookie';
// import {PencilSquare} from 'react-bootstrap-icons'
import {PencilSquare} from 'react-bootstrap-icons'

const host = `${process.env.REACT_APP_HOST}`

function StudentList() {
const navigate = useNavigate();

const [id, ] = useState(Cookies.get('uid'))
const [searchQuery, setSearchQuery] = useState('');
const [showname, setshowname] = useState([])

Expand All @@ -19,16 +23,33 @@ function StudentList() {

const [showModal, setShowModal] = useState(false);
const [isEdit, setIsEdit] = useState(false);
const [gonEdit, setGonEdit] = useState("");

const [ID, setID] = useState("");
const [name, setName] = useState("");
const [group, setGroup] = useState("");
const [section, setSection] = useState("");
const [UID,setUID] = useState("");
const [Name,setName] = useState("");
const [Section,setSection] = useState("");
const [Group,setGroup] = useState("");

// const [Email,] = useState(sessionStorage.getItem("Email"));
// const [Email,] = useState(Cookies.get("email"));
const [classId,] = useState(sessionStorage.getItem("classId"));

const fetchName = async () => {
try {
const response = await fetch(`${host}/TA/Student/List?CSYID=${classId}`, {
method: "GET",
credentials: "include",
headers: {
"Content-type": "application/json; charset=UTF-8",
"Access-Control-Allow-Origin": "*",
"X-CSRF-TOKEN": Cookies.get("csrf_token")
}
});
const dataname = await response.json();
setshowname(dataname["data"]["Students"]);
} catch (error) {
console.error('Error fetching user data:', error);
// Display an error message to the user
}
};

useEffect(() => {
const fetchClass = async () => {
try {
Expand Down Expand Up @@ -66,25 +87,6 @@ function StudentList() {
}
};

const fetchName = async () => {
try {
const response = await fetch(`${host}/TA/Student/List?CSYID=${classId}`, {
method: "GET",
credentials: "include",
headers: {
"Content-type": "application/json; charset=UTF-8",
"Access-Control-Allow-Origin": "*",
"X-CSRF-TOKEN": Cookies.get("csrf_token")
}
});
const dataname = await response.json();
setshowname(dataname["data"]["Students"]);
} catch (error) {
console.error('Error fetching user data:', error);
// Display an error message to the user
}
};

fetchClass();
fetchSection();
fetchName();
Expand Down Expand Up @@ -135,44 +137,126 @@ function StudentList() {
}

const handleEditStudent = async (toEdit) => {
console.log(toEdit)
setIsEdit(true);
setID(toEdit["ID"]);
setName(toEdit["Name (English)"]);
setSection(toEdit["Section"]);
setGroup(toEdit["Group"]);
console.log(toEdit["ID"]);
setShowModal(true);
document.getElementById("SID").disabled = true;

}

const handleChangeGonEdit = async (k, v) => {
var x = gonEdit
x[k] = v
console.log(x)
setGonEdit(x)
setUID(toEdit["ID"])
setName(toEdit["Name (English)"])
setSection(toEdit["Section"])
setGroup(toEdit["Group"])
setShowModal(true);
setIsEdit(true);
}

const handleAddStudent = async () => {
setShowModal(true);
setIsEdit(false);
document.getElementById("SID").disabled = false
setID("");
setName("");
setGroup("");
setSection("");
setUID("")
setName("")
setSection("")
setGroup(showname.length > 0 ? (showname[0]["Group"] === '-' ? '-' : '') : '')
setShowModal(true);
setIsEdit(false)
}

const UpdateStudent = async () => {
const RemoveStudent = async () => {
const bd = JSON.stringify({
"SID": UID,
"CSYID": classId
})

try {
withReactContent(Swal).fire({
html: `<div class="pos-center">
<div class="loader"></div>
</div> `,
showCloseButton: false,
showCancelButton: false,
showConfirmButton: false,
background: "rgba(0, 0, 0, 0)"
})
const response = await fetch(`${host}/TA/Student/remove`, {
method: 'POST',
credentials: "include",
headers: {
"Content-type": "application/json; charset=UTF-8",
"Access-Control-Allow-Origin": "*",
"X-CSRF-TOKEN": Cookies.get('csrf_token')
},
body: bd
});
const responseData = await response.json();
withReactContent(Swal).close()
if (responseData["success"]){
withReactContent(Swal).fire({
title: responseData["msg"],
icon: "success"
})
fetchName()
}else{
withReactContent(Swal).fire({
title: "Error!",
icon: "error",
text: responseData["msg"]
})
}
} catch (error) {
console.error('Error exporting data:', error);
withReactContent(Swal).close()
withReactContent(Swal).fire({
title: "There is error!",
icon: "error"
})
}
}

const RemoveStudent = async () => {

}
const SaveStudent = async () => {
const bd = JSON.stringify({
"SID": UID,
"Name": Name,
"Section": Section,
"Group": Group,
"CSYID": classId
})

const AddStudent = async () => {
try {
withReactContent(Swal).fire({
html: `<div class="pos-center">
<div class="loader"></div>
</div> `,
showCloseButton: false,
showCancelButton: false,
showConfirmButton: false,
background: "rgba(0, 0, 0, 0)"
})
const response = await fetch(`${host}/TA/Student/${isEdit?"edit":"add"}`, {
method: 'POST',
credentials: "include",
headers: {
"Content-type": "application/json; charset=UTF-8",
"Access-Control-Allow-Origin": "*",
"X-CSRF-TOKEN": Cookies.get('csrf_token')
},
body: bd
});
const responseData = await response.json();
withReactContent(Swal).close()
if (responseData["success"]){
withReactContent(Swal).fire({
title: responseData["msg"],
icon: "success"
})
fetchName()
}else{
withReactContent(Swal).fire({
title: "Error!",
icon: "error",
text: responseData["msg"]
})
}
} catch (error) {
console.error('Error exporting data:', error);
withReactContent(Swal).close()
withReactContent(Swal).fire({
title: "There is error!",
icon: "error"
})
}

}

Expand Down Expand Up @@ -213,7 +297,7 @@ function StudentList() {
<li className="nav-item">
<button className="nav-link active">Student List</button>
</li>
{/* <button style={{marginLeft: "1.5rem"}} className="btn btn-outline-success" type="button" id="button-addon2" onClick={() => handleAddStudent()} >+ Add</button> */}
<button style={{marginLeft: "1.5rem"}} className="btn btn-outline-success" type="button" id="button-addon2" onClick={() => handleAddStudent()} >+ Add</button>
</ul>
</div>
<div className="col-md-2">
Expand Down Expand Up @@ -255,7 +339,7 @@ function StudentList() {
<th scope="col" className="col-1 text-center">Section</th>
<th scope="col" className="col-1 text-center">Group</th>
<th scope="col" className="col-1 text-center">Score</th>
{/* <th scope="col" className="col-1 text-center">Edit</th> */}
<th scope="col" className="col-1 text-center">Edit</th>
</tr>
</thead>
<tbody>
Expand All @@ -272,7 +356,7 @@ function StudentList() {
<td className='text-center'>{element["Section"]}</td>
<td className='text-center'>{element["Group"]}</td>
<td className='text-center'>{element["Score"]}/{element["MaxScore"]}</td>
{/* <td className='text-center'><button type="button" className="btn btn-warning" onClick={() => {handleEditStudent(element)}}><PencilSquare/></button></td> */}
{id !== element["ID"] ? (<td className='text-center'><button type="button" className="btn btn-warning" onClick={() => {handleEditStudent(element)}}><PencilSquare/></button></td>):(null)}
</tr>
))
) : (
Expand All @@ -289,6 +373,11 @@ function StudentList() {
<br />
</div>
</div>





<div className={`modal fade ${showModal ? 'show' : ''}`} tabIndex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true" style={{ display: showModal ? 'block' : 'none' }}>
<div className="modal-dialog">
<div className="modal-content">
Expand All @@ -297,42 +386,37 @@ function StudentList() {
<button type="button" className="btn-close" onClick={handleCloseModal} aria-label="Close"></button>
</div>
<div className="modal-body">
<form>
<div className="form-group">
<label htmlFor="studentId">Student ID</label>
<input type="text" className="form-control" id="SID" placeholder="Student ID" value={ID} onChange={(e) => {setID(e.target.value)}}/>
</div>
<div className="form-group">
<label htmlFor="exampleInputEmail1">Name</label>
<input type="text" className="form-control" id="nameEdit" placeholder="Enter name" value={name} onChange={(e) => {setName(e.target.value)}}/>
</div>
<div className="form-group">
<label htmlFor="exampleInputEmail1">Section</label>
<input type="text" className="form-control" id="sectionEdit" placeholder="Enter section" value={section} onChange={(e) => {setSection(e.target.value)}}/>
</div>
<div className="form-group">
<label htmlFor="exampleInputEmail1">Group</label>
<input type="text" className="form-control" id="groupEdit" placeholder="Enter group" value={group} onChange={(e) => {setGroup(e.target.value)}}/>
</div>
</form>

<form>
<div className="form-group">
<label>Student ID</label>
<input type="text" className="form-control" placeholder="Student ID" value={UID} onChange={(e) => {setUID(e.target.value)}} disabled={isEdit}/>
</div>
<div className="form-group">
<label>Name</label>
<input type="text" className="form-control" placeholder="Name" value={Name} onChange={(e) => {setName(e.target.value)}}/>
</div>
<div className="form-group">
<label>Section</label>
<input type="text" className="form-control" placeholder="Section" value={Section} onChange={(e) => {setSection(e.target.value)}}/>
</div>
<div className="form-group">
<label>Group</label>
<input type="text" className="form-control" placeholder="Group" value={Group} onChange={(e) => {setGroup(e.target.value)}} disabled={Group === "-"}/>
</div>
</form>
</div>
<div className="modal-footer" style={{justifyContent: "flex-start"}}>
<button type="button" className="btn btn-outline-secondary" onClick={handleCloseModal}>
Cancel
</button>
{isEdit ? (
<button type="button" className="btn btn-danger" onClick={handleCloseModal}>
Remove
</button>
):(
""
)}
<button type="button" className="btn btn-success" onClick={handleCloseModal}>
Save
{isEdit ? (
<button type="button" className="btn btn-danger" onClick={RemoveStudent}>
Remove
</button>
):(null)}
<button type="button" className="btn btn-success" onClick={SaveStudent}>
{isEdit ? "Save" : "Add"}
</button>


</div>
</div>
</div>
Expand Down
Loading