Lines 3-115
import { setError } from "./messages";
Link Here
|
3 |
export const fetchAgreement = async function (agreement_id) { |
3 |
export const fetchAgreement = async function (agreement_id) { |
4 |
if (!agreement_id) return; |
4 |
if (!agreement_id) return; |
5 |
const apiUrl = "/api/v1/erm/agreements/" + agreement_id; |
5 |
const apiUrl = "/api/v1/erm/agreements/" + agreement_id; |
6 |
let agreement; |
6 |
return await myFetch(apiUrl, { |
7 |
await fetch(apiUrl, { |
|
|
8 |
headers: { |
7 |
headers: { |
9 |
"x-koha-embed": |
8 |
"x-koha-embed": |
10 |
"periods,user_roles,user_roles.patron,agreement_licenses,agreement_licenses.license,agreement_relationships,agreement_relationships.related_agreement,documents,agreement_packages,agreement_packages.package,vendor", |
9 |
"periods,user_roles,user_roles.patron,agreement_licenses,agreement_licenses.license,agreement_relationships,agreement_relationships.related_agreement,documents,agreement_packages,agreement_packages.package,vendor", |
11 |
}, |
10 |
}, |
12 |
}) |
11 |
}); |
13 |
.then(checkError) |
|
|
14 |
.then( |
15 |
(result) => { |
16 |
agreement = result; |
17 |
}, |
18 |
(error) => { |
19 |
setError(error); |
20 |
} |
21 |
); |
22 |
return agreement; |
23 |
}; |
12 |
}; |
24 |
|
13 |
|
25 |
export const fetchAgreements = async function () { |
14 |
export const fetchAgreements = async function () { |
26 |
const apiUrl = "/api/v1/erm/agreements?_per_page=-1"; |
15 |
const apiUrl = "/api/v1/erm/agreements?_per_page=-1"; |
27 |
let agreements; |
16 |
return await myFetch(apiUrl); |
28 |
await fetch(apiUrl) |
|
|
29 |
.then(checkError) |
30 |
.then( |
31 |
(result) => { |
32 |
agreements = result; |
33 |
}, |
34 |
(error) => { |
35 |
setError(error); |
36 |
} |
37 |
); |
38 |
return agreements; |
39 |
}; |
17 |
}; |
40 |
|
18 |
|
41 |
export const fetchLicense = async function (license_id) { |
19 |
export const fetchLicense = async function (license_id) { |
42 |
if (!license_id) return; |
20 |
if (!license_id) return; |
43 |
const apiUrl = "/api/v1/erm/licenses/" + license_id; |
21 |
const apiUrl = "/api/v1/erm/licenses/" + license_id; |
44 |
let license; |
22 |
return await myFetch(apiUrl, { |
45 |
await fetch(apiUrl, { |
|
|
46 |
headers: { |
23 |
headers: { |
47 |
"x-koha-embed": "user_roles,user_roles.patron,vendor,documents", |
24 |
"x-koha-embed": "user_roles,user_roles.patron,vendor,documents", |
48 |
}, |
25 |
}, |
49 |
}) |
26 |
}); |
50 |
.then(checkError) |
|
|
51 |
.then( |
52 |
(result) => { |
53 |
license = result; |
54 |
}, |
55 |
(error) => { |
56 |
setError(error); |
57 |
} |
58 |
); |
59 |
return license; |
60 |
}; |
27 |
}; |
61 |
|
28 |
|
62 |
export const fetchLicenses = async function () { |
29 |
export const fetchLicenses = async function () { |
63 |
const apiUrl = "/api/v1/erm/licenses?_per_page=-1"; |
30 |
const apiUrl = "/api/v1/erm/licenses?_per_page=-1"; |
64 |
let licenses; |
31 |
return await myFetch(apiUrl, { |
65 |
await fetch(apiUrl, { |
|
|
66 |
headers: { |
32 |
headers: { |
67 |
"x-koha-embed": "vendor.name", |
33 |
"x-koha-embed": "vendor.name", |
68 |
}, |
34 |
}, |
69 |
}) |
35 |
}); |
70 |
.then(checkError) |
|
|
71 |
.then( |
72 |
(result) => { |
73 |
licenses = result; |
74 |
}, |
75 |
(error) => { |
76 |
setError(error); |
77 |
} |
78 |
); |
79 |
return licenses; |
80 |
}; |
36 |
}; |
81 |
|
37 |
|
82 |
export const fetchPatron = async function (patron_id) { |
38 |
export const fetchPatron = async function (patron_id) { |
83 |
if (!patron_id) return; |
39 |
if (!patron_id) return; |
84 |
const apiUrl = "/api/v1/patrons/" + patron_id; |
40 |
const apiUrl = "/api/v1/patrons/" + patron_id; |
85 |
let patron; |
41 |
return await myFetch(apiUrl); |
86 |
await fetch(apiUrl) |
|
|
87 |
.then(checkError) |
88 |
.then( |
89 |
(result) => { |
90 |
patron = result; |
91 |
}, |
92 |
(error) => { |
93 |
setError(error); |
94 |
} |
95 |
); |
96 |
return patron; |
97 |
}; |
42 |
}; |
98 |
|
43 |
|
99 |
export const fetchVendors = async function () { |
44 |
export const fetchVendors = async function () { |
100 |
const apiUrl = "/api/v1/acquisitions/vendors?_per_page=-1"; |
45 |
const apiUrl = "/api/v1/acquisitions/vendors?_per_page=-1"; |
101 |
let vendors; |
46 |
return await myFetch(apiUrl); |
102 |
await fetch(apiUrl) |
|
|
103 |
.then(checkError) |
104 |
.then( |
105 |
(result) => { |
106 |
vendors = result; |
107 |
}, |
108 |
(error) => { |
109 |
setError(error); |
110 |
} |
111 |
); |
112 |
return vendors; |
113 |
}; |
47 |
}; |
114 |
|
48 |
|
115 |
const _createEditPackage = async function (method, erm_package) { |
49 |
const _createEditPackage = async function (method, erm_package) { |
Lines 136-155
const _createEditPackage = async function (method, erm_package) {
Link Here
|
136 |
}, |
70 |
}, |
137 |
}; |
71 |
}; |
138 |
|
72 |
|
139 |
let r; |
73 |
return await myFetch(apiUrl, options, 1); |
140 |
await fetch(apiUrl, options) |
|
|
141 |
.then( |
142 |
(response) => { |
143 |
r = response; |
144 |
}, |
145 |
(error) => { |
146 |
setError(error); |
147 |
} |
148 |
) |
149 |
.catch((e) => { |
150 |
console.log(e); |
151 |
}); |
152 |
return r; |
153 |
}; |
74 |
}; |
154 |
|
75 |
|
155 |
export const createPackage = function (erm_package) { |
76 |
export const createPackage = function (erm_package) { |
Lines 161-183
export const editPackage = function (erm_package) {
Link Here
|
161 |
|
82 |
|
162 |
const _fetchPackage = async function (apiUrl, package_id) { |
83 |
const _fetchPackage = async function (apiUrl, package_id) { |
163 |
if (!package_id) return; |
84 |
if (!package_id) return; |
164 |
let erm_package; |
85 |
return await myFetch(apiUrl, { |
165 |
await fetch(apiUrl, { |
|
|
166 |
headers: { |
86 |
headers: { |
167 |
"x-koha-embed": |
87 |
"x-koha-embed": |
168 |
"package_agreements,package_agreements.agreement,resources+count,vendor", |
88 |
"package_agreements,package_agreements.agreement,resources+count,vendor", |
169 |
}, |
89 |
}, |
170 |
}) |
90 |
}); |
171 |
.then(checkError) |
|
|
172 |
.then( |
173 |
(result) => { |
174 |
erm_package = result; |
175 |
}, |
176 |
(error) => { |
177 |
setError(error); |
178 |
} |
179 |
); |
180 |
return erm_package; |
181 |
}; |
91 |
}; |
182 |
export const fetchLocalPackage = function (package_id) { |
92 |
export const fetchLocalPackage = function (package_id) { |
183 |
const apiUrl = "/api/v1/erm/eholdings/local/packages/" + package_id; |
93 |
const apiUrl = "/api/v1/erm/eholdings/local/packages/" + package_id; |
Lines 190-210
export const fetchEBSCOPackage = function (package_id) {
Link Here
|
190 |
|
100 |
|
191 |
export const _fetchPackages = async function (apiUrl) { |
101 |
export const _fetchPackages = async function (apiUrl) { |
192 |
let packages; |
102 |
let packages; |
193 |
await fetch(apiUrl, { |
103 |
return await myFetch(apiUrl, { |
194 |
headers: { |
104 |
headers: { |
195 |
"x-koha-embed": "resources+count,vendor.name", |
105 |
"x-koha-embed": "resources+count,vendor.name", |
196 |
}, |
106 |
}, |
197 |
}) |
107 |
}); |
198 |
.then(checkError) |
|
|
199 |
.then( |
200 |
(result) => { |
201 |
packages = result; |
202 |
}, |
203 |
(error) => { |
204 |
setError(error); |
205 |
} |
206 |
); |
207 |
return packages; |
208 |
}; |
108 |
}; |
209 |
export const fetchLocalPackages = function () { |
109 |
export const fetchLocalPackages = function () { |
210 |
const apiUrl = "/api/v1/erm/eholdings/local/packages?_per_page=-1"; |
110 |
const apiUrl = "/api/v1/erm/eholdings/local/packages?_per_page=-1"; |
Lines 215-262
export const fetchEBSCOPackages = function () {
Link Here
|
215 |
return _fetchPackages(apiUrl); |
115 |
return _fetchPackages(apiUrl); |
216 |
}; |
116 |
}; |
217 |
|
117 |
|
218 |
export const fetchCountLocalPackages = async function (filters) { |
118 |
export const fetchLocalPackageCount = async function (filters) { |
219 |
const q = { |
119 |
const q = filters |
220 |
"me.name": { like: "%" + filters.package_name + "%" }, |
120 |
? { |
221 |
...(filters.content_type |
121 |
"me.name": { like: "%" + filters.package_name + "%" }, |
222 |
? { "me.content_type": filters.content_type } |
122 |
...(filters.content_type |
223 |
: {}), |
123 |
? { "me.content_type": filters.content_type } |
224 |
}; |
124 |
: {}), |
|
|
125 |
} |
126 |
: {}; |
225 |
|
127 |
|
226 |
const params = { |
128 |
const params = { |
227 |
_page: 1, |
129 |
_page: 1, |
228 |
_per_page: 1, |
130 |
_per_page: 1, |
229 |
q: JSON.stringify(q), |
131 |
q: JSON.stringify(q), |
230 |
}; |
132 |
}; |
231 |
let count_local_packages; |
|
|
232 |
var apiUrl = "/api/v1/erm/eholdings/local/packages"; |
133 |
var apiUrl = "/api/v1/erm/eholdings/local/packages"; |
233 |
await fetch(apiUrl + "?" + new URLSearchParams(params)) |
134 |
return await myFetchTotal(apiUrl + "?" + new URLSearchParams(params)); |
234 |
//.then(checkError) |
|
|
235 |
.then( |
236 |
(response) => |
237 |
(count_local_packages = response.headers.get("X-Total-Count")) |
238 |
); |
239 |
return count_local_packages; |
240 |
}; |
135 |
}; |
241 |
|
136 |
|
242 |
export const _fetchTitle = async function (apiUrl, title_id) { |
137 |
export const _fetchTitle = async function (apiUrl, title_id) { |
243 |
if (!title_id) return; |
138 |
if (!title_id) return; |
244 |
let title; |
139 |
return await myFetch(apiUrl, { |
245 |
await fetch(apiUrl, { |
|
|
246 |
headers: { |
140 |
headers: { |
247 |
"x-koha-embed": "resources,resources.package", |
141 |
"x-koha-embed": "resources,resources.package", |
248 |
}, |
142 |
}, |
249 |
}) |
143 |
}); |
250 |
.then(checkError) |
|
|
251 |
.then( |
252 |
(result) => { |
253 |
title = result; |
254 |
}, |
255 |
(error) => { |
256 |
setError(error); |
257 |
} |
258 |
); |
259 |
return title; |
260 |
}; |
144 |
}; |
261 |
export const fetchLocalTitle = function (title_id) { |
145 |
export const fetchLocalTitle = function (title_id) { |
262 |
const apiUrl = "/api/v1/erm/eholdings/local/titles/" + title_id; |
146 |
const apiUrl = "/api/v1/erm/eholdings/local/titles/" + title_id; |
Lines 287-321
export const fetchLocalTitleCount = async function (filters) {
Link Here
|
287 |
_per_page: 1, |
171 |
_per_page: 1, |
288 |
...(q ? { q: JSON.stringify(q) } : {}), |
172 |
...(q ? { q: JSON.stringify(q) } : {}), |
289 |
}; |
173 |
}; |
290 |
let count_local_titles; |
|
|
291 |
var apiUrl = "/api/v1/erm/eholdings/local/titles"; |
174 |
var apiUrl = "/api/v1/erm/eholdings/local/titles"; |
292 |
await fetch(apiUrl + "?" + new URLSearchParams(params)) |
175 |
return await myFetchTotal(apiUrl + "?" + new URLSearchParams(params)); |
293 |
//.then(checkError) |
|
|
294 |
.then( |
295 |
(response) => |
296 |
(count_local_titles = response.headers.get("X-Total-Count")) |
297 |
); |
298 |
return count_local_titles; |
299 |
}; |
176 |
}; |
300 |
|
177 |
|
301 |
export const _fetchResource = async function (apiUrl, resource_id) { |
178 |
export const _fetchResource = async function (apiUrl, resource_id) { |
302 |
if (!resource_id) return; |
179 |
if (!resource_id) return; |
303 |
let resource; |
180 |
return await myFetch(apiUrl, { |
304 |
await fetch(apiUrl, { |
|
|
305 |
headers: { |
181 |
headers: { |
306 |
"x-koha-embed": "title,package,vendor", |
182 |
"x-koha-embed": "title,package,vendor", |
307 |
}, |
183 |
}, |
308 |
}) |
184 |
}); |
309 |
.then(checkError) |
|
|
310 |
.then( |
311 |
(result) => { |
312 |
resource = result; |
313 |
}, |
314 |
(error) => { |
315 |
setError(error); |
316 |
} |
317 |
); |
318 |
return resource; |
319 |
}; |
185 |
}; |
320 |
export const fetchLocalResource = function (resource_id) { |
186 |
export const fetchLocalResource = function (resource_id) { |
321 |
const apiUrl = "/api/v1/erm/eholdings/local/resources/" + resource_id; |
187 |
const apiUrl = "/api/v1/erm/eholdings/local/resources/" + resource_id; |
Lines 327-344
export const fetchEBSCOResource = function (resource_id) {
Link Here
|
327 |
}; |
193 |
}; |
328 |
|
194 |
|
329 |
export const _fetchResources = async function (apiUrl) { |
195 |
export const _fetchResources = async function (apiUrl) { |
330 |
let resources; |
196 |
return await myFetch(apiUrl); |
331 |
await fetch(apiUrl) |
|
|
332 |
.then(checkError) |
333 |
.then( |
334 |
(result) => { |
335 |
resources = result; |
336 |
}, |
337 |
(error) => { |
338 |
setError(error); |
339 |
} |
340 |
); |
341 |
return resources; |
342 |
}; |
197 |
}; |
343 |
|
198 |
|
344 |
export const fetchLocalResources = function () { |
199 |
export const fetchLocalResources = function () { |
Lines 350-361
export const fetchEBSCOResources = function () {
Link Here
|
350 |
return _fetchResources(apiUrl); |
205 |
return _fetchResources(apiUrl); |
351 |
}; |
206 |
}; |
352 |
|
207 |
|
353 |
export const checkError = function (response) { |
208 |
export const myFetch = async function (url, options, return_response) { |
|
|
209 |
let r; |
210 |
await fetch(url, options || {}) |
211 |
.then((response) => checkError(response, return_response)) |
212 |
.then( |
213 |
(result) => { |
214 |
r = result; |
215 |
}, |
216 |
(error) => { |
217 |
setError(error.toString()); |
218 |
} |
219 |
) |
220 |
.catch((error) => { |
221 |
setError(error); |
222 |
}); |
223 |
return r; |
224 |
}; |
225 |
export const myFetchTotal = async function (url, options) { |
226 |
let r; |
227 |
await myFetch(url, options, 1).then( |
228 |
(response) => { |
229 |
if (response) { |
230 |
r = response.headers.get("X-Total-Count"); |
231 |
} |
232 |
}, |
233 |
(error) => { |
234 |
setError(error.toString()); |
235 |
} |
236 |
); |
237 |
return r; |
238 |
}; |
239 |
|
240 |
export const checkError = function (response, return_response) { |
354 |
if (response.status >= 200 && response.status <= 299) { |
241 |
if (response.status >= 200 && response.status <= 299) { |
355 |
return response.json(); |
242 |
return return_response ? response : response.json(); |
356 |
} else { |
243 |
} else { |
357 |
console.log("Server returned an error:"); |
244 |
console.log("Server returned an error:"); |
358 |
console.log(response); |
245 |
console.log(response); |
359 |
setError("%s (%s)".format(response.statusText, response.status)); |
246 |
throw Error("%s (%s)".format(response.statusText, response.status)); |
360 |
} |
247 |
} |
361 |
}; |
248 |
}; |