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

(-)a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsFormAdd.vue (-16 / +26 lines)
Lines 198-203 import { Link Here
198
    submitted,
198
    submitted,
199
} from "../../messages"
199
} from "../../messages"
200
import { fetchAgreement } from "../../fetch/erm.js"
200
import { fetchAgreement } from "../../fetch/erm.js"
201
import { submitToAPI } from "../../fetch/fetch.js"
202
import { ApiClient } from "../../fetch/api-client.js";
201
import { storeToRefs } from "pinia"
203
import { storeToRefs } from "pinia"
202
204
203
export default {
205
export default {
Lines 340-345 export default { Link Here
340
342
341
            let apiUrl = "/api/v1/erm/agreements"
343
            let apiUrl = "/api/v1/erm/agreements"
342
344
345
            //This is just temporary for new api calls implementation
346
            let a_id = agreement.agreement_id;
347
343
            let method = "POST"
348
            let method = "POST"
344
            if (agreement.agreement_id) {
349
            if (agreement.agreement_id) {
345
                method = "PUT"
350
                method = "PUT"
Lines 390-412 export default { Link Here
390
                },
395
                },
391
            }
396
            }
392
397
393
            isSubmitting()
398
            const client = new ApiClient(
394
            fetch(apiUrl, options)
399
                "/api/v1/erm/",
395
                .then(response => {
400
                {
396
                    if (response.status == 200) {
401
                    "Content-Type": "application/json;charset=utf-8",
397
                        this.$router.push("/cgi-bin/koha/erm/agreements")
402
                }
398
                        setMessage(this.$__("Agreement updated"))
403
            );
399
                    } else if (response.status == 201) {
404
400
                        this.$router.push("/cgi-bin/koha/erm/agreements")
405
            (async () => {
401
                        setMessage(this.$__("Agreement created"))
406
                try {
402
                    } else {
407
                    if ( this.$route.path.includes("edit") ) {
403
                        setError(response.message || response.statusText)
408
                        await client.agreements.update(agreement, a_id);
409
                        setMessage(this.$__("Agreement updated"));
410
                    }else{
411
                        await client.agreements.create(agreement);
412
                        setMessage(this.$__("Agreement created"));
404
                    }
413
                    }
405
                })
414
                    this.$router.push("/cgi-bin/koha/erm/agreements")
406
                .catch(error => {
415
                } catch (err) {
407
                    setError(error)
416
                    setError(err.message || err.statusText)
408
                })
417
                }
409
                .then(() => submitted())
418
419
            })();
410
        },
420
        },
411
        onStatusChanged(e) {
421
        onStatusChanged(e) {
412
            if (e.authorised_value != "closed") {
422
            if (e.authorised_value != "closed") {
(-)a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/api-client.js (+21 lines)
Line 0 Link Here
1
import HttpClient from "./http-client";
2
3
export class ApiClient extends HttpClient {
4
  constructor(baseURL, headers) {
5
    super({
6
      baseURL,
7
      headers: headers
8
    });
9
  }
10
11
  get agreements() {
12
    return {
13
      get: () => this.get("/agreements"),
14
      delete: (id) => this.delete(`/agreements/${id}`),
15
      create: (agreement) => this.post("/agreements", agreement),
16
      update: (agreement, id) => this.put("/agreements/"+id, agreement)
17
    };
18
  }
19
}
20
21
export default ApiClient;
(-)a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/fetch.js (+26 lines)
Line 0 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 (-1 / +82 lines)
Line 0 Link Here
0
- 
1
class HttpClient {
2
    constructor(options = {}) {
3
      this._baseURL = options.baseURL || "";
4
      this._headers = options.headers || {};
5
    }
6
  
7
    async _fetchJSON(endpoint, options = {}) {
8
      const res = await fetch(this._baseURL + endpoint, {
9
        ...options,
10
        headers: this._headers
11
      });
12
  
13
      if (!res.ok) throw new Error(res.statusText);
14
  
15
      if (options.parseResponse !== false && res.status !== 204)
16
        return res.json();
17
  
18
      return undefined;
19
    }
20
  
21
    setHeader(key, value) {
22
      this._headers[key] = value;
23
      return this;
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,
43
        method: "GET"
44
      });
45
    }
46
  
47
    post(endpoint, body, options = {}) {
48
      return this._fetchJSON(endpoint, {
49
        ...options,
50
        body: body ? JSON.stringify(body) : undefined,
51
        method: "POST"
52
      });
53
    }
54
  
55
    put(endpoint, body, options = {}) {
56
        console.log('puting');
57
      return this._fetchJSON(endpoint, {
58
        ...options,
59
        body: body ? JSON.stringify(body) : undefined,
60
        method: "PUT"
61
      });
62
    }
63
  
64
    patch(endpoint, operations, options = {}) {
65
      return this._fetchJSON(endpoint, {
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,
76
        ...options,
77
        method: "DELETE"
78
      });
79
    }
80
  }
81
  
82
  export default HttpClient;

Return to bug 32939