Use mixins useSearchCustomer

Prefer composition vs. inheritance
This commit is contained in:
Jean-Baptiste Bellet
2022-05-31 08:57:11 +02:00
parent 760de3b507
commit 39b7f56e6d
2 changed files with 24 additions and 19 deletions

View File

@@ -0,0 +1,22 @@
export const useSearchCustomer = (controller) => {
Object.assign(controller, {
load: function (query, callback) {
var params = {
q: query,
distributor_id: this.distributorValue,
};
fetch("/admin/search/customers.json?" + new URLSearchParams(params))
.then((response) => response.json())
.then((json) => {
this.items = json;
callback(json);
})
.catch((error) => {
this.items = [];
console.log(error);
callback();
});
},
});
};

View File

@@ -1,9 +1,11 @@
import TomSelectController from "./tom_select_controller";
import { useSearchCustomer } from "./mixins/useSearchCustomer";
export default class extends TomSelectController {
static values = { options: Object, distributor: Number };
connect() {
useSearchCustomer(this);
const options = {
valueField: "id",
labelField: "email",
@@ -19,25 +21,6 @@ export default class extends TomSelectController {
this.items = [];
}
load(query, callback) {
var params = {
q: query,
distributor_id: this.distributorValue,
};
fetch("/admin/search/customers.json?" + new URLSearchParams(params))
.then((response) => response.json())
.then((json) => {
this.items = json;
callback(json);
})
.catch((error) => {
this.items = [];
console.log(error);
callback();
});
}
renderOption(item, escape) {
if (!item.bill_address) {
return this.renderWithNoBillAddress(item, escape);