Skip to content

Commit 3101188

Browse files
authored
Merge pull request fac29b#47 from fac29b/dynamically-add-event-listeners
Dynamically add event listeners
2 parents c011175 + 548637b commit 3101188

5 files changed

Lines changed: 66 additions & 52 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"scripts": {
3-
"start": "nodemon server.js",
3+
"watch": "nodemon server.js",
44
"devStart": "nodemon server.js"
55
},
66
"dependencies": {

public/index.css

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ main {
8080
transform: translate(-50%,-50%);
8181
}
8282

83+
84+
85+
/* .fa-paper-plane {
86+
background-color: white;
87+
border-radius: 5%;
88+
display: flex;
89+
align-items: center;
90+
justify-content: center;
91+
} */
92+
8393
.fa-microphone {
8494
height: 30px;
8595
width: 30px;
@@ -143,6 +153,16 @@ button {
143153
transition: background-color 0.5s ease;
144154
}
145155

156+
.send-to-user-inbox-btn {
157+
text-transform: uppercase;
158+
height: 20px;
159+
margin-right: 10px;
160+
margin-left: 0px;
161+
border-radius: 5px;
162+
background-color: var(--green);
163+
transition: background-color 0.5s ease;
164+
}
165+
146166
.send-recipe-to-user-inbox,
147167
.want-another-recipe {
148168
width: 170px;
@@ -151,12 +171,10 @@ button {
151171
}
152172

153173
.email-section {
154-
width: 200px;
174+
width: 170px;
155175
display: none;
156176
grid-template-columns: 2fr 1fr;
157-
gap: 1rem;
158-
margin:auto
159-
177+
margin:auto;
160178
}
161179

162180

public/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ <h3 id="loading-text">Creating Recipe...</h3>
123123
name="user_email_address"
124124
placeholder="sophie@example.com"
125125
/>
126-
<i class="fa fa-paper-plane" aria-hidden="true" fa-lg></i>
126+
<button class="send-to-user-inbox-btn">send</button>
127+
<!-- <i class="fa fa-paper-plane" aria-hidden="true" fa-lg></i> -->
127128
<!-- <button class="send-email-btn">send</button> -->
128129
</section>
129130
</div>

public/index.js

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ const userEmail = document.querySelector("#user-email");
1818
const sendEmailButton = document.querySelector(".send-email-btn");
1919
const emailSection = document.querySelector(".email-section");
2020
const paperPlane = document.querySelector(".fa-paper-plane");
21+
const sendToUserInboxBtn = document.querySelector(".send-to-user-inbox-btn");
22+
console.log(sendToUserInboxBtn);
23+
2124
const dietaryRequirements = Array.from(
2225
document.querySelectorAll(".dietary-requirements")
2326
);
@@ -29,7 +32,8 @@ let textContent;
2932
let imageUrl;
3033
let isLactoseIntolerant;
3134
let dishOriginCountry;
32-
let currentChar
35+
let currentChar;
36+
3337
const defaultRecipe = `
3438
Apologies, but our AI Recipe-Making expert is unavailable. Please try again later. In the meantime, please find one of our favourite recipes below.
3539
@@ -170,7 +174,7 @@ darkLightButton.addEventListener("change", () => {
170174
});
171175
});
172176

173-
paperPlane.addEventListener("click", () => {
177+
sendToUserInboxBtn.addEventListener("click", () => {
174178
let emailOBject = {
175179
[userEmail.name]: userEmail.value,
176180
};
@@ -264,9 +268,9 @@ recipeButtons.forEach((button) => {
264268
textContent = data.text.choices[0].message.content;
265269
gptResponseElement.innerHTML = `
266270
<div class="recording">
267-
<i class="fa-solid fa-microphone"></i>
268-
<i class="fa-solid fa-pause"></i>
269-
<i class="fa-solid fa-stop"></i>
271+
<i class="fa-solid fa-microphone" name="microphone"></i>
272+
<i class="fa-solid fa-pause" name="pause"></i>
273+
<i class="fa-solid fa-stop" name="stop"></i>
270274
<div class="speed-wrapper">
271275
<label for="speed">Speed</label>
272276
<input type="number" name="speed" id="speed" min="0.25" max="2" step="0.25" value="1">
@@ -282,11 +286,18 @@ recipeButtons.forEach((button) => {
282286

283287
const utterance = new SpeechSynthesisUtterance();
284288

289+
const speechBtns = Array.from(
290+
document.querySelectorAll(".fa-solid")
291+
);
292+
const speedBtn = document.querySelector("#speed");
293+
294+
console.log(speechBtns);
295+
285296
function readRecipe(recipe) {
286-
if(speechSynthesis.paused && speechSynthesis.speaking) {
297+
if (speechSynthesis.paused && speechSynthesis.speaking) {
287298
return speechSynthesis.resume();
288299
}
289-
if(speechSynthesis.speaking) return
300+
if (speechSynthesis.speaking) return;
290301
utterance.text = recipe;
291302
utterance.rate = speedBtn.value || 1;
292303
speechSynthesis.speak(utterance);
@@ -300,32 +311,18 @@ recipeButtons.forEach((button) => {
300311
speechSynthesis.cancel();
301312
}
302313

303-
const microphoneBtn = document.querySelector(".fa-microphone");
304-
const pauseBtn = document.querySelector(".fa-pause");
305-
const stopBtn = document.querySelector(".fa-stop");
306-
const speedBtn = document.querySelector("#speed");
307-
308-
console.log(speedBtn);
309-
310-
// speedBtn.addEventListener("input", () => {
311-
// stopREeading();
312-
// readRecipe(utterance.text.substring(currentChar));
313-
314-
// console.log("speed has been incremented")
315-
// })
316-
317-
// utterance.addEventListener("boundary", (e) => {
318-
// currentChar = e.charIndex;
319-
// })
320-
321-
stopBtn.addEventListener("click", stopREeading);
322-
323-
pauseBtn.addEventListener("click", pauseReading);
324-
325-
326-
327-
microphoneBtn.addEventListener("click", () => {
328-
readRecipe(`${textContent}`);
314+
speechBtns.forEach((speechBtn) => {
315+
speechBtn.addEventListener("click", () => {
316+
const btnName = speechBtn.getAttribute("name");
317+
if (btnName === "microphone") {
318+
console.log(btnName);
319+
readRecipe(`${textContent}`);
320+
} else if (btnName === "pause") {
321+
pauseReading();
322+
} else if (btnName === "stop") {
323+
stopREeading();
324+
}
325+
});
329326
});
330327
})
331328
.catch((error) => {

server.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,26 @@ app.get("/email", async (req, res) => {
5050

5151

5252
app.get("/openai", async (req, res) => {
53-
console.log(req.query);
53+
54+
const {recipe_country_of_origin, is_lactose_intolerant, is_vegan, what_are_user_other_dietary_requirements } = req.query
5455
try {
5556
console.log(
56-
{ country: req.query.recipe_country_of_origin },
57-
{ lactose_intolerant: req.query.is_lactose_intolerant },
58-
{ is_vegan: req.query.is_vegan },
59-
{
60-
user_otherdietary_requirements:
61-
req.query.what_are_user_other_dietary_requirements,
62-
}
57+
{ recipe_country_of_origin },
58+
{ is_lactose_intolerant },
59+
{ is_vegan },
60+
{what_are_user_other_dietary_requirements}
6361
);
6462

6563
const prompt = `Provide a recipe for a dish from ${
66-
req.query.recipe_country_of_origin
64+
recipe_country_of_origin
6765
}, taking into account the fact that I'm ${
68-
req.query.is_lactose_intolerant === "true"
66+
is_lactose_intolerant === "true"
6967
? "lactose intolerant"
7068
: "not lactose intolerant"
71-
} ${req.query.is_vegan === "true" ? "vegan" : "not vegan"} and ${
72-
req.query.what_are_user_other_dietary_requirements === ""
69+
} ${is_vegan === "true" ? "vegan" : "not vegan"} and ${
70+
what_are_user_other_dietary_requirements === ""
7371
? "I have no other dietary requirements"
74-
: req.query.what_are_user_other_dietary_requirements
72+
: what_are_user_other_dietary_requirements
7573
} `;
7674

7775
console.log(prompt);

0 commit comments

Comments
 (0)