| 1 |
import { Interaction } from "@web/public/interaction"; |
| 2 |
import { registry } from "@web/core/registry"; |
| 3 |
import { rpc } from "@web/core/network/rpc"; |
| 4 |
|
| 5 |
export class PortalHomeCounters extends Interaction { |
| 6 |
static selector = ".o_portal_my_home"; |
| 7 |
|
| 8 |
async willStart() { |
| 9 |
return this.updateCounters(); |
| 10 |
} |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
getCountersAlwaysDisplayed() { |
| 18 |
return []; |
| 19 |
} |
| 20 |
|
| 21 |
async updateCounters() { |
| 22 |
const needed = Object.values(this.el.querySelectorAll("[data-placeholder_count]")).map( |
| 23 |
(documentsCounterEl) => documentsCounterEl.dataset["placeholder_count"] |
| 24 |
); |
| 25 |
const numberRpc = Math.min(Math.ceil(needed.length / 5), 3); |
| 26 |
const counterByRpc = Math.ceil(needed.length / numberRpc); |
| 27 |
const countersAlwaysDisplayed = this.getCountersAlwaysDisplayed(); |
| 28 |
|
| 29 |
const proms = [...Array(Math.min(numberRpc, needed.length)).keys()].map(async (i) => { |
| 30 |
const documentsCountersData = await rpc("/my/counters", { |
| 31 |
counters: needed.slice(i * counterByRpc, (i + 1) * counterByRpc), |
| 32 |
}); |
| 33 |
Object.keys(documentsCountersData).forEach((counterName) => { |
| 34 |
const documentsCounterEl = this.el.querySelector( |
| 35 |
`[data-placeholder_count='${counterName}']` |
| 36 |
); |
| 37 |
documentsCounterEl.textContent = documentsCountersData[counterName]; |
| 38 |
|
| 39 |
if ( |
| 40 |
documentsCountersData[counterName] !== 0 || |
| 41 |
countersAlwaysDisplayed.includes(counterName) |
| 42 |
) { |
| 43 |
documentsCounterEl.closest(".o_portal_index_card").classList.remove("d-none"); |
| 44 |
} |
| 45 |
}); |
| 46 |
return documentsCountersData; |
| 47 |
}); |
| 48 |
return Promise.all(proms).then((results) => { |
| 49 |
this.el.querySelector(".o_portal_doc_spinner").remove(); |
| 50 |
}); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
registry.category("public.interactions").add("portal.portal_home_counters", PortalHomeCounters); |