Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moving requests #114

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 9 additions & 36 deletions src/utils/api-requests/calories-burned.requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,23 @@ import { errorOnGetSearchActivity,

// calories burned api requests

// helper functions
async function processSearchedActivity(trackedDayInfo, activityResults) {
console.log(trackedDayInfo)

return activityResults.map((activityResult) => {
return {
activity: String(activityResult.name),
searchedActivity: String(trackedDayInfo.activity),
dateTracked: String(trackedDayInfo.dateTracked),
caloriesBurnedPerHour: Number(activityResult.calories_per_hour),
durationMinutes: Number(activityResult.duration_minutes),
totalCaloriesBurned: Number(activityResult.total_calories)
}
})
}

// searching activity
export async function getSearchActivity(trackedDayInfo) {
try {
let url = `${process.env.REACT_APP_API_NINJAS_CALORIES_BURNED_URL}${trackedDayInfo.activity}`

if (trackedDayInfo.weightPounds !== "") {
url = url + `&weight=${trackedDayInfo.weightPounds}`
}
if (trackedDayInfo.durationMinutes !== "") {
url = url + `&duration=${trackedDayInfo.durationMinutes}`
}

const resActivityResults = await fetch(`${url}`, {
method: "GET",
console.log(`${process.env.REACT_APP_API_URL_CALORIES_BURNED_SEARCH_ACTIVITY}`)
const response = await fetch(`${process.env.REACT_APP_API_URL_CALORIES_BURNED_SEARCH_ACTIVITY}`, {
method: "POST",
headers: {
"X-Api-Key": `${process.env.REACT_APP_API_NINJAS_KEY}`
}
"Content-Type": "application/json"
},
body: JSON.stringify(trackedDayInfo)
})
const resJSON = await response.json()

const resJSON = await resActivityResults.json()
const res = await processSearchedActivity(trackedDayInfo, resJSON)
return res
return resJSON.searchedActivities
} catch (error) {
console.log(error)
errorOnGetSearchActivity()

if (error) {
return console.error("Request failed: ", error)
}
}
}

Expand Down
45 changes: 9 additions & 36 deletions src/utils/api-requests/nutrient-predictor.requests.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,5 @@
import { errorOnGetNutrientPredictions } from "../errors/nutrient-predictor.errors";

// helper functions
const processNutrientPredictions = async (nutrientPredictions) => {
return nutrientPredictions.map((nutrientPrediction) => {
return {
name: nutrientPrediction.name,
servingSizeG: nutrientPrediction.serving_size_g,
calories: nutrientPrediction.calories,
macronutrients: {
carbohydratesTotalG: nutrientPrediction.carbohydrates_total_g,
proteinG: nutrientPrediction.protein_g,
fatTotalG: nutrientPrediction.fat_total_g,
fatSaturatedG: nutrientPrediction.fat_saturated_g,
},
micronutrients: {
sodiumMG: nutrientPrediction.sodium_mg,
potassiumMG: nutrientPrediction.potassium_mg,
cholesterolMg: nutrientPrediction.cholesterol_mg,
fiberG: nutrientPrediction.fiber_g,
sugarG: nutrientPrediction.sugar_g
}
}
})
}

// nutrient predictor api requests

export const getMealPredictions = async (imagePath) => {
Expand All @@ -49,22 +25,19 @@ export const getMealPredictions = async (imagePath) => {

export const getNutrientPredictions = async (mealDescription) => {
try {
const resNutrientPredictions = await fetch(`${process.env.REACT_APP_API_NINJAS_NUTRIENT_PREDICTOR_URL}${mealDescription}`, {
method: "GET",
console.log(`${process.env.REACT_APP_API_URL_NUTRIENT_PREDICTOR_PREDICT_NUTRIENTS}`)
const response = await fetch(`${process.env.REACT_APP_API_URL_NUTRIENT_PREDICTOR_PREDICT_NUTRIENTS}`, {
method: "POST",
headers: {
"X-Api-Key": `${process.env.REACT_APP_API_NINJAS_KEY}`
}
"Content-Type": "text/plain"
},
body: String(mealDescription)
})
const resJSON = await response.json()

const resJSON = await resNutrientPredictions.json()
const res = await processNutrientPredictions(resJSON)
console.log(res)
return res
return resJSON.predictionResults
} catch (error) {
console.log(error)
errorOnGetNutrientPredictions()

if (error) {
return console.error("Request failed: ", error)
}
}
}
Loading