1
1
import Service from '@ember/service' ;
2
- import { inject as service } from '@ember/service' ;
3
2
import { tracked } from '@glimmer/tracking' ;
4
3
5
4
export default class DataService extends Service {
6
- @service libraryData ;
7
- @tracked booksReq = 0 ;
8
- @tracked libraryReq = 0 ;
5
+ @tracked booksReq = 0 ;
6
+ @tracked libraryReq = 0 ;
9
7
10
- // async getData(url) {
11
- // let arr = url.split('/');
8
+ async getData ( url ) {
9
+ let data = await fetch ( url ) . then ( ( response ) => {
10
+ if ( response . ok ) return response . json ( ) ;
11
+ } ) ;
12
12
13
- // // library
14
- // let library_id = arr[1];
15
- // let lib = await fetch(`/libraries/${library_id}`)
16
- // .then((response) => response.json())
17
- // .then((r) => r.data);
18
-
19
- // // books
20
- // if (arr.length == 3) {
21
- // let books = await fetch(`/libraries/${library_id}/books`)
22
- // .then((response) => response.json())
23
- // .then((r) => r.data);
24
- // return books;
25
- // } else if (arr.length > 3) {
26
- // let book_id = arr[3];
27
- // let book = await fetch(`/books/${book_id}`)
28
- // .then((response) => response.json())
29
- // .then((r) => r.data);
30
-
31
- // let lib_id = book.relationships.library.data.id;
32
-
33
- // if (library_id != lib_id) book = null;
34
-
35
- // if (book) this.booksReq++;
36
- // return book;
37
- // } else {
38
- // if (lib) this.libraryReq++;
39
- // return lib;
40
- // }
41
- // }
42
-
43
- async getData ( url ) {
44
- console . log ( url ) ;
45
- let data = await fetch ( url )
46
- . then ( ( response ) => response . json ( ) )
47
- . then ( ( r ) => r . data ) ;
48
-
49
- if ( data ) {
50
- if ( data . type == 'libraries' ) {
51
- if ( data ) this . libraryReq ++ ;
52
- }
53
- else if ( data . type == 'books' ) {
54
- if ( data ) this . booksReq ++ ;
55
- }
56
- }
57
- return data ;
13
+ if ( data ) {
14
+ if ( data . library ) {
15
+ this . libraryReq ++ ;
16
+ } else if ( data . book ) {
17
+ this . booksReq ++ ;
18
+ }
58
19
}
20
+ return data ;
21
+ }
59
22
}
23
+ // if (data.type == 'libraries') {
24
+ // this.libraryReq++;
25
+ // } else if (data.type == 'books') {
26
+ // this.booksReq++;
27
+ // }
60
28
61
29
// let book;
62
30
// lib.books.forEach(element => {
@@ -72,4 +40,37 @@ export default class DataService extends Service {
72
40
// lib = element;
73
41
// return;
74
42
// }
75
- // });
43
+ // });
44
+
45
+ // async getData(url) {
46
+ // let arr = url.split('/');
47
+
48
+ // // library
49
+ // let library_id = arr[1];
50
+ // let lib = await fetch(`/libraries/${library_id}`)
51
+ // .then((response) => response.json())
52
+ // .then((r) => r.data);
53
+
54
+ // // books
55
+ // if (arr.length == 3) {
56
+ // let books = await fetch(`/libraries/${library_id}/books`)
57
+ // .then((response) => response.json())
58
+ // .then((r) => r.data);
59
+ // return books;
60
+ // } else if (arr.length > 3) {
61
+ // let book_id = arr[3];
62
+ // let book = await fetch(`/books/${book_id}`)
63
+ // .then((response) => response.json())
64
+ // .then((r) => r.data);
65
+
66
+ // let lib_id = book.relationships.library.data.id;
67
+
68
+ // if (library_id != lib_id) book = null;
69
+
70
+ // if (book) this.booksReq++;
71
+ // return book;
72
+ // } else {
73
+ // if (lib) this.libraryReq++;
74
+ // return lib;
75
+ // }
76
+ // }
0 commit comments