-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
91 lines (78 loc) · 3.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { loading, stopLoading, enableMainBtn, errorMessage } from "./uxFunctions"
import Recipe from "./recipeConstructor"
const formContainer = document.getElementById('form-container')
const mainBtn = document.getElementById('main-btn')
const ingredients = document.getElementById('ingredients')
const additionalIngredientForm = document.getElementById('additional-ingredients-form')
const people = document.getElementById('total-people')
const time = document.getElementById('cooking-time')
const equipment = document.getElementById('cooking-equipment')
let action = 'submit'
mainBtn.addEventListener('click', function () {
if (action === 'submit') {
action = 'reset'
const additionalIngredients = additionalIngredientForm.querySelector('input[type="radio"]:checked').value
main(ingredients.value, additionalIngredients, people.value, time.value, equipment.value)
mainBtn.innerText = 'Reset'
} else {
location.reload()
}
})
async function main(ingredients, additionalIngredients, people, time, equipment){
try {
formContainer.classList.toggle('hidden')
loading('loadingGetRecipes')
const recipeResponse = await getRecipes(ingredients, additionalIngredients, people, time, equipment)
loading('loadingGetFormattedRecipes')
// consolodated two netlify functions into one cloudflare worker function.
// this promise is here just so the second loading graphic still shows. previously it ran after getRecipes and before getFormattedRecipes (depricated)
await new Promise(resolve => setTimeout(resolve, 1500))
const recipeArray = JSON.parse(recipeResponse)
stopLoading()
renderRecipes(recipeArray)
} catch (e) {
stopLoading()
throw new Error('Error running main function', e)
}
}
async function getRecipes(ingredients, additionalIngredients, people, time, equipment){
try {
const response = await fetch('https://fetch-recipes-worker.eddie-oconnor3.workers.dev', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ingredients, additionalIngredients, people, time, equipment})
})
if(response.ok){
const data = await response.json()
return data.formattedRecipes.choices[0].message.content
} else {
throw new Error('Error fetching recipes from cloudflare worker.')
}
} catch (e) {
stopLoading()
errorMessage(mainBtn, e.message)
console.error('Error fetching recipes.', e)
}
}
async function renderRecipes(recipeArray){
const recipeResults = document.getElementById('recipe-results')
let recipeResultsHtml = ''
try {
if(recipeArray.length > 0){
recipeResultsHtml = recipeArray.map((recipe) => new Recipe(recipe).getRecipeHtml()).join('')
} else {
throw new Error('Error loading recipes.')
}
recipeResults.innerHTML = recipeResultsHtml
} catch (e) {
stopLoading()
errorMessage(mainBtn, e.message)
console.error('Error loading recipes.', e)
}
}
/* UX Functions */
formContainer.addEventListener('change', () => {
enableMainBtn(mainBtn, ingredients.value, additionalIngredientForm, people.value, time.value, equipment.value)
})