import React, {Component} from "react"; import {Link} from "react-router-dom"; import {Row, Col} from "react-bootstrap" import {getJson} from "./Request"; /** Module de recherche d'étudiant */ class SearchStudent extends Component { constructor(props) { super(props); this.state = { students: [], // Status possibles: // 0: Vide - 1: Pas de resultat - 2: Un ou plusieurs resultats search_status: 0, }; this.handleChangeSearch = this.handleChangeSearch.bind(this) this.searchStudent = this.searchStudent.bind(this); } handleChangeSearch(e) { this.setState({ search: e.target.value }); } /** * Lance une recherche de l'étudiant depuis l'API * @param search {String} - Texte recherché */ searchStudent(search) { let dept = window.location.href.split('/')[7] let BASE_URL = window.$api_url getJson(BASE_URL + dept + '/Scolarite/Notes/search_etud_by_name?term=' + search +'&format=json') .then(res => { this.setState({ students: res.data }); if (this.state.students.length === 0) { this.setState({search_status: 1, toast: true}); } else { this.setState({search_status: 2, toast: false}); } }) this.setState({searched: true}) } /** * Presentation du résultat * @returns {JSX.Element} - Resultat au format JSX */ result() { if (this.state.toast === true) { return (
Aucun étudiant trouvé
) } else if (this.state.search_status === 2) { return ( {this.state.students.map((student) => { return ( {student.label} ) })} ) } } render() { return (
{this.result()}
) } } export default SearchStudent