@@ -7,6 +7,69 @@ import ViewOrder from "./ViewOrder";
7
7
import Confirm from "./Confirm" ;
8
8
import ViewIngredient from "./ViewIngredient" ;
9
9
10
+ const baseServerURL = "http://localhost:8080" ;
11
+
12
+ async function inventoryLoader ( ) {
13
+ const inventory = { Sallad : { price : 10 , foundation : true , vegan : true } } ;
14
+ await new Promise ( resolve => setTimeout ( resolve , 500 ) ) ;
15
+
16
+ /*let promiseFoundations = safeFetchJson(new URL('foundations', baseServerURL))
17
+ .then(result => {
18
+ console.log(result);
19
+ result.forEach(element => {
20
+ let promiseFoundationDetails = fetchIngredient('foundations', element)
21
+ .then(result => {
22
+ inventory[element] = {...result};
23
+ console.log(inventory);
24
+ });
25
+ });
26
+ return result;
27
+ }); */
28
+ let myPromise = [ ] ;
29
+ myPromise . push ( fetchIngredientType ( 'foundations' , inventory ) ) ;
30
+ myPromise . push ( fetchIngredientType ( 'extras' , inventory ) ) ;
31
+ myPromise . push ( fetchIngredientType ( 'proteins' , inventory ) ) ;
32
+ myPromise . push ( fetchIngredientType ( 'dressings' , inventory ) ) ;
33
+
34
+ return Promise . all ( myPromise ) . then ( _ => {
35
+ return inventory ;
36
+ } ) ;
37
+ }
38
+
39
+ async function fetchIngredientType ( ingredientType , inventory ) {
40
+ let promises = [ ] ;
41
+ safeFetchJson ( new URL ( ingredientType , baseServerURL ) )
42
+ . then ( result => {
43
+ result . forEach ( element => {
44
+ let promise = fetchIngredient ( ingredientType , element ) ;
45
+ promises . push ( { [ element ] : promise } ) ;
46
+ promise . then ( result => {
47
+ inventory [ element ] = { ...result } ;
48
+ } ) ;
49
+ } ) ;
50
+ } )
51
+
52
+ return Promise . all ( promises ) ;
53
+ }
54
+
55
+ async function fetchIngredient ( type , ingredient ) {
56
+ let resp = safeFetchJson ( new URL ( type + '/' + ingredient , baseServerURL ) ) ;
57
+ resp . then ( properties => {
58
+ console . log ( { [ ingredient ] : { ...properties } } ) ;
59
+ } ) ;
60
+ return resp ;
61
+ }
62
+
63
+ function safeFetchJson ( url ) {
64
+ return fetch ( url )
65
+ . then ( response => {
66
+ if ( ! response . ok ) {
67
+ throw new Error ( `${ url } returned status ${ response . status } ` ) ;
68
+ }
69
+ return response . json ( ) ;
70
+ } ) ;
71
+ }
72
+
10
73
const router = createBrowserRouter ( [
11
74
{
12
75
element : < App /> ,
@@ -17,6 +80,7 @@ const router = createBrowserRouter([
17
80
} ,
18
81
{
19
82
path : "compose-salad" ,
83
+ loader : inventoryLoader ,
20
84
element : < ComposeSalad /> ,
21
85
children : [
22
86
{
0 commit comments