forked from fga-eps-mds/2024.1-CALCULUS-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from fga-eps-mds/feat#66/subject-access
[TEST] Increase test coverage on web (fga-eps-mds/2024.2-ARANDU-DOC#71)
- Loading branch information
Showing
7 changed files
with
446 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// subjectFunctions.ts | ||
import { Subject } from '@/lib/interfaces/subjetc.interface'; | ||
import { deleteSubjects, GetSubjects, GetSubjectsByUserId } from '@/services/studioMaker.service'; | ||
import { toast } from 'sonner'; | ||
|
||
export const updateSubject = (subject: Subject, listSubjects: Subject[], setListSubjects: React.Dispatch<React.SetStateAction<Subject[]>>) => { | ||
// Verificar se listSubjects não é undefined ou null | ||
if (listSubjects && Array.isArray(listSubjects)) { | ||
setListSubjects( | ||
listSubjects.map((s) => (s._id === subject._id ? subject : s)), | ||
); | ||
} | ||
}; | ||
|
||
export const addSubject = ( | ||
subject: Subject, | ||
listSubjects: Subject[], | ||
setListSubjects: React.Dispatch<React.SetStateAction<Subject[]>> | ||
) => { | ||
// Verificar se listSubjects não é undefined ou null | ||
if (listSubjects && Array.isArray(listSubjects)) { | ||
setListSubjects( | ||
[...listSubjects, subject].sort((a, b) => a.order - b.order), | ||
); | ||
} | ||
}; | ||
|
||
export const handleSubjectAction = ( | ||
action: string, | ||
setEditionDialogOpen: React.Dispatch<React.SetStateAction<boolean>>, | ||
setExclusionDialogOpen: React.Dispatch<React.SetStateAction<boolean>> | ||
) => { | ||
if (action === 'editar') { | ||
setEditionDialogOpen(true); | ||
} | ||
if (action === 'excluir') { | ||
setExclusionDialogOpen(true); | ||
} | ||
}; | ||
|
||
export const handleRemoveSubject = async ( | ||
subject: Subject, | ||
listSubjects: Subject[], | ||
setListSubjects: React.Dispatch<React.SetStateAction<Subject[]>>, | ||
setExclusionDialogOpen: React.Dispatch<React.SetStateAction<boolean>> | ||
) => { | ||
const response = await deleteSubjects({ | ||
id: subject._id, | ||
token: JSON.parse(localStorage.getItem('token')!), | ||
}); | ||
if (response.data) { | ||
toast.success('Disciplina excluída com sucesso!'); | ||
setListSubjects(listSubjects.filter((s) => s._id !== subject._id)); | ||
setExclusionDialogOpen(false); | ||
} else { | ||
toast.error('Erro ao excluir disciplina. Tente novamente mais tarde!'); | ||
} | ||
}; | ||
|
||
export const handleMenuOpen = ( | ||
event: React.MouseEvent<HTMLButtonElement>, | ||
subject: Subject, | ||
setAnchorEl: React.Dispatch<React.SetStateAction<null | HTMLElement>>, | ||
setSelectedSubject: React.Dispatch<React.SetStateAction<Subject | null>> | ||
) => { | ||
setAnchorEl(event.currentTarget); | ||
setSelectedSubject(subject); | ||
}; | ||
|
||
export const fetchSubjects = async ( | ||
params: { pointId: string }, | ||
setListSubjects: React.Dispatch<React.SetStateAction<Subject[]>>, | ||
setFilteredSubjects: React.Dispatch<React.SetStateAction<Subject[]>> | ||
): Promise<Subject[]> => { | ||
let subjects: Subject[]; | ||
|
||
// Verifica qual função chamar baseado no pointId | ||
if (params.pointId == "admin") { | ||
subjects = await GetSubjects(); // Busca todas as disciplinas | ||
} else { | ||
subjects = await GetSubjectsByUserId(params.pointId); // Busca as disciplinas pelo userId | ||
} | ||
|
||
// Ordena os subjects pelo campo 'order' | ||
subjects.sort((a, b) => a.order - b.order); | ||
|
||
// Atualiza os estados com as disciplinas ordenadas | ||
setListSubjects(subjects); | ||
setFilteredSubjects(subjects); | ||
|
||
return subjects; | ||
}; | ||
|
||
export const handleMenuClose = (setAnchorEl: React.Dispatch<React.SetStateAction<null | HTMLElement>>,) => { | ||
setAnchorEl(null); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.