@@ -12,12 +12,11 @@ import {
1212 stopAudio ,
1313 createAudio ,
1414 createUserRecipe ,
15- cacheData ,
16- CACHE_NAME_URL ,
15+ cacheData ,
16+ CACHE_NAME_URL ,
1717 CACHE_NAME_AUDIO ,
1818 emailObject ,
1919 audioElement ,
20-
2120} from "./js_utilities/functions_and_variables.js" ;
2221
2322import {
@@ -59,9 +58,7 @@ import {
5958} from "./js_utilities/query_selector.js" ;
6059
6160let currentCameraIndex = 0 ;
62- const switchCameraButton = document . getElementById ( 'switchCamera' ) ;
63-
64-
61+ const switchCameraButton = document . getElementById ( "switchCamera" ) ;
6562
6663wantToTakeAPicture . addEventListener ( "click" , ( ) => {
6764 removeElements ( [ pictureSectionHeadline , wantToTakeAPicture ] ) ;
@@ -168,32 +165,54 @@ recipeButtons.forEach((button) => {
168165 const userRecipe = createUserRecipe ( button , dietaryRequirements , userText ) ;
169166 console . log ( userRecipe ) ;
170167
171-
172-
173-
168+ const data = { } ;
174169
175170 const eventSource = new EventSource ( `/stream?${ createQuery ( userRecipe ) } ` ) ;
176171
177172 eventSource . onmessage = async function ( event ) {
178- let data = JSON . parse ( event . data ) ;
179- if ( data . message ) {
180- if ( data . message === "stop" ) {
173+ let eventData = JSON . parse ( event . data ) ;
174+ if ( eventData . message ) {
175+ if ( eventData . message === "stop" ) {
181176 eventSource . close ( ) ;
182177 return ;
183178 }
184179 displayElements ( [ gptResponseElement ] ) ;
185- gptResponseElement . textContent += data . message ;
180+ gptResponseElement . textContent += eventData . message ;
186181 return ;
187- } else if ( data . errorMessage === "invalid_api_key" ) {
182+ } else if ( eventData . errorMessage === "invalid_api_key" ) {
188183 eventSource . close ( ) ;
189- console . log ( data . errorMessage ) ;
184+ console . log ( eventData . errorMessage ) ;
190185 displayElements ( [ gptResponseElement , tryAgainBtn ] ) ;
191186 removeElements ( [ loadingContainer ] ) ;
192187 gptResponseElement . innerHTML = defaultRecipe ;
193188 return ;
194189 }
195190
196- if ( data . audio ) {
191+ if ( eventData . audio ) {
192+ data . audio = eventData . audio ;
193+ }
194+ if ( eventData . image ) {
195+ data . image = eventData . image ;
196+ }
197+ // console.log("cacheObject", data);
198+ console . log ( "data.audio" , eventData . audio ) ;
199+ console . log ( "data.image" , eventData . image ) ;
200+
201+ if ( data . audio && data . image ) {
202+ console . log ( typeof data . image ) ;
203+ removeElements ( [ loadingContainer ] ) ;
204+ const imageUrl = data . image . data [ 0 ] . url ;
205+ console . log ( `imageURL ${ imageUrl } ` ) ;
206+ // await cacheData(imageUrl, CACHE_NAME_URL, "image");
207+ backgroundImg . src = imageUrl ;
208+ backgroundImg . onload = ( ) => {
209+ console . log ( "Image loaded successfully" ) ;
210+ } ;
211+ backgroundImg . onerror = ( ) => {
212+ console . error ( "Error loading image" ) ;
213+ } ;
214+
215+ ///
197216 console . log ( data . audio ) ;
198217 const audio_data = createAudio ( data . audio ) ;
199218 console . log ( `line 261: ${ audio_data } ` ) ;
@@ -236,40 +255,39 @@ recipeButtons.forEach((button) => {
236255 } ) ;
237256 }
238257
239- if ( data . image ) {
240- console . log ( typeof data . image ) ;
241- removeElements ( [ loadingContainer ] ) ;
242- const imageUrl = data . image . data [ 0 ] . url ;
243- console . log ( `imageURL ${ imageUrl } ` ) ;
244- await cacheData ( imageUrl , CACHE_NAME_URL , "image" ) ;
245- backgroundImg . src = imageUrl ;
246- backgroundImg . onload = ( ) => {
247- console . log ( "Image loaded successfully" ) ;
248- } ;
249- backgroundImg . onerror = ( ) => {
250- console . error ( "Error loading image" ) ;
251- } ;
252- }
258+ // if (data.image) {
259+ // console.log(typeof data.image);
260+ // removeElements([loadingContainer]);
261+ // const imageUrl = data.image.data[0].url;
262+ // console.log(`imageURL ${imageUrl}`);
263+ // // await cacheData(imageUrl, CACHE_NAME_URL, "image");
264+ // backgroundImg.src = imageUrl;
265+ // backgroundImg.onload = () => {
266+ // console.log("Image loaded successfully");
267+ // };
268+ // backgroundImg.onerror = () => {
269+ // console.error("Error loading image");
270+ // };
271+ // }
253272 } ;
254273 } ) ;
255274} ) ;
256275
257276// Picture section
258277
259-
260278async function getVideoDevices ( ) {
261279 const devices = await navigator . mediaDevices . enumerateDevices ( ) ;
262- return devices . filter ( device => device . kind === ' videoinput' ) ;
280+ return devices . filter ( ( device ) => device . kind === " videoinput" ) ;
263281}
264282
265283async function startCamera ( deviceId ) {
266284 const constraints = {
267285 audio : false ,
268286 video : {
269287 deviceId : deviceId ? { exact : deviceId } : undefined ,
270- width : { min : 1024 , ideal : 1280 , max : 1920 } ,
271- height : { min : 576 , ideal : 720 , max : 1080 }
272- }
288+ width : { min : 1024 , ideal : 1280 , max : 1920 } ,
289+ height : { min : 576 , ideal : 720 , max : 1080 } ,
290+ } ,
273291 } ;
274292
275293 try {
@@ -283,26 +301,30 @@ async function startCamera(deviceId) {
283301
284302async function initializeCamera ( ) {
285303 const videoDevices = await getVideoDevices ( ) ;
286-
304+
287305 if ( videoDevices . length > 1 ) {
288- switchCameraButton . style . display = ' block' ;
289- switchCameraButton . addEventListener ( ' click' , ( ) => {
306+ switchCameraButton . style . display = " block" ;
307+ switchCameraButton . addEventListener ( " click" , ( ) => {
290308 currentCameraIndex = ( currentCameraIndex + 1 ) % videoDevices . length ;
291309 startCamera ( videoDevices [ currentCameraIndex ] . deviceId ) ;
292310 } ) ;
293311 } else {
294- switchCameraButton . style . display = ' none' ;
312+ switchCameraButton . style . display = " none" ;
295313 }
296314
297315 // Start with the rear camera if available
298- const rearCameraDevice = videoDevices . find ( device => device . label . toLowerCase ( ) . includes ( 'back' ) || device . label . toLowerCase ( ) . includes ( 'rear' ) ) ;
299- startCamera ( rearCameraDevice ? rearCameraDevice . deviceId : videoDevices [ 0 ] . deviceId ) ;
316+ const rearCameraDevice = videoDevices . find (
317+ ( device ) =>
318+ device . label . toLowerCase ( ) . includes ( "back" ) ||
319+ device . label . toLowerCase ( ) . includes ( "rear" )
320+ ) ;
321+ startCamera (
322+ rearCameraDevice ? rearCameraDevice . deviceId : videoDevices [ 0 ] . deviceId
323+ ) ;
300324}
301325
302326initializeCamera ( ) ;
303327
304-
305-
306328// navigator.mediaDevices
307329// .getUserMedia(constraint)
308330// .then((stream) => {
0 commit comments