client/src/components/Medications.js
//GET medications from API via search
GET medications from API via search
GET medications from API via search
function searchMedicationsOnAPI(e){
        e.preventDefault()
        if (genericMedication) {
            fetch(`https://api.fda.gov/drug/drugsfda.json?search=openfda.generic_name:${medicationName}`, {
                method: "GET",
                headers: {
                    "Content-Type": "application/json"
                },
            })
            .then(response => response.json())
            .then(data => {
                if (!data.error) {
                    let savedGenericInfo = data.results[0].products.map((product) => {
                        return product.active_ingredients[0]
                    })
                    setMedicationsFromAPI(savedGenericInfo)
                } else {
                    alert("Generic medication not found! Please search again.")
                }
            })
        } else {
            fetch(`https://api.fda.gov/drug/drugsfda.json?search=openfda.brand_name:${medicationName}`, {
                method: "GET",
                headers: {
                    "Content-Type": "application/json"
                },
            })
            .then(response => response.json())
            .then(data => {
                if (!data.error) {
                    let savedBrandInfo = data.results[0].products.map((product) => {
                        return product.active_ingredients[0]
                    })
                    setMedicationsFromAPI(savedBrandInfo)
                } else {
                    alert("Brand name medication not found! Please search again.")
                }
            })
        }
    }
function searchMedicationsOnAPI ( e ) { e . preventDefault ( ) if ( genericMedication ) { fetch ( ` ${ medicationName } ` , { method : "GET" , headers : { "Content-Type" : "application/json" } , } ) . then ( response => response . json ( ) ) . then ( data => { if ( ! data . error ) { let savedGenericInfo = data . results [ 0 ] . products . map ( ( product ) => { return product . active_ingredients [ 0 ] } ) setMedicationsFromAPI ( savedGenericInfo ) } else { alert ( "Generic medication not found! Please search again." ) } } ) } else { fetch ( ` ${ medicationName } ` , { method : "GET" , headers : { "Content-Type" : "application/json" } , } ) . then ( response => response . json ( ) ) . then ( data => { if ( ! data . error ) { let savedBrandInfo = data . results [ 0 ] . products . map ( ( product ) => { return product . active_ingredients [ 0 ] } ) setMedicationsFromAPI ( savedBrandInfo ) } else { alert ( "Brand name medication not found! Please search again." ) } } ) } }
GET medications from API via search
GET medications from API via search