Skip to content

Commit 9da7e8e

Browse files
authored
Update data.js
1 parent 078eccf commit 9da7e8e

File tree

1 file changed

+53
-52
lines changed

1 file changed

+53
-52
lines changed

app/services/data.js

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,30 @@
11
import Service from '@ember/service';
2-
import { inject as service } from '@ember/service';
32
import { tracked } from '@glimmer/tracking';
43

54
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;
97

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+
});
1212

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+
}
5819
}
20+
return data;
21+
}
5922
}
23+
// if (data.type == 'libraries') {
24+
// this.libraryReq++;
25+
// } else if (data.type == 'books') {
26+
// this.booksReq++;
27+
// }
6028

6129
// let book;
6230
// lib.books.forEach(element => {
@@ -72,4 +40,37 @@ export default class DataService extends Service {
7240
// lib = element;
7341
// return;
7442
// }
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

Comments
 (0)