Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gustaallves committed Dec 6, 2023
2 parents e509fb7 + 8aa9040 commit 511228b
Show file tree
Hide file tree
Showing 4 changed files with 294 additions and 176 deletions.
3 changes: 2 additions & 1 deletion backend/account/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
processar_horario,
selecionar_materia,
mandar_materiaView,
remover_materias,
remover_materia,
)

Expand All @@ -24,6 +25,6 @@
path('selecionar_materia/', selecionar_materia, name='selecionar_materia'), # Rota para seleção de matéria
path('mandar_materias/', mandar_materiaView.as_view(), name='mandar_materia'),
path('remover_materia/', remover_materia, name='remover_materia'),

path('remover_materias/', remover_materias, name='remover_materias'),

]
48 changes: 34 additions & 14 deletions backend/account/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,24 @@ def processar_horario(request):
# View para selecionar matéria
@api_view(['POST'])
def selecionar_materia(request):
serializer = SelecionarMateriaSerializer(data=request.data)
if serializer.is_valid():
id_turma_professor = serializer.validated_data['idTurmaProfessor']
if request.method == 'POST':
data = request.data
id_turmas_professor = data.get('idTurmaProfessor', [])

# Busque os dados correspondentes no arquivo JSON
materia_json = obter_dados_do_json(id_turma_professor)
for id_turma_professor in id_turmas_professor:
# Sua lógica para obter dados correspondentes no arquivo JSON
materia_json = obter_dados_do_json(id_turma_professor)

if materia_json:
# Crie uma instância do modelo e salve no banco de dados
materia_selecionada = MateriaSelecionada(**materia_json)
materia_selecionada.save()
if materia_json:
# Sua lógica para criar uma instância do modelo e salvar no banco de dados
materia_selecionada = MateriaSelecionada(**materia_json)
materia_selecionada.save()

return Response({'status': 'Matérias selecionadas com sucesso!'})
return Response({'status': 'erro', 'message': 'Método não permitido'}, status=405)

return Response({'status': 'Matéria selecionada com sucesso!'})
else:
return Response({'erro': 'ID de turma professor não encontrado no JSON'}, status=404)
return Response(serializer.errors, status=400)

#remove as amterias da grade
def remover_materia(request):
materia_id = request.data.get('materia_id', None)

Expand All @@ -147,6 +148,7 @@ def remover_materia(request):

return Response({'status': 'erro', 'message': 'ID da matéria não fornecido'}, status=400)


# Função auxiliar para obter dados do JSON
def obter_dados_do_json(id_turma_professor):
# Abra o arquivo JSON e procure pelo id_turma_professor
Expand All @@ -166,4 +168,22 @@ def get(self, request):
dados = MateriaSelecionada.objects.all()
serializer = MandarMateriaSerializer(dados, many=True)

return Response(serializer.data)
return Response(serializer.data)

#remove a materia selecionada
@api_view(['POST'])
def remover_materias(request):
ids_turma_professor = request.data.get('idsTurmaProfessor', [])

try:
# Adicione essas linhas para depuração
print(f'Tentando remover matérias com IDs: {ids_turma_professor}')

# Remova as matérias do banco de dados
MateriaSelecionada.objects.filter(idTurmaProfessor__in=ids_turma_professor).delete()
return Response({'message': 'Matérias removidas com sucesso'}, status=status.HTTP_200_OK)
except Exception as e:
# Adicione essas linhas para depuração
print(f'Erro ao remover matérias: {str(e)}')

