View | Details | Raw Unified | Return to bug 32939
Collapse All | Expand All

(-)a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsFormAdd.vue (-34 / +27 lines)
Lines 190-196 import AgreementLicenses from "./AgreementLicenses.vue" Link Here
190
import AgreementRelationships from "./AgreementRelationships.vue"
190
import AgreementRelationships from "./AgreementRelationships.vue"
191
import Documents from "./Documents.vue"
191
import Documents from "./Documents.vue"
192
import { setMessage, setError, setWarning } from "../../messages"
192
import { setMessage, setError, setWarning } from "../../messages"
193
import { fetchAgreement, checkError } from "../../fetch/erm.js"
193
import { ERMAPIClient } from "../../fetch/erm-api-client.js"
194
import { storeToRefs } from "pinia"
194
import { storeToRefs } from "pinia"
195
195
196
export default {
196
export default {
Lines 245-251 export default { Link Here
245
    beforeRouteEnter(to, from, next) {
245
    beforeRouteEnter(to, from, next) {
246
        next(vm => {
246
        next(vm => {
247
            if (to.params.agreement_id) {
247
            if (to.params.agreement_id) {
248
                vm.agreement = vm.getAgreement(to.params.agreement_id)
248
                vm.getAgreement(to.params.agreement_id)
249
            } else {
249
            } else {
250
                vm.initialized = true
250
                vm.initialized = true
251
            }
251
            }
Lines 253-261 export default { Link Here
253
    },
253
    },
254
    methods: {
254
    methods: {
255
        async getAgreement(agreement_id) {
255
        async getAgreement(agreement_id) {
256
            const agreement = await fetchAgreement(agreement_id)
256
            const client = new ERMAPIClient()
257
            this.agreement = agreement
257
            try {
258
            this.initialized = true
258
                await client.agreements.get(agreement_id).then(data => {
259
                    this.agreement = data
260
                    this.initialized = true
261
                })
262
            } catch (err) {
263
                setError(err.message || err.statusText)
264
            }
259
        },
265
        },
260
        checkForm(agreement) {
266
        checkForm(agreement) {
261
            let errors = []
267
            let errors = []
Lines 326-343 export default { Link Here
326
332
327
            //let agreement= Object.assign( {} ,this.agreement); // copy
333
            //let agreement= Object.assign( {} ,this.agreement); // copy
328
            let agreement = JSON.parse(JSON.stringify(this.agreement)) // copy
334
            let agreement = JSON.parse(JSON.stringify(this.agreement)) // copy
335
            let agreement_id = agreement.agreement_id
329
336
330
            if (!this.checkForm(agreement)) {
337
            if (!this.checkForm(agreement)) {
331
                return false
338
                return false
332
            }
339
            }
333
340
334
            let apiUrl = "/api/v1/erm/agreements"
335
336
            let method = "POST"
337
            if (agreement.agreement_id) {
338
                method = "PUT"
339
                apiUrl += "/" + agreement.agreement_id
340
            }
341
            delete agreement.agreement_id
341
            delete agreement.agreement_id
342
            delete agreement.vendor
342
            delete agreement.vendor
343
            agreement.is_perpetual = agreement.is_perpetual ? true : false
343
            agreement.is_perpetual = agreement.is_perpetual ? true : false
Lines 375-404 export default { Link Here
375
375
376
            delete agreement.agreement_packages
376
            delete agreement.agreement_packages
377
377
378
            const options = {
378
            const client = new ERMAPIClient()
379
                method: method,
379
            ;(async () => {
380
                body: JSON.stringify(agreement),
380
                try {
381
                headers: {
381
                    if (agreement_id) {
382
                    "Content-Type": "application/json;charset=utf-8",
382
                        await client.agreements
383
                },
383
                            .update(agreement, agreement_id)
384
            }
384
                            .then(setMessage(this.$__("Agreement updated")))
385
386
            fetch(apiUrl, options)
387
                .then(response => checkError(response, 1))
388
                .then(response => {
389
                    if (response.status == 200) {
390
                        this.$router.push("/cgi-bin/koha/erm/agreements")
391
                        setMessage(this.$__("Agreement updated"))
392
                    } else if (response.status == 201) {
393
                        this.$router.push("/cgi-bin/koha/erm/agreements")
394
                        setMessage(this.$__("Agreement created"))
395
                    } else {
385
                    } else {
396
                        setError(response.message || response.statusText)
386
                        await client.agreements
387
                            .create(agreement)
388
                            .then(setMessage(this.$__("Agreement created")))
397
                    }
389
                    }
398
                })
390
                    this.$router.push("/cgi-bin/koha/erm/agreements")
399
                .catch(error => {
391
                } catch (err) {
400
                    setError(error)
392
                    setError(err.message || err.statusText)
401
                })
393
                }
394
            })()
402
        },
395
        },
403
        onStatusChanged(e) {
396
        onStatusChanged(e) {
404
            if (e.authorised_value != "closed") {
397
            if (e.authorised_value != "closed") {
(-)a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsFormConfirmDelete.vue (-26 / +21 lines)
Lines 36-42 Link Here
36
</template>
36
</template>
37
37
38
<script>
38
<script>
39
import { fetchAgreement, checkError } from "../../fetch/erm.js"
39
import { ERMAPIClient } from "../../fetch/erm-api-client.js"
40
import { setMessage, setError } from "../../messages"
40
import { setMessage, setError } from "../../messages"
41
41
42
export default {
42
export default {
Lines 53-87 export default { Link Here
53
    },
53
    },
54
    methods: {
54
    methods: {
55
        async getAgreement(agreement_id) {
55
        async getAgreement(agreement_id) {
56
            const agreement = await fetchAgreement(agreement_id)
56
            const client = new ERMAPIClient()
57
            this.agreement = agreement
57
            try {
58
            this.initialized = true
58
                await client.agreements.get(agreement_id).then(data => {
59
                    this.agreement = data
60
                    this.initialized = true
61
                })
62
            } catch (err) {
63
                setError(err.message || err.statusText)
64
            }
59
        },
65
        },
60
        onSubmit(e) {
66
        onSubmit(e) {
61
            e.preventDefault()
67
            e.preventDefault()
62
68
63
            let apiUrl = "/api/v1/erm/agreements/" + this.agreement.agreement_id
69
            const client = new ERMAPIClient()
64
70
            ;(async () => {
65
            const options = {
71
                try {
66
                method: "DELETE",
72
                    await client.agreements
67
                headers: {
73
                        .delete(this.agreement.agreement_id)
68
                    "Content-Type": "application/json;charset=utf-8",
74
                        .then(setMessage(this.$__("Agreement deleted")))
69
                },
75
                    this.$router.push("/cgi-bin/koha/erm/agreements")
70
            }
76
                } catch (err) {
71
77
                    setError(err)
72
            fetch(apiUrl, options)
78
                }
73
                .then(response => checkError(response, 1))
79
            })()
74
                .then(response => {
75
                    if (response.status == 204) {
76
                        setMessage(this.$__("Agreement deleted"))
77
                        this.$router.push("/cgi-bin/koha/erm/agreements")
78
                    } else {
79
                        setError(response.message || response.statusText)
80
                    }
81
                })
82
                .catch(error => {
83
                    setError(error)
84
                })
85
        },
80
        },
86
    },
81
    },
87
    name: "AgreementsFormConfirmDelete",
82
    name: "AgreementsFormConfirmDelete",
(-)a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsList.vue (-2 / +3 lines)
Lines 37-44 Link Here
37
import flatPickr from "vue-flatpickr-component"
37
import flatPickr from "vue-flatpickr-component"
38
import Toolbar from "./AgreementsToolbar.vue"
38
import Toolbar from "./AgreementsToolbar.vue"
39
import { inject, createVNode, render } from "vue"
39
import { inject, createVNode, render } from "vue"
40
import { ERMAPIClient } from "../../fetch/erm-api-client.js"
40
import { storeToRefs } from "pinia"
41
import { storeToRefs } from "pinia"
41
import { fetchAgreements } from "../../fetch/erm.js"
42
import { useDataTable, build_url } from "../../composables/datatables"
42
import { useDataTable, build_url } from "../../composables/datatables"
43
43
44
export default {
44
export default {
Lines 93-99 export default { Link Here
93
    },
93
    },
94
    methods: {
94
    methods: {
95
        async getAgreements() {
95
        async getAgreements() {
96
            const agreements = await fetchAgreements()
96
            const client = new ERMAPIClient()
97
            const agreements = await client.agreements.getAll()
97
            this.agreements = agreements
98
            this.agreements = agreements
98
            this.initialized = true
99
            this.initialized = true
99
        },
100
        },
(-)a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsShow.vue (-4 / +11 lines)
Lines 300-306 Link Here
300
300
301
<script>
301
<script>
302
import { inject } from "vue"
302
import { inject } from "vue"
303
import { fetchAgreement } from "../../fetch/erm.js"
303
import { ERMAPIClient } from "../../fetch/erm-api-client.js"
304
import { setError } from "../../messages"
304
305
305
export default {
306
export default {
306
    setup() {
307
    setup() {
Lines 346-354 export default { Link Here
346
    },
347
    },
347
    methods: {
348
    methods: {
348
        async getAgreement(agreement_id) {
349
        async getAgreement(agreement_id) {
349
            const agreement = await fetchAgreement(agreement_id)
350
            const client = new ERMAPIClient()
350
            this.agreement = agreement
351
            try {
351
            this.initialized = true
352
                await client.agreements.get(agreement_id).then(data => {
353
                    this.agreement = data
354
                    this.initialized = true
355
                })
356
            } catch (err) {
357
                setError(err.message || err.statusText)
358
            }
352
        },
359
        },
353
    },
360
    },
354
    name: "AgreementsShow",
361
    name: "AgreementsShow",
(-)a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/erm-api-client.js (+46 lines)
Line 0 Link Here
1
import HttpClient from "./http-client";
2
3
export class ERMAPIClient extends HttpClient {
4
    constructor() {
5
        super({
6
            baseURL: "/api/v1/erm/",
7
        });
8
    }
9
10
    get agreements() {
11
        return {
12
            get: (id) =>
13
                this.get({
14
                    endpoint: "agreements/" + id,
15
                    headers: {
16
                        "x-koha-embed":
17
                            "periods,user_roles,user_roles.patron,agreement_licenses,agreement_licenses.license,agreement_relationships,agreement_relationships.related_agreement,documents,agreement_packages,agreement_packages.package,vendor",
18
                    },
19
                }),
20
            getAll: (query) =>
21
                this.get({
22
                    endpoint: "agreements?" + (query || "_per_page=-1"),
23
                }),
24
            delete: (id) =>
25
                this.delete({
26
                    endpoint: "agreements/" + id,
27
                    headers: this.getDefaultJSONPayloadHeader(),
28
                }),
29
            create: (agreement) =>
30
                this.post({
31
                    endpoint: "agreements",
32
                    body: agreement,
33
                    headers: this.getDefaultJSONPayloadHeader(),
34
                }),
35
            update: (agreement, id) =>
36
                this.put({
37
                    endpoint: "agreements/" + id,
38
                    body: agreement,
39
                    headers: this.getDefaultJSONPayloadHeader(),
40
                }),
41
            //count: () => this.count("agreements"), //TODO: Implement count method
42
        };
43
    }
44
}
45
46
export default ERMAPIClient;
(-)a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/erm.js (-15 / +1 lines)
Lines 1-20 Link Here
1
import { setError } from "../messages";
1
import { setError } from "../messages";
2
2
3
export const fetchAgreement = function (agreement_id) {
3
//TODO: all of these functions should be deleted and reimplemented in the components using ERMAPIClient
4
    if (!agreement_id) return;
5
    const apiUrl = "/api/v1/erm/agreements/" + agreement_id;
6
    return myFetch(apiUrl, {
7
        headers: {
8
            "x-koha-embed":
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",
10
        },
11
    });
12
};
13
14
export const fetchAgreements = function () {
15
    const apiUrl = "/api/v1/erm/agreements?_per_page=-1";
16
    return myFetch(apiUrl);
17
};
18
4
19
export const fetchLicense = function (license_id) {
5
export const fetchLicense = function (license_id) {
20
    if (!license_id) return;
6
    if (!license_id) return;
(-)a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/http-client.js (-1 / +59 lines)
Line 0 Link Here
0
- 
1
class HttpClient {
2
    constructor(options = {}) {
3
        this._baseURL = options.baseURL || "";
4
    }
5
6
    async _fetchJSON(endpoint, headers = {}, options = {}) {
7
        const res = await fetch(this._baseURL + endpoint, {
8
            ...options,
9
            headers: headers,
10
        });
11
12
        if (!res.ok) throw new Error(res.statusText);
13
14
        if (options.parseResponse !== false && res.status !== 204)
15
            return res.json();
16
17
        return undefined;
18
    }
19
20
    get(params = {}) {
21
        console.log(params);
22
        return this._fetchJSON(params.endpoint, params.headers, {
23
            ...params.options,
24
            method: "GET",
25
        });
26
    }
27
28
    post(params = {}) {
29
        return this._fetchJSON(params.endpoint, params.headers, {
30
            ...params.options,
31
            body: params.body ? JSON.stringify(params.body) : undefined,
32
            method: "POST",
33
        });
34
    }
35
36
    put(params = {}) {
37
        return this._fetchJSON(params.endpoint, params.headers, {
38
            ...params.options,
39
            body: params.body ? JSON.stringify(params.body) : undefined,
40
            method: "PUT",
41
        });
42
    }
43
44
    delete(params = {}) {
45
        return this._fetchJSON(params.endpoint, params.headers, {
46
            parseResponse: false,
47
            ...params.options,
48
            method: "DELETE",
49
        });
50
    }
51
52
    //TODO: Implement count method
53
54
    getDefaultJSONPayloadHeader() {
55
        return { "Content-Type": "application/json;charset=utf-8" };
56
    }
57
}
58
59
export default HttpClient;

Return to bug 32939