StudentAdd.js 3.3 KB
import React from 'react';
import {post} from 'axios';
class StudentAdd extends React.Component{

    constructor(props){
        super(props);
        this.state={
            st_Code:'',
            st_Name:'',
            st_Id:'',
            st_Major:'',
            st_Midscore:'',
            st_Finalscore:'',
            st_Assignscore:'',
            st_Attendscore:'',
            st_Score:''
        }
    }

    handleFormSubmit = (e) => {
        e.preventDefault()
        this.addStudent()
        .then((response) => {
            console.log(response.data);
            this.props.stateRefresh();
        })
        this.setState({
            st_Code:'',
            st_Name:'',
            st_Id:'',
            st_Major:'',
            st_Midscore:'',
            st_Finalscore:'',
            st_Assignscore:'',
            st_Attendscore:'',
            st_Score:''
        })

    }

    handleValueChange = (e) => {
        let nextState = {};
        nextState[e.target.name]=e.target.value;
        this.setState(nextState);
    }

    addStudent = () =>{
        const url ='/api/students';
        const formData=new FormData();
        formData.append('st_Code',this.state.st_Code);
        formData.append('st_Name',this.state.st_Name);
        formData.append('st_Id',this.state.st_Id);
        formData.append('st_Major',this.state.st_Major);
        formData.append('st_Midscore',this.state.st_Midscore);
        formData.append('st_Finalscore',this.state.st_Finalscore);
        formData.append('st_Assignscore',this.state.st_Assignscore);
        formData.append('st_Attendscore',this.state.st_Attendscore);
        formData.append('st_Score',this.state.st_Score);
        const config = {
            headers: {
                'content-tupe': 'multipart/form-data'        
            }

        }
        return post(url, formData, config);
    }

    render(){
        return(
            <form onSubmit={this.handleFormSubmit}>
                <h1>학생 추가</h1>
                코드 번호 : <input type="text" name="st_Code" value={this.state.st_Code} onChange={this.handleValueChange}/><br/>
                   이름   : <input type="text" name="st_Name" value={this.state.st_Name} onChange={this.handleValueChange}/><br/>
                   학번   : <input type="text" name="st_Id" value={this.state.st_Id} onChange={this.handleValueChange}/><br/>
                   전공   : <input type="text" name="st_Major" value={this.state.st_Major} onChange={this.handleValueChange}/><br/>
                중간 점수 : <input type="text" name="st_Midscore" value={this.state.st_Midscore} onChange={this.handleValueChange}/><br/>
                기말 점수 : <input type="text" name="st_Finalscore" value={this.state.st_Finalscore} onChange={this.handleValueChange}/><br/>
                과제 점수 : <input type="text" name="st_Assignscore" value={this.state.st_Assignscore} onChange={this.handleValueChange}/><br/>
                출석 점수 : <input type="text" name="st_Attendscore" value={this.state.st_Attendscore} onChange={this.handleValueChange}/><br/>
                   학점   : <input type="text" name="st_Score" value={this.state.st_Score} onChange={this.handleValueChange}/><br/>
                <button type="submit">추가하기</button>

            </form>
        )
    }
}

export default StudentAdd;