return Response({'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
202 changes: 121 additions & 81 deletions frontend/src/pages/Grade.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,67 @@ import axios from 'axios';
import Navbar from "../components/Navbar";
import Grade from '../styleheets/Grade.css'

const myObjectCinco = [
"35T23", "46M34", "24M34","35M12","46T23","35M34","46M12","35T45",
"24M12","26T45", "26M34","24T45","25T23", "26T23", "24T23","46T45",
"26M12","35M12",
];
// const myObjectCinco = [
// "35T23", "46M34", "24M34",
// "35M12","46T23","35M34",
// "46M12","35T45",
// "24M12","26T45", "26M34"
// ,"24T45","25T23", "26T23",
// "24T23","46T45", "26M12",
// ];


const myObject = {
"35T23": [{"id": "3T2"}, {"id": "3T3"}, {"id": "5T2"}, {"id": "5T3"}],
"46M34": [{"id": "4M3"}, {"id": "4M4"}, {"id": "6M3"}, {"id": "6M4"}],
"24M34": [{"id": "2M3"}, {"id": "2M4"}, {"id": "4M3"}, {"id": "4M4"}],
"35M12": [{"id": "3M1"}, {"id": "3M2"}, {"id": "5M1"}, {"id": "5M2"}],
"46T23": [{"id": "4T2"}, {"id": "4T3"}, {"id": "6T2"}, {"id": "6T3"}],
"35M34": [{"id": "3M3"}, {"id": "3M4"}, {"id": "5M3"}, {"id": "5M4"}],
"46M12": [{"id": "4M1"}, {"id": "4M2"}, {"id": "6M1"}, {"id": "6M2"}],
"35T45": [{"id": "3T4"}, {"id": "3T5"}, {"id": "5T4"}, {"id": "5T5"}],
"24M12": [{"id": "2M1"}, {"id": "2M2"}, {"id": "4M1"}, {"id": "4M1"}],
"26T45": [{"id": "2T4"}, {"id": "2T5"}, {"id": "6T4"}, {"id": "6T5"}],
"26M34": [{"id": "2M3"}, {"id": "2M4"}, {"id": "6M3"}, {"id": "6M4"}],
"24T45": [{"id": "2T4"}, {"id": "2T5"}, {"id": "4T4"}, {"id": "4T5"}],
"25T23": [{"id": "2T2"}, {"id": "2T3"}, {"id": "5T2"}, {"id": "5T3"}],
"26T23": [{"id": "2T2"}, {"id": "2T3"}, {"id": "6T2"}, {"id": "6T3"}],
"24T23": [{"id": "2T2"}, {"id": "2T3"}, {"id": "4T2"}, {"id": "4T3"}],
"46T45": [{"id": "4T4"}, {"id": "4T5"}, {"id": "6T4"}, {"id": "6T5"}],
"26M12": [{"id": "2M1"}, {"id": "2M2"}, {"id": "6M1"}, {"id": "6M2"}],
"4M1": [{ "id": "4M1" }],
"35T6 35N1": [{ "id": "3T5" }, { "id": "3N1" }],
"24M5 24T1": [{ "id": "2M5" }, { "id": "2T1" }, { "id": "4M5" }, { "id": "4T1" }],
"46T23": [{ "id": "4T2" }, { "id": "4T3" }, { "id": "6T2" }, { "id": "6T3" }],
"2M12": [{ "id": "2M1" }, { "id": "2M2" }],
"3M2": [{ "id": "3M2" }],
"235M5 235T1": [{ "id": "2M5" }, { "id": "3M5" }, { "id": "5M5" }, { "id": "2T1" }, { "id": "3T1" }, { "id": "5T1" }],
"246M12": [{ "id": "2M1" }, { "id": "2M2" }, { "id": "4M1" }, { "id": "4M2" }, { "id": "6M1" }, { "id": "6M2" }],
"4M345 6M34": [{ "id": "4M3" }, { "id": "4M4" }, { "id": "4M5" }, { "id": "6M3" }, { "id": "6M4" }],
"235M12": [{ "id": "2M1" }, { "id": "2M2" }, { "id": "3M1" }, { "id": "3M2" }, { "id": "5M1" }, { "id": "5M2" }],
"26T45": [{ "id": "2T4" }, { "id": "2T5" }, { "id": "6T4" }, { "id": "6T5" }],
"3M34": [{ "id": "3M3" }, { "id": "3M4" }],
"35M5 35T1": [{ "id": "3M5" }, { "id": "5M5" }, { "id": "3T1" }, { "id": "5T1" }],
"35T45 6T4": [{ "id": "3T4" }, { "id": "3T5" }, { "id": "5T4" }, { "id": "5T5" }, { "id": "6T4" }],
"4T2345": [{ "id": "4T2" }, { "id": "4T3" }, { "id": "4T4" }, { "id": "4T5" }],
"235M5 235T1": [{ "id": "2M5" }, { "id": "3M5" }, { "id": "5M5" }, { "id": "2T1" }, { "id": "3T1" }, { "id": "5T1" }],
"46M12": [{ "id": "4M1" }, { "id": "4M2" }, { "id": "6M1" }, { "id": "6M2" }],
"4T45": [{ "id": "4T4" }, { "id": "4T5" }],
"235T45": [{ "id": "2T4" }, { "id": "2T5" }, { "id": "3T4" }, { "id": "3T5" }, { "id": "5T4" }, { "id": "5T5" }],
"24M34": [{ "id": "2M3" }, { "id": "2M4" }, { "id": "4M3" }, { "id": "4M4" }],
"4T4":[{id:"4T4"}],
"3M12":[{id: "3M1"},{id: "3M2"}],
"5M12":[{id: "5M1"}, {id: "5M2"}],
"4T23":[{id: "4T2" }, {id: "4T3" }],
"246T45":[{id: "2T4" },{id: "2T5" },{id: "4T4" },{id: "4T5" },{id: "6T4" },{id: "6T5" }],
"4M34":[{id: "4M3" },{id: "4M4" }],
"7T12345":[{id: "7T1" },{id: "7T2" },{id: "7T3" },{id: "7T4" },{id: "7T5" }],
"356M12": [{ id: "3M1" }, { id: "3M2" },{ id: "5M1" }, { id: "5M2" },{ id: "6M1" }, { id: "6M2" }],
"5M34": [{ id: "5M3" }, { id: "5M4" }],
"26M34 4M4": [{ id: "2M3" },{ id: "2M4" }, { id: "6M3" },{ id: "6M4" }, { id: "4M4" }],
"2M5 35M12": [{ id: "2M5" }, { id: "3M1" }, { id: "3M2" },{ id: "5M1" }, { id: "5M2" }],
"35T45": [{ id: "3T4" }, { id: "3T5" },{ id: "5T4" }, { id: "5T5" }],
"46M34": [{ id: "4M3" }, { id: "4M4" },{ id: "6M3" }, { id: "6M4" }],
"4M2": [{ id: "4M2" }],
"6M12": [{ id: "6M1" }, { id: "6M2" }],
"24T23": [{ id: "2T2" }, { id: "2T3" },{ id: "4T2" }, { id: "4T3" }],
"4M12": [{ id: "4M1" }, { id: "4M2" }],
"46M34": [{ id: "4M3" }, { id: "4M4" },{ id: "6M3" }, { id: "6M4" }],
"3M5 3T1": [{ id: "3M5" }, { id: "3T1" }],
"2T23": [{ id: "2T2" }, { id: "2T3" }],
"2T45": [{ id: "2T4" }, { id: "2T5" }],
"4T4":[{"id":"4T4"}],
"3M12":[{"id": "3M1"},{"id": "3M2"}],
"5M12":[{"id": "5M1"}, {"id": "5M2"}],
"4T23":[{"id": "4T2" }, {"id": "4T3" }],
"246T45":[{"id": "2T4" },{"id": "2T5" },{"id": "4T4" },{"id": "4T5" },{"id": "6T4" },{"id": "6T5" }],
"4M34":[{"id": "4M3" },{"id": "4M4" }],
"7T12345":[{"id": "7T1" },{"id": "7T2" },{"id": "7T3" },{"id": "7T4" },{"id": "7T5" }],
"356M12": [{ "id": "3M1" }, { "id": "3M2" },{ "id": "5M1" }, { "id": "5M2" },{ "id": "6M1" }, { "id": "6M2" }],
"5M34": [{ "id": "5M3" }, { "id": "5M4" }],
"26M34 4M4": [{ "id": "2M3" },{ "id": "2M4" }, { "id": "6M3" },{ "id": "6M4" }, { "id": "4M4" }],
"2M5 35M12": [{ "id": "2M5" }, { "id": "3M1" }, { "id": "3M2" },{ "id": "5M1" }, { "id": "5M2" }],
"4M2": [{ "id": "4M2" }],
"6M12": [{ "id": "6M1" }, { "id": "6M2" }],
"4M12": [{ "id": "4M1" }, { "id": "4M2" }],
"3M5 3T1": [{ "id": "3M5" }, { "id": "3T1" }],
"2T23": [{ "id": "2T2" }, { "id": "2T3" }],
"2T45": [{ "id": "2T4" }, { "id": "2T5" }],
"24T6 24N1": [{ "id": "2T6" },{ "id": "4T6" }, { "id": "2N1" }, { "id": "4N1" }],
"35T45 6T4": [{ "id": "3T4" }, { "id": "3T5" },{ "id": "5T4" }, { "id": "5T5" },{ "id": "6T4" }],
"2M5 35M12": [{ "id": "2M5" }, { "id": "3M1" },{ "id": "3M2" },{ "id": "5M1" },{ "id": "5M2" }],
Expand Down Expand Up @@ -92,30 +104,16 @@ const myObject = {
"246M5 246T1": [{ "id": "2M5" }, { "id": "4M5" },{ "id": "6M5" }, { "id": "2T1" }, { "id": "4T1" },{ "id": "6T1" },],
"235M5 235T1": [{ "id": "2M5" }, { "id": "3M5" }, { "id": "5M5" }, { "id": "2T1" }, { "id": "3T1" }, { "id": "5T1" }],
"246M12": [{ "id": "4M1" },{ "id": "4M2" }, { "id": "2M1" },{ "id": "2M2" },{ "id": "6M1" },{ "id": "6M2" }],
"46T6 46N1": [{ "id": "4T6" }, { "id": "4N1" },{ "id": "6T6" }, { "id": "6N1" }]
"46T6 46N1": [{ "id": "4T6" }, { "id": "4N1" },{ "id": "6T6" }, { "id": "6N1" }],
"3T23 5T2" : [{"id" : "3T2"}, {"id" : "3T3"}, {"id" : "5T2"}]
}


function cincoChar(horario) {
const var1 = horario[0] + horario[2] + horario[3];
const var2 = horario[0] + horario[2] + horario[4];
const var3 = horario[1] + horario[2] + horario[3];
const var4 = horario[1] + horario[2] + horario[4];

return {
var1,
var2,
var3,
var4,
};
}


const MyTableComponent = () => {


const [valoresBack, setValoresBack] = useState([]);

const [materiasSelecionadas, setMateriasSelecionadas] = useState([]);

useEffect(() => {
axios.get('http://127.0.0.1:8000/api/user/mandar_materias')
.then(response => {
Expand All @@ -127,19 +125,64 @@ const MyTableComponent = () => {
});
}, []);


// const handleAdicionarSelection = (materia) => {
// // Adicione a matéria à lista de matérias selecionadas
// setMateriasSelecionadas((prevMaterias) => [...prevMaterias, materia]);
// };

// const handleCancelarSelection = (idTurmaProfessor) => {
// // Remova a matéria da lista de matérias selecionadas
// setMateriasSelecionadas((prevMaterias) =>
// prevMaterias.filter((materia) => materia.idTurmaProfessor !== idTurmaProfessor)
// );
// };

const handleRemoverTodas = () => {
// Remova todas as matérias da lista de matérias selecionadas do banco de dados
const idsTurmaProfessor = materiasSelecionadas.map((materia) => materia.idTurmaProfessor);

// Faça uma chamada à API para remover as matérias do banco de dados
axios
.post('http://127.0.0.1:8000/api/user/remover_materias/', { idsTurmaProfessor })
.then((response) => {
console.log('Matérias removidas com sucesso do banco de dados:', response.data);
// Limpe a lista de matérias selecionadas após a remoção bem-sucedida
setMateriasSelecionadas([]);
// Atualize a página automaticamente
window.location.reload();
})
.catch((error) => {
console.error('Erro ao remover matérias do banco de dados:', error);
});
};

const handleCheckboxChange = (idTurmaProfessor) => {
// Verifique se a matéria já está na lista de selecionadas
const isAlreadySelected = materiasSelecionadas.some((materia) => materia.idTurmaProfessor === idTurmaProfessor);

if (isAlreadySelected) {
// Se já estiver selecionada, remova da lista
setMateriasSelecionadas((prevMaterias) =>
prevMaterias.filter((materia) => materia.idTurmaProfessor !== idTurmaProfessor)
);
} else {
// Se não estiver selecionada, adicione à lista
const materia = valoresBack.find((materia) => materia.idTurmaProfessor === idTurmaProfessor);
if (materia) {
setMateriasSelecionadas((prevMaterias) => [...prevMaterias, materia]);
}
}
};

//pegar as materias != 5 char e mandar para a tabela
useEffect(() => {
if (valoresBack.length > 0) {
const keys = Object.keys(myObject);

valoresBack.forEach(item => {
const horario = item.horario;


const matchingKey = keys.find(key => key.includes(horario));



if (matchingKey) {
const materiaIds = myObject[matchingKey].map(obj => obj.id);

Expand All @@ -160,34 +203,6 @@ const MyTableComponent = () => {
}, [valoresBack]);


//pegar as materias com 5 char e mandar para a tabela
useEffect(() => {
valoresBack.forEach(item => {
const horarioDoBanco = item.horario;
console.log(`horarioDoBanco: ${horarioDoBanco}`);
if (myObjectCinco.includes(horarioDoBanco)) {
const { var1, var2, var3, var4 } = cincoChar(horarioDoBanco);
const idsToCompare = [var1, var2, var3, var4];
console.log(`idsToCompare: ${idsToCompare}`);



idsToCompare.forEach(idToCompare => {
// Construa o identificador da célula correspondente
const cellIdToCompare = `${idToCompare}`;
console.log(`cellIdToCompare: ${cellIdToCompare}`);
// Obtenha a célula correspondente da tabela
const cellToCompare = document.getElementById(cellIdToCompare);
console.log(`cellToCompare: ${cellToCompare}`);

if (cellToCompare) {
cellToCompare.innerHTML = item.nomeMateria;
}
});
}
});
}, [valoresBack]);

return (
<div>
<Navbar />
Expand Down Expand Up @@ -317,6 +332,31 @@ const MyTableComponent = () => {
</tbody>
</table>

{valoresBack.length > 0 && (
<div className="selected-materias">
<h2>Matérias Selecionadas</h2>
<ul>
{valoresBack.map((materia) => (
<li className="bloco" key={materia.idTurmaProfessor}>
<label>
{materia.nomeMateria} - {materia.nomeProfessor}
<input
type="checkbox"
checked={materiasSelecionadas.some((m) => m.idTurmaProfessor === materia.idTurmaProfessor)}
onChange={() => handleCheckboxChange(materia.idTurmaProfessor)}
/>
</label>
</li>
))}
</ul>
{valoresBack.length > 0 && (
<button onClick={handleRemoverTodas} className="cancel-button">
Remover Todas do Banco de Dados
</button>
)}
</div>
)}

</div>
);
}
Expand Down
Loading

0 comments on commit 511228b

Please sign in to comment.