|
| 1 | +/** @odoo-module */ |
| 2 | + |
| 3 | +import { Component, onWillStart, useState } from "@odoo/owl"; |
| 4 | +import { useService } from "@web/core/utils/hooks"; |
| 5 | +import { KeepLast } from "@web/core/utils/concurrency"; |
| 6 | +import { fuzzyLookup } from "@web/core/utils/search"; |
| 7 | +import { Pager } from "@web/core/pager/pager"; |
| 8 | + |
| 9 | +export class CustomerList extends Component { |
| 10 | + static template = "awesome_kanban.CustomerList"; |
| 11 | + |
| 12 | + static components = { Pager }; |
| 13 | + |
| 14 | + static props = { |
| 15 | + selectCustomer: Function |
| 16 | + } |
| 17 | + |
| 18 | + setup() { |
| 19 | + this.orm = useService("orm"); |
| 20 | + this.customers = useState({ data: [] }); |
| 21 | + this.keepLast = new KeepLast(); |
| 22 | + this.state = useState({ displayActiveCustomers: false, searchString: ''}) |
| 23 | + onWillStart(async () => { |
| 24 | + this.customers.data = await this.loadCustomers([]); |
| 25 | + this.pager.total = this.customers.data.length; |
| 26 | + }) |
| 27 | + this.pager = useState({ offset: 0, limit: 20 }); |
| 28 | + } |
| 29 | + |
| 30 | + loadCustomers() { |
| 31 | + const domain = this.state.displayActiveCustomers ? [["opportunity_ids", "!=", false]] : []; |
| 32 | + return this.orm.searchRead("res.partner", domain, ["display_name"]); |
| 33 | + } |
| 34 | + |
| 35 | + get displayedCustomers() { |
| 36 | + const displayCustomers = this.filterCustomers(this.state.searchString); |
| 37 | + this.pager.total = displayCustomers.length; |
| 38 | + return displayCustomers.slice(this.pager.offset, this.pager.offset + this.pager.limit); |
| 39 | + } |
| 40 | + |
| 41 | + async checkboxChange(ev) { |
| 42 | + this.pager.offset = 0; |
| 43 | + this.state.displayActiveCustomers = ev.target.checked; |
| 44 | + this.customers.data = await this.keepLast.add(this.loadCustomers()); |
| 45 | + this.pager.total = this.customers.data.length; |
| 46 | + } |
| 47 | + |
| 48 | + filterCustomers(name) { |
| 49 | + if (name) { |
| 50 | + const filteredCustomers = fuzzyLookup( |
| 51 | + name, |
| 52 | + this.customers.data, |
| 53 | + (customer) => customer.display_name |
| 54 | + ); |
| 55 | + while (this.pager.offset > filteredCustomers.length) this.pager.offset -= this.pager.limit; |
| 56 | + return filteredCustomers; |
| 57 | + } else { |
| 58 | + return this.customers.data; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + onUpdatePager(data) { |
| 63 | + Object.assign(this.pager, data); |
| 64 | + } |
| 65 | + |
| 66 | +} |
0 commit comments