Skip to content

Commit

Permalink
Merge pull request #114 from tahmid-saj/dev-general-cleanup-1
Browse files Browse the repository at this point in the history
moving requests
  • Loading branch information
tahmid-saj authored Jun 13, 2024
2 parents 5f0d95d + ecf9529 commit 6f4f50d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 72 deletions.
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)
}
}
}

0 comments on commit 6f4f50d

Please sign in to comment.