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

(-)a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementRelationships.vue (-2 / +3 lines)
Lines 85-91 Link Here
85
</template>
85
</template>
86
86
87
<script>
87
<script>
88
import { fetchAgreements } from "../../fetch/erm.js"
88
import { ERMAPIClient } from "../../fetch/api-client.js";
89
89
90
export default {
90
export default {
91
    data() {
91
    data() {
Lines 94-100 export default { Link Here
94
        }
94
        }
95
    },
95
    },
96
    beforeCreate() {
96
    beforeCreate() {
97
        fetchAgreements().then(agreements => {
97
        const client = new ERMAPIClient()
98
        const agreements = client.agreements.getAll("_per_page=-1").then(agreements => {
98
            this.agreements = agreements.filter(
99
            this.agreements = agreements.filter(
99
                agreement => agreement.agreement_id !== this.agreement_id
100
                agreement => agreement.agreement_id !== this.agreement_id
100
            )
101
            )
(-)a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsFormAdd.vue (-40 / +14 lines)
Lines 194-205 import { Link Here
194
    setMessage,
194
    setMessage,
195
    setError,
195
    setError,
196
    setWarning,
196
    setWarning,
197
    isSubmitting,
198
    submitted,
199
} from "../../messages"
197
} from "../../messages"
200
import { fetchAgreement } from "../../fetch/erm.js"
198
import { ERMAPIClient } from "../../fetch/api-client.js";
201
import { submitToAPI } from "../../fetch/fetch.js"
202
import { ApiClient } from "../../fetch/api-client.js";
203
import { storeToRefs } from "pinia"
199
import { storeToRefs } from "pinia"
204
200
205
export default {
201
export default {
Lines 254-260 export default { Link Here
254
    beforeRouteEnter(to, from, next) {
250
    beforeRouteEnter(to, from, next) {
255
        next(vm => {
251
        next(vm => {
256
            if (to.params.agreement_id) {
252
            if (to.params.agreement_id) {
257
                vm.agreement = vm.getAgreement(to.params.agreement_id)
253
                vm.getAgreement(to.params.agreement_id)
258
            } else {
254
            } else {
259
                vm.initialized = true
255
                vm.initialized = true
260
            }
256
            }
Lines 262-270 export default { Link Here
262
    },
258
    },
263
    methods: {
259
    methods: {
264
        async getAgreement(agreement_id) {
260
        async getAgreement(agreement_id) {
265
            const agreement = await fetchAgreement(agreement_id)
261
            const client = new ERMAPIClient();
266
            this.agreement = agreement
262
            try {
267
            this.initialized = true
263
                client.agreements.get(agreement_id).then(data => { this.agreement = data; this.initialized = true; } );
264
            } catch (err) {
265
                setError(err.message || err.statusText)
266
            }
268
        },
267
        },
269
        checkForm(agreement) {
268
        checkForm(agreement) {
270
            let errors = []
269
            let errors = []
Lines 335-355 export default { Link Here
335
334
336
            //let agreement= Object.assign( {} ,this.agreement); // copy
335
            //let agreement= Object.assign( {} ,this.agreement); // copy
337
            let agreement = JSON.parse(JSON.stringify(this.agreement)) // copy
336
            let agreement = JSON.parse(JSON.stringify(this.agreement)) // copy
337
            let agreement_id = agreement.agreement_id;
338
338
339
            if (!this.checkForm(agreement)) {
339
            if (!this.checkForm(agreement)) {
340
                return false
340
                return false
341
            }
341
            }
342
342
343
            let apiUrl = "/api/v1/erm/agreements"
344
345
            //This is just temporary for new api calls implementation
346
            let a_id = agreement.agreement_id;
347
348
            let method = "POST"
349
            if (agreement.agreement_id) {
350
                method = "PUT"
351
                apiUrl += "/" + agreement.agreement_id
352
            }
353
            delete agreement.agreement_id
343
            delete agreement.agreement_id
354
            delete agreement.vendor
344
            delete agreement.vendor
355
            agreement.is_perpetual = agreement.is_perpetual ? true : false
345
            agreement.is_perpetual = agreement.is_perpetual ? true : false
Lines 387-422 export default { Link Here
387
377
388
            delete agreement.agreement_packages
378
            delete agreement.agreement_packages
389
379
390
            const options = {
380
            const client = new ERMAPIClient();
391
                method: method,
392
                body: JSON.stringify(agreement),
393
                headers: {
394
                    "Content-Type": "application/json;charset=utf-8",
395
                },
396
            }
397
398
            const client = new ApiClient(
399
                "/api/v1/erm/",
400
                {
401
                    "Content-Type": "application/json;charset=utf-8",
402
                }
403
            );
404
405
            (async () => {
381
            (async () => {
406
                try {
382
                try {
407
                    if ( this.$route.path.includes("edit") ) {
383
                    if ( agreement_id ) {
408
                        await client.agreements.update(agreement, a_id);
384
                        await client.agreements.update(agreement, agreement_id).then(setMessage(this.$__("Agreement updated")));
409
                        setMessage(this.$__("Agreement updated"));
410
                    }else{
385
                    }else{
411
                        await client.agreements.create(agreement);
386
                        await client.agreements.create(agreement).then(setMessage(this.$__("Agreement created")));
412
                        setMessage(this.$__("Agreement created"));
413
                    }
387
                    }
414
                    this.$router.push("/cgi-bin/koha/erm/agreements")
388
                    this.$router.push("/cgi-bin/koha/erm/agreements")
415
                } catch (err) {
389
                } catch (err) {
416
                    setError(err.message || err.statusText)
390
                    setError(err.message || err.statusText)
417
                }
391
                }
418
419
            })();
392
            })();
393
420
        },
394
        },
421
        onStatusChanged(e) {
395
        onStatusChanged(e) {
422
            if (e.authorised_value != "closed") {
396
            if (e.authorised_value != "closed") {
(-)a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsFormConfirmDelete.vue (-27 / +17 lines)
Lines 32-39 Link Here
32
</template>
32
</template>
33
33
34
<script>
34
<script>
35
import { fetchAgreement } from "../../fetch/erm.js"
35
import { ERMAPIClient } from "../../fetch/api-client.js";
36
import { setMessage, setError, isSubmitting, submitted } from "../../messages"
36
import { setMessage, setError } from "../../messages"
37
import ButtonSubmit from "../ButtonSubmit.vue"
37
import ButtonSubmit from "../ButtonSubmit.vue"
38
38
39
export default {
39
export default {
Lines 50-85 export default { Link Here
50
    },
50
    },
51
    methods: {
51
    methods: {
52
        async getAgreement(agreement_id) {
52
        async getAgreement(agreement_id) {
53
            const agreement = await fetchAgreement(agreement_id)
53
            const client = new ERMAPIClient();
54
            this.agreement = agreement
54
            try {
55
            this.initialized = true
55
                client.agreements.get(agreement_id).then(data => { this.agreement = data; this.initialized = true; } );
56
            } catch (err) {
57
                setError(err.message || err.statusText)
58
            }
56
        },
59
        },
57
        onSubmit(e) {
60
        onSubmit(e) {
58
            e.preventDefault()
61
            e.preventDefault()
59
62
60
            let apiUrl = "/api/v1/erm/agreements/" + this.agreement.agreement_id
63
            const client = new ERMAPIClient();
61
64
            (async () => {
62
            const options = {
65
                try {
63
                method: "DELETE",
66
                    await client.agreements.delete(this.agreement.agreement_id).then(setMessage(this.$__("Agreement deleted")));
64
                headers: {
67
                    this.$router.push("/cgi-bin/koha/erm/agreements")
65
                    "Content-Type": "application/json;charset=utf-8",
68
                } catch (err) {
66
                },
69
                    setError(err)
67
            }
70
                }
71
            })();
68
72
69
            isSubmitting()
70
            fetch(apiUrl, options)
71
                .then(response => {
72
                    if (response.status == 204) {
73
                        setMessage(this.$__("Agreement deleted"))
74
                        this.$router.push("/cgi-bin/koha/erm/agreements")
75
                    } else {
76
                        setError(response.message || response.statusText)
77
                    }
78
                })
79
                .then(() => submitted())
80
                .catch(error => {
81
                    setError(error)
82
                })
83
        },
73
        },
84
    },
74
    },
85
    components: {
75
    components: {
(-)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/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("_per_page=-1")
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 / +8 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/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 => { this.agreement = data; this.initialized = true; } );
353
            } catch (err) {
354
                setError(err.message || err.statusText)
355
            }
352
        },
356
        },
353
    },
357
    },
354
    name: "AgreementsShow",
358
    name: "AgreementsShow",
(-)a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/api-client.js (-9 / +34 lines)
Lines 1-21 Link Here
1
import HttpClient from "./http-client";
1
import HttpClient from "./http-client";
2
2
3
export class ApiClient extends HttpClient {
3
export class ERMAPIClient extends HttpClient {
4
  constructor(baseURL, headers) {
4
  constructor(options = {})
5
  {
5
    super({
6
    super({
6
      baseURL,
7
      baseURL: "/api/v1/erm/",
7
      headers: headers
8
      headers: options.headers
8
    });
9
    });
9
  }
10
  }
10
11
11
  get agreements() {
12
  get agreements() {
12
    return {
13
    return {
13
      get: () => this.get("/agreements"),
14
      get: (id) => this.get(
14
      delete: (id) => this.delete(`/agreements/${id}`),
15
        "agreements/"+id,
15
      create: (agreement) => this.post("/agreements", agreement),
16
          {
16
      update: (agreement, id) => this.put("/agreements/"+id, agreement)
17
            "x-koha-embed":
18
                "periods,user_roles,user_roles.patron,agreement_licenses,agreement_licenses.license,agreement_relationships,agreement_relationships.related_agreement,documents,agreement_packages,agreement_packages.package,vendor",
19
          }
20
      ),
21
      getAll: (query) => this.get("agreements"+(query?"?"+query:"")),
22
      count: () => this.count("agreements"),
23
      delete: (id) => this.delete(
24
        "agreements/"+id,
25
        {
26
          "Content-Type": "application/json;charset=utf-8",
27
        }
28
      ),
29
      create: (agreement) => this.post(
30
        "agreements",
31
        agreement,
32
        {
33
          "Content-Type": "application/json;charset=utf-8"
34
        }
35
      ),
36
      update: (agreement, id) => this.put(
37
        "agreements/"+id, agreement,
38
        {
39
          "Content-Type": "application/json;charset=utf-8"
40
        }
41
      )
17
    };
42
    };
18
  }
43
  }
19
}
44
}
20
45
21
export default ApiClient;
46
export default ERMAPIClient;
(-)a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/erm.js (-21 / +1 lines)
Lines 1-26 Link Here
1
import { setError } from "../messages";
1
import { setError } from "../messages";
2
2
3
export const fetchAgreement = async 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
    let agreement;
7
    await fetch(apiUrl, {
8
        headers: {
9
            "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",
11
        },
12
    })
13
        .then(checkError)
14
        .then(
15
            (result) => {
16
                agreement = result;
17
            },
18
            (error) => {
19
                setError(error);
20
            }
21
        );
22
    return agreement;
23
};
24
4
25
export const fetchAgreements = async function () {
5
export const fetchAgreements = async function () {
26
    const apiUrl = "/api/v1/erm/agreements?_per_page=-1";
6
    const apiUrl = "/api/v1/erm/agreements?_per_page=-1";
(-)a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/fetch.js (-26 lines)
Lines 1-26 Link Here
1
import {
2
    setMessage,
3
    setError,
4
    isSubmitting,
5
    submitted,
6
} from "../messages"
7
8
export const submitToAPI = async function (args) {
9
    isSubmitting()
10
    await fetch(args.apiUrl, args.options)
11
        .then(response => {
12
            if (response.status == 200) {
13
                args.router.push(args.success_redirect)
14
                setMessage(args.updated_message)
15
            } else if (response.status == 201) {
16
                args.router.push(args.success_redirect)
17
                setMessage(args.created_message)
18
            } else {
19
                setError(response.message || response.statusText)
20
            }
21
        })
22
        .catch(error => {
23
            setError(error)
24
        })
25
        .then(() => submitted())
26
};
(-)a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/http-client.js (-43 / +23 lines)
Lines 1-77 Link Here
1
import {
2
  isSubmitting,
3
  submitted,
4
} from "../messages"
5
1
class HttpClient {
6
class HttpClient {
2
    constructor(options = {}) {
7
    constructor(options = {}) {
3
      this._baseURL = options.baseURL || "";
8
      this._baseURL = options.baseURL || "";
4
      this._headers = options.headers || {};
9
      this._headers = options.headers || {};
5
    }
10
    }
6
  
11
  
7
    async _fetchJSON(endpoint, options = {}) {
12
    async _fetchJSON(endpoint, headers = {}, options = {}) {
13
      // isSubmitting() //FIXME: need to fix below submitted() call
8
      const res = await fetch(this._baseURL + endpoint, {
14
      const res = await fetch(this._baseURL + endpoint, {
9
        ...options,
15
        ...options,
10
        headers: this._headers
16
        headers: headers
11
      });
17
      })
12
  
18
      // .then(() => submitted()); //FIXME: seemingly working but throws error on console?
19
13
      if (!res.ok) throw new Error(res.statusText);
20
      if (!res.ok) throw new Error(res.statusText);
14
  
21
  
15
      if (options.parseResponse !== false && res.status !== 204)
22
      if (options.parseResponse !== false && res.status !== 204)
16
        return res.json();
23
        return res.json();
17
  
24
18
      return undefined;
25
      return undefined;
19
    }
26
    }
20
  
27
  
21
    setHeader(key, value) {
28
    get(endpoint, headers = {}, options = {}) {
22
      this._headers[key] = value;
29
      return this._fetchJSON(endpoint, headers,
23
      return this;
30
      {
24
    }
25
  
26
    getHeader(key) {
27
      return this._headers[key];
28
    }
29
  
30
    setBasicAuth(username, password) {
31
      this._headers.Authorization = `Basic ${btoa(`${username}:${password}`)}`;
32
      return this;
33
    }
34
  
35
    setBearerAuth(token) {
36
      this._headers.Authorization = `Bearer ${token}`;
37
      return this;
38
    }
39
  
40
    get(endpoint, options = {}) {
41
      return this._fetchJSON(endpoint, {
42
        ...options,
31
        ...options,
43
        method: "GET"
32
        method: "GET"
44
      });
33
      });
45
    }
34
    }
35
36
    //TODO: Implement count method
46
  
37
  
47
    post(endpoint, body, options = {}) {
38
    post(endpoint, body, headers = {}, options = {}) {
48
      return this._fetchJSON(endpoint, {
39
      return this._fetchJSON(endpoint, headers, {
49
        ...options,
40
        ...options,
50
        body: body ? JSON.stringify(body) : undefined,
41
        body: body ? JSON.stringify(body) : undefined,
51
        method: "POST"
42
        method: "POST"
52
      });
43
      });
53
    }
44
    }
54
  
45
  
55
    put(endpoint, body, options = {}) {
46
    put(endpoint, body, headers = {}, options = {}) {
56
        console.log('puting');
47
      return this._fetchJSON(endpoint, headers, {
57
      return this._fetchJSON(endpoint, {
58
        ...options,
48
        ...options,
59
        body: body ? JSON.stringify(body) : undefined,
49
        body: body ? JSON.stringify(body) : undefined,
60
        method: "PUT"
50
        method: "PUT"
61
      });
51
      });
62
    }
52
    }
63
  
53
  
64
    patch(endpoint, operations, options = {}) {
54
    delete(endpoint, headers = {}, options = {}) {
65
      return this._fetchJSON(endpoint, {
55
      return this._fetchJSON(endpoint, headers, {
66
        parseResponse: false,
67
        ...options,
68
        body: JSON.stringify(operations),
69
        method: "PATCH"
70
      });
71
    }
72
  
73
    delete(endpoint, options = {}) {
74
      return this._fetchJSON(endpoint, {
75
        parseResponse: false,
56
        parseResponse: false,
76
        ...options,
57
        ...options,
77
        method: "DELETE"
58
        method: "DELETE"
78
- 

Return to bug 32939