Bug 32939: Use APIClient to fetch licenses
Signed-off-by: Matt Blenkinsop <matt.blenkinsop@ptfs-europe.com> Signed-off-by: Nick Clemens <nick@bywatersolutions.com> Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
parent
b8dac5b1e2
commit
23266f7b00
7 changed files with 100 additions and 87 deletions
|
@ -101,7 +101,8 @@
|
|||
</template>
|
||||
|
||||
<script>
|
||||
import { fetchLicenses } from "../../fetch/erm.js"
|
||||
import { APIClient } from "../../fetch/api-client.js"
|
||||
|
||||
export default {
|
||||
name: "AgreementLicenses",
|
||||
data() {
|
||||
|
@ -115,7 +116,14 @@ export default {
|
|||
agreement_licenses: Array,
|
||||
},
|
||||
beforeCreate() {
|
||||
fetchLicenses().then(licenses => (this.licenses = licenses))
|
||||
const client = APIClient.erm
|
||||
client.licenses.getAll.then(
|
||||
licenses => {
|
||||
this.licenses = licenses
|
||||
this.initialized = true
|
||||
},
|
||||
error => {}
|
||||
)
|
||||
},
|
||||
methods: {
|
||||
addLicense() {
|
||||
|
|
|
@ -149,8 +149,8 @@ import { inject } from "vue"
|
|||
import flatPickr from "vue-flatpickr-component"
|
||||
import UserRoles from "./UserRoles.vue"
|
||||
import Documents from "./Documents.vue"
|
||||
import { setMessage, setError, setWarning } from "../../messages"
|
||||
import { fetchLicense, checkError } from "../../fetch/erm.js"
|
||||
import { setMessage, setWarning } from "../../messages"
|
||||
import { APIClient } from "../../fetch/api-client.js"
|
||||
import { storeToRefs } from "pinia"
|
||||
|
||||
export default {
|
||||
|
@ -199,9 +199,11 @@ export default {
|
|||
},
|
||||
methods: {
|
||||
async getLicense(license_id) {
|
||||
const license = await fetchLicense(license_id)
|
||||
this.license = license
|
||||
this.initialized = true
|
||||
const client = APIClient.erm
|
||||
client.licenses.get(license_id).then(license => {
|
||||
this.license = license
|
||||
this.initialized = true
|
||||
})
|
||||
},
|
||||
checkForm(license) {
|
||||
let errors = []
|
||||
|
@ -229,6 +231,7 @@ export default {
|
|||
e.preventDefault()
|
||||
|
||||
let license = JSON.parse(JSON.stringify(this.license)) // copy
|
||||
let license_id = license.license_id
|
||||
|
||||
if (!this.checkForm(license)) {
|
||||
return false
|
||||
|
@ -256,35 +259,24 @@ export default {
|
|||
({ file_type, uploaded_on, ...keepAttrs }) => keepAttrs
|
||||
)
|
||||
|
||||
const options = {
|
||||
method: method,
|
||||
body: JSON.stringify(license),
|
||||
headers: {
|
||||
"Content-Type": "application/json;charset=utf-8",
|
||||
},
|
||||
}
|
||||
|
||||
fetch(apiUrl, options)
|
||||
.then(response => checkError(response, 1))
|
||||
.then(
|
||||
response => {
|
||||
if (response.status == 200) {
|
||||
this.$router.push("/cgi-bin/koha/erm/licenses")
|
||||
setMessage(this.$__("License updated"))
|
||||
} else if (response.status == 201) {
|
||||
this.$router.push("/cgi-bin/koha/erm/licenses")
|
||||
setMessage(this.$__("License created"))
|
||||
} else {
|
||||
setError(response.message || response.statusText)
|
||||
}
|
||||
const client = APIClient.erm
|
||||
if (license_id) {
|
||||
client.licenses.update(license, license_id).then(
|
||||
success => {
|
||||
setMessage(this.$__("License updated"))
|
||||
this.$router.push("/cgi-bin/koha/erm/licenses")
|
||||
},
|
||||
error => {
|
||||
setError(error)
|
||||
}
|
||||
error => {}
|
||||
)
|
||||
.catch(e => {
|
||||
console.log(e)
|
||||
})
|
||||
} else {
|
||||
client.licenses.create(license).then(
|
||||
success => {
|
||||
setMessage(this.$__("License created"))
|
||||
this.$router.push("/cgi-bin/koha/erm/licenses")
|
||||
},
|
||||
error => {}
|
||||
)
|
||||
}
|
||||
},
|
||||
},
|
||||
components: {
|
||||
|
|
|
@ -35,8 +35,8 @@
|
|||
</template>
|
||||
|
||||
<script>
|
||||
import { fetchLicense, checkError } from "../../fetch/erm.js"
|
||||
import { setMessage, setError } from "../../messages"
|
||||
import { APIClient } from "../../fetch/api-client.js"
|
||||
import { setMessage } from "../../messages"
|
||||
|
||||
export default {
|
||||
data() {
|
||||
|
@ -53,35 +53,23 @@ export default {
|
|||
},
|
||||
methods: {
|
||||
async getLicense(license_id) {
|
||||
const license = await fetchLicense(license_id)
|
||||
this.license = license
|
||||
this.initialized = true
|
||||
const client = APIClient.erm
|
||||
client.licenses.get(license_id).then(data => {
|
||||
this.license = data
|
||||
this.initialized = true
|
||||
})
|
||||
},
|
||||
onSubmit(e) {
|
||||
e.preventDefault()
|
||||
|
||||
let apiUrl = "/api/v1/erm/licenses/" + this.license.license_id
|
||||
|
||||
const options = {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
"Content-Type": "application/json;charset=utf-8",
|
||||
const client = APIClient.erm
|
||||
client.licenses.delete(this.license.license_id).then(
|
||||
success => {
|
||||
setMessage(this.$__("License deleted"))
|
||||
this.$router.push("/cgi-bin/koha/erm/licenses")
|
||||
},
|
||||
}
|
||||
|
||||
fetch(apiUrl, options)
|
||||
.then(response => checkError(response, 1))
|
||||
.then(response => {
|
||||
if (response.status == 204) {
|
||||
this.$router.push("/cgi-bin/koha/erm/licenses")
|
||||
setMessage(this.$__("License deleted"))
|
||||
} else {
|
||||
setError(response.message || response.statusText)
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
setError(error)
|
||||
})
|
||||
error => {}
|
||||
)
|
||||
},
|
||||
},
|
||||
name: "LicensesFormConfirmDelete",
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
import Toolbar from "./LicensesToolbar.vue"
|
||||
import { inject, createVNode, render } from "vue"
|
||||
import { storeToRefs } from "pinia"
|
||||
import { fetchLicenses } from "../../fetch/erm.js"
|
||||
import { APIClient } from "../../fetch/api-client.js"
|
||||
import { useDataTable } from "../../composables/datatables"
|
||||
|
||||
export default {
|
||||
|
@ -49,9 +49,14 @@ export default {
|
|||
},
|
||||
methods: {
|
||||
async getLicenses() {
|
||||
const licenses = await fetchLicenses()
|
||||
this.licenses = licenses
|
||||
this.initialized = true
|
||||
const client = APIClient.erm
|
||||
await client.licenses.getAll().then(
|
||||
licenses => {
|
||||
this.licenses = licenses
|
||||
this.initialized = true
|
||||
},
|
||||
error => {}
|
||||
)
|
||||
},
|
||||
show_license: function (license_id) {
|
||||
this.$router.push("/cgi-bin/koha/erm/licenses/" + license_id)
|
||||
|
|
|
@ -145,7 +145,7 @@
|
|||
|
||||
<script>
|
||||
import { inject } from "vue"
|
||||
import { fetchLicense } from "../../fetch/erm.js"
|
||||
import { APIClient } from "../../fetch/api-client.js"
|
||||
|
||||
export default {
|
||||
setup() {
|
||||
|
@ -185,9 +185,14 @@ export default {
|
|||
},
|
||||
methods: {
|
||||
async getLicense(license_id) {
|
||||
const license = await fetchLicense(license_id)
|
||||
this.license = license
|
||||
this.initialized = true
|
||||
const client = APIClient.erm
|
||||
client.licenses.get(license_id).then(
|
||||
license => {
|
||||
this.license = license
|
||||
this.initialized = true
|
||||
},
|
||||
error => {}
|
||||
)
|
||||
},
|
||||
},
|
||||
components: {},
|
||||
|
|
|
@ -38,6 +38,40 @@ export class ERMAPIClient extends HttpClient {
|
|||
//count: () => this.count("agreements"), //TODO: Implement count method
|
||||
};
|
||||
}
|
||||
|
||||
get licenses() {
|
||||
return {
|
||||
get: (id) =>
|
||||
this.get({
|
||||
endpoint: "licenses/" + id,
|
||||
headers: {
|
||||
"x-koha-embed":
|
||||
"user_roles,user_roles.patron,vendor,documents"
|
||||
},
|
||||
}),
|
||||
getAll: (query) =>
|
||||
this.get({
|
||||
endpoint: "licenses?" + (query || "_per_page=-1"),
|
||||
headers: {
|
||||
"x-koha-embed": "vendor.name",
|
||||
},
|
||||
}),
|
||||
delete: (id) =>
|
||||
this.delete({
|
||||
endpoint: "licenses/" + id,
|
||||
}),
|
||||
create: (license) =>
|
||||
this.post({
|
||||
endpoint: "licenses",
|
||||
body: license,
|
||||
}),
|
||||
update: (license, id) =>
|
||||
this.put({
|
||||
endpoint: "licenses/" + id,
|
||||
body: license,
|
||||
}),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default ERMAPIClient;
|
||||
|
|
|
@ -2,25 +2,6 @@ import { setError } from "../messages";
|
|||
|
||||
//TODO: all of these functions should be deleted and reimplemented in the components using ERMAPIClient
|
||||
|
||||
export const fetchLicense = function (license_id) {
|
||||
if (!license_id) return;
|
||||
const apiUrl = "/api/v1/erm/licenses/" + license_id;
|
||||
return myFetch(apiUrl, {
|
||||
headers: {
|
||||
"x-koha-embed": "user_roles,user_roles.patron,vendor,documents",
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export const fetchLicenses = function () {
|
||||
const apiUrl = "/api/v1/erm/licenses?_per_page=-1";
|
||||
return myFetch(apiUrl, {
|
||||
headers: {
|
||||
"x-koha-embed": "vendor.name",
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export const fetchPatron = function (patron_id) {
|
||||
if (!patron_id) return;
|
||||
const apiUrl = "/api/v1/patrons/" + patron_id;
|
||||
|
|
Loading…
Reference in a new issue