@@ -52,9 +52,17 @@ import {
5252 emailUserRecipeSection ,
5353} from "./js_utilities/query_selector.js" ;
5454
55- let audioElement ;
55+
56+ let audioElement = new Audio ( ) ;
57+
5658let emailObject
5759
60+ const audio_source = document . querySelector ( ".audio_source" ) ;
61+
62+ console . log ( audio_source )
63+
64+
65+
5866
5967
6068wantToTakeAPicture . addEventListener ( "click" , ( ) => {
@@ -169,6 +177,33 @@ recipeButtons.forEach((button) => {
169177 console . log ( userRecipe ) ;
170178
171179 const CACHE_NAME = 'image-cache-v1' ;
180+ const CACHE_NAME_AUDIO = 'image-cache-v2'
181+
182+
183+ // Function to cache the audio (without fetching the audio)
184+ async function cacheAudio ( audio ) {
185+ const cache = await caches . open ( CACHE_NAME_AUDIO ) ;
186+ const response = new Response ( JSON . stringify ( { audio, timestamp : Date . now ( ) } ) ) ;
187+ await cache . put ( "last-generated-audio" , response ) ;
188+ }
189+
190+ // Funnction to get the cached audio data
191+ async function getCachedAudio ( ) {
192+ const cache = await caches . open ( CACHE_NAME_AUDIO ) ;
193+ const response = await cache . match ( "last-generated-audio" ) ;
194+ if ( response ) {
195+ const data = await response . json ( ) ;
196+ const now = Date . now ( ) ;
197+ if ( now - data . timestamp < 24 * 60 * 60 * 1000 ) {
198+ return data . url ;
199+ } else {
200+ await cache . delete ( "last-generated-audio" )
201+ }
202+ }
203+ return null
204+ }
205+
206+
172207
173208 // Function to cache the image URL (without fetching the image)
174209 async function cacheImageUrl ( url ) {
@@ -218,15 +253,21 @@ recipeButtons.forEach((button) => {
218253 }
219254
220255 if ( data . audio ) {
256+
221257 console . log ( data . audio ) ;
222- loadingText . innerHTML = "Hang in there creating the audio and image..." ;
258+
259+ const audio_data = createAudio ( data . audio ) ;
260+ console . log ( `line 261: ${ audio_data } ` )
261+ // Cache the url object
262+ await cacheAudio ( audio_data )
263+
223264 displayElementsFlex ( [ recording ] ) ;
224265 displayElements ( [ sendRecipeToUserInboxBtn , userWantAnotherRecipe ] ) ;
225266
226267 const speechBtns = Array . from ( document . querySelectorAll ( ".fa-solid" ) ) ;
227268 const speedBtn = document . querySelector ( "#speed" ) ;
228269
229- audioElement = createAudio ( data . audio ) ;
270+ audioElement . src = createAudio ( data . audio ) ;
230271
231272 audioElement . stop = function ( ) {
232273 this . pause ( ) ;
@@ -235,7 +276,7 @@ recipeButtons.forEach((button) => {
235276
236277 userWantAnotherRecipe . addEventListener ( "click" , ( ) => {
237278 displayElements ( [ headline , allergies , ...recipeButtons , mainElement ] ) ;
238- removeElements ( [ userText , emailSection ] ) ;
279+ removeElements ( [ userText , emailSection , recording ] ) ;
239280 emptyTheElement ( gptResponseElement ) ;
240281 resetCheckedStateToFalse ( dietaryRequirements ) ;
241282 userText . value = "" ;
@@ -269,10 +310,20 @@ recipeButtons.forEach((button) => {
269310 await cacheImageUrl ( imageUrl ) ;
270311 backgroundImg . addEventListener ( "load" , ( ) => {
271312 backgroundImg . src = imageUrl ;
313+ loadingText . textContent = "Hang in there creating the audio..." ;
314+
272315 } )
273316 }
274317 } ;
275318
319+ // Before setting up the eventSource, check for a cached audio
320+ async function loadCachedAudio ( ) {
321+ const cachedAudio = await getCachedAudio ( ) ;
322+ if ( cachedAudio ) {
323+ audioElement . src = cachedAudio ;
324+ }
325+ }
326+
276327// Before setting up the eventSource, check for a cached image URL
277328async function loadCachedImage ( ) {
278329 const cachedImageUrl = await getCachedImageUrl ( ) ;
@@ -283,6 +334,7 @@ async function loadCachedImage() {
283334
284335// Call this function before setting up the eventSource
285336loadCachedImage ( ) ;
337+ loadCachedAudio ( ) ;
286338 } ) ;
287339} ) ;
288340
0 commit comments