{"size": 2624, "ext": "js", "max_stars_repo_path": "src/components/pages/AddReport.js", "max_stars_repo_name": "alexmeigz/CS148_Project_Frontend", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/components/pages/AddReport.js", "max_issues_repo_name": "alexmeigz/CS148_Project_Frontend", "max_issues_repo_issues_event_min_datetime": "2020-11-04T03:14:00.000Z", "max_issues_repo_issues_event_max_datetime": "2020-12-17T02:22:06.000Z", "max_forks_repo_path": "src/components/pages/AddReport.js", "max_forks_repo_name": "alexmeigz/CS148_Project_Frontend", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 28, "max_forks_count": null, "avg_line_length": 30.8705882353, "max_line_length": 138, "alphanum_fraction": 0.5030487805}
// AddReport.js
// Engineer: Pranav Acharya

import React, {useState} from "react";

function AddReport(props) {
    let server = "http://localhost:8118/api"
    if (process.env.REACT_APP_REMOTE === "1") { 
        server = "https://nutriflix-flask-backend.herokuapp.com/api"
    }
    if (process.env.NODE_ENV !== "development") {
        server = "https://nutriflix-flask-backend.herokuapp.com/api"
    }

    const [newInfo, setNewInfo] = useState({
        ...props.postData,
        report_info: ""
    })

    function handleChange(evt) { 
        const name = evt.target.name 
        const value = evt.target.value 

	  setNewInfo({
        ...newInfo,
        [name]: value
      })
    }

    function submitReport(event) {
        event.preventDefault();

        let url = `${server}/report/?`

        url += `&userReporter_id=${(JSON.parse(sessionStorage.getItem("user")).user_id)}`
        url += `&reportedUser_id=${props.postData.user_id}`

        /*requiredParams.forEach((param, index) => {
            if (newInfo[param] === "") {
                newInfo[param] = props.postData[param]
            }
            url += `&${param}=${newInfo[param]}`
        });*/

        console.log(url)

        fetch(url, 
            {
                method: 'POST',
                headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
                }, 
                body: newInfo["report_info"],          
            })
            .then(response => response.json()) 
                .then(data => {
                if(data["message"] === "Report created successfully!" || data["message"] === "Report submitted successfully!"){
                    alert("Report created successfully!")
                    props.cancelReport()
                }
                else{
                    alert(`Error creating report: ${data["message"]}`)
                }
            })
            .catch((error) => console.log("Report creation error: " + error))

    }

    return (
        <div className="update-form">
            <h1> Add Report </h1>
            <form onSubmit={submitReport}>
                <div className="form_input">
                    <label className="form_label" for="report_info"> Report: </label>         
                    <textarea className="form_field" type="text" value={newInfo.report_info} name="report_info" onChange={handleChange} />
                </div>
                
                <input className="form_submit" type="submit" value="Submit" />
            </form>
        </div>
    )
}

export default AddReport;