fixing teacher manage student

This commit is contained in:
Dimas Atmodjo 2024-12-16 00:40:37 +07:00
parent 866b542e12
commit df69cd3467
2 changed files with 48 additions and 31 deletions

View File

@ -22,6 +22,7 @@ const useStudents = () => {
const [loaderState, setLoaderState] = useState({ loading: false, successMessage: '', title: '', description: '', confirmAction: false }); const [loaderState, setLoaderState] = useState({ loading: false, successMessage: '', title: '', description: '', confirmAction: false });
const resetForm = () =>{ const resetForm = () =>{
setFileImport(null);
setFormData({ setFormData({
fullName: '', fullName: '',
nisn: '', nisn: '',
@ -41,15 +42,19 @@ const useStudents = () => {
}; };
const createdStudent = await studentService.createData(newStudent); const createdStudent = await studentService.createData(newStudent);
setStudents((prevStudents) => [...prevStudents, createdStudent.payload]); setStudents((prevStudents) => [...prevStudents, createdStudent.payload]);
} catch (err) {
setError(err);
}finally{
resetForm(); resetForm();
setLoaderState(prev => ({ setLoaderState(prev => ({
...prev, ...prev,
loading: false, loading: false,
successMessage: 'Your new entry has been successfully created and saved.' successMessage: 'Your new entry has been successfully created and saved.'
})); }));
} catch (err) {
setLoaderState(prev => ({
...prev,
title: "ERROR",
loading: false,
successMessage: err.response.data.message
}));
} }
}; };
@ -60,16 +65,19 @@ const useStudents = () => {
console.log(file); console.log(file);
try { try {
const response = await studentService.registerImport(fileData); const response = await studentService.registerImport(fileData);
console.log(response.data);
} catch (err) {
setError(err);
}finally{
resetForm();
setLoaderState(prev => ({ setLoaderState(prev => ({
...prev, ...prev,
loading: false, loading: false,
successMessage: 'Your new entry has been successfully created and saved.' successMessage: 'Your new entry has been successfully created and saved.'
})); }));
resetForm();
} catch (err) {
setLoaderState(prev => ({
...prev,
title: "ERROR",
loading: false,
successMessage: err.response.data.message
}));
} }
} }
@ -83,14 +91,18 @@ const useStudents = () => {
setStudents((prevStudents) => setStudents((prevStudents) =>
prevStudents.map((s) => (s.ID === id ? student.payload : s)) prevStudents.map((s) => (s.ID === id ? student.payload : s))
); );
} catch (err) {
setError(err);
}finally{
setLoaderState(prev => ({ setLoaderState(prev => ({
...prev, ...prev,
loading: false, loading: false,
successMessage: 'Your data has been successfully updated.' successMessage: 'Your data has been successfully updated.'
})); }));
} catch (err) {
setLoaderState(prev => ({
...prev,
title: "ERROR",
loading: false,
successMessage: err.response.data.message
}));
} }
}; };
@ -98,15 +110,19 @@ const useStudents = () => {
handleShowLoader('Deleted', '', true) handleShowLoader('Deleted', '', true)
try { try {
await studentService.deleteData(id); await studentService.deleteData(id);
} catch (err) {
setError(err);
}finally{
setStudents((prevStudents) => prevStudents.filter((s) => s.ID !== id)); setStudents((prevStudents) => prevStudents.filter((s) => s.ID !== id));
setLoaderState(prev => ({ setLoaderState(prev => ({
...prev, ...prev,
loading: false, loading: false,
successMessage: 'Your data has been successfully deleted.' successMessage: 'Your data has been successfully deleted.'
})); }));
} catch (err) {
setLoaderState(prev => ({
...prev,
title: "ERROR",
loading: false,
successMessage: err.response.data.message
}));
} }
}; };
@ -175,22 +191,23 @@ const useStudents = () => {
}, [page, limit]); }, [page, limit]);
const handleDownloadTemplate = async () => {try { const handleDownloadTemplate = async () => {
const response = await studentService.getTemplate(); try {
const response = await studentService.getTemplate();
const blob = new Blob([response], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
const downloadUrl = URL.createObjectURL(blob); const blob = new Blob([response], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
const downloadUrl = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = downloadUrl; const link = document.createElement('a');
link.download = 'template.xls'; link.href = downloadUrl;
link.click(); link.download = 'template.xlsx';
link.click();
URL.revokeObjectURL(downloadUrl);
} catch (error) { URL.revokeObjectURL(downloadUrl);
console.error('Error downloading file:', error); } catch (error) {
alert('Gagal mengunduh file'); console.error('Error downloading file:', error);
} alert('Gagal mengunduh file');
}
} }
return { return {

View File

@ -377,7 +377,7 @@ const ManageStudents = () => {
onDragLeave={handleDragLeave} onDragLeave={handleDragLeave}
> >
<h5 className={`m-0 ${fileImport ? `text-green fs-3` : `text-grey`}`}>{fileImport ? fileImport.name : "Upload Excel file here"}</h5> <h5 className={`m-0 ${fileImport ? `text-green fs-3` : `text-grey`}`}>{fileImport ? fileImport.name : "Upload Excel file here"}</h5>
<span className={`fs-14p ${fileImport ? `text-green` : `text-grey`}`}>{fileImport ? "Your file is ready to import" : "Please make sure your Excel file follows the template."}</span> <span className={`fs-14p ${fileImport ? `text-green` : `text-grey`}`}>{fileImport ? "Your file is ready to import" : "Please make sure your Excel file follows the template.(*.xlsx)"}</span>
</label> </label>
</Row> </Row>
<div className="d-flex justify-content-end"> <div className="d-flex justify-content-end">