| 1 |
import { _t } from "@web/core/l10n/translation"; |
| 2 |
import { ConfirmationDialog, AlertDialog } from "@web/core/confirmation_dialog/confirmation_dialog"; |
| 3 |
import { ErrorDialog } from "@web/core/errors/error_dialogs"; |
| 4 |
import { |
| 5 |
useEnv, |
| 6 |
onMounted, |
| 7 |
onPatched, |
| 8 |
useComponent, |
| 9 |
useRef, |
| 10 |
useState, |
| 11 |
useExternalListener, |
| 12 |
} from "@odoo/owl"; |
| 13 |
import { KeepLast } from "@web/core/utils/concurrency"; |
| 14 |
|
| 15 |
/** |
| 16 |
* Introduce error handlers in the component. |
| 17 |
* |
| 18 |
* IMPROVEMENT: This is a terrible hook. There could be a better way to handle |
| 19 |
* the error when the order failed to sync. |
| 20 |
* FIXME POSREF: move this to the error_handler registry. |
| 21 |
*/ |
| 22 |
export function useErrorHandlers() { |
| 23 |
const component = useComponent(); |
| 24 |
const dialog = useEnv().services.dialog; |
| 25 |
|
| 26 |
component._handlePushOrderError = async function (error) { |
| 27 |
// This error handler receives `error` equivalent to `error.message` of the rpc error. |
| 28 |
if (error.message === "Backend Invoice") { |
| 29 |
dialog.add(ConfirmationDialog, { |
| 30 |
title: _t("Please print the invoice from the backend"), |
| 31 |
body: |
| 32 |
_t( |
| 33 |
"The order has been synchronized earlier. Please make the invoice from the backend for the order: " |
| 34 |
) + error.data.order.name, |
| 35 |
}); |
| 36 |
} else if (error.code < 0) { |
| 37 |
// XmlHttpRequest Errors |
| 38 |
dialog.add(ConfirmationDialog, { |
| 39 |
title: _t("Unable to sync order"), |
| 40 |
body: _t( |
| 41 |
"Check the internet connection then try to sync again by clicking on the red wifi button (upper right of the screen)." |
| 42 |
), |
| 43 |
}); |
| 44 |
} else if (error.data) { |
| 45 |
// OpenERP Server Errors |
| 46 |
dialog.add(ErrorDialog, { |
| 47 |
traceback: |
| 48 |
error.data.debug.status.message_body || |
| 49 |
_t("The server encountered an error while receiving your order."), |
| 50 |
}); |
| 51 |
} else { |
| 52 |
// ??? |
| 53 |
await dialog.add(AlertDialog, { |
| 54 |
title: _t("Unknown Error"), |
| 55 |
body: _t("The order could not be sent to the server due to an unknown error"), |
| 56 |
showReloadButton: true, |
| 57 |
}); |
| 58 |
} |
| 59 |
}; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Assumes t-ref="root" in the root element of the component that uses this hook. |
| 64 |
*/ |
| 65 |
export function useAutoFocusToLast() { |
| 66 |
const root = useRef("root"); |
| 67 |
let target = null; |
| 68 |
function autofocus() { |
| 69 |
const prevTarget = target; |
| 70 |
const allInputs = root.el.querySelectorAll("input"); |
| 71 |
target = allInputs[allInputs.length - 1]; |
| 72 |
if (target && target !== prevTarget) { |
| 73 |
target.focus(); |
| 74 |
target.selectionStart = target.selectionEnd = target.value.length; |
| 75 |
} |
| 76 |
} |
| 77 |
onMounted(autofocus); |
| 78 |
onPatched(autofocus); |
| 79 |
} |
| 80 |
|
| 81 |
export function useAsyncLockedMethod(method) { |
| 82 |
const component = useComponent(); |
| 83 |
let called = false; |
| 84 |
return async (...args) => { |
| 85 |
if (called) { |
| 86 |
return; |
| 87 |
} |
| 88 |
try { |
| 89 |
called = true; |
| 90 |
return await method.call(component, ...args); |
| 91 |
} finally { |
| 92 |
called = false; |
| 93 |
} |
| 94 |
}; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Wrapper for an async function that exposes the status of the function call. |
| 99 |
* |
| 100 |
* Sample use case: |
| 101 |
* ```js |
| 102 |
* { |
| 103 |
* // inside in a component |
| 104 |
* this.doPrint = useTrackedAsync(() => this.printReceipt()) |
| 105 |
* this.doPrint.status === 'idle' |
| 106 |
* this.doPrint.call() // triggers the given async function |
| 107 |
* this.doPrint.status === 'loading' |
| 108 |
* ['success', 'error].includes(this.doPrint.status) && this.doPrint.result |
| 109 |
* } |
| 110 |
* ``` |
| 111 |
* @param {(...args: any[]) => Promise<any>} asyncFn |
| 112 |
* @param {{ keepLast?: boolean }} [options] - Options for managing concurrency. |
| 113 |
*/ |
| 114 |
export function useTrackedAsync(asyncFn, options = {}) { |
| 115 |
/** |
| 116 |
* @type {{ |
| 117 |
* status: 'idle' | 'loading' | 'error' | 'success', |
| 118 |
* result: any, |
| 119 |
* lastArgs: any[] |
| 120 |
* }} |
| 121 |
*/ |
| 122 |
const state = useState({ |
| 123 |
status: "idle", |
| 124 |
result: null, |
| 125 |
lastArgs: null, |
| 126 |
}); |
| 127 |
|
| 128 |
const { keepLast = false } = options; |
| 129 |
|
| 130 |
const baseMethod = async (...args) => { |
| 131 |
state.status = "loading"; |
| 132 |
state.result = null; |
| 133 |
state.lastArgs = args; |
| 134 |
try { |
| 135 |
const result = await asyncFn(...args); |
| 136 |
state.status = "success"; |
| 137 |
state.result = result; |
| 138 |
} catch (error) { |
| 139 |
state.status = "error"; |
| 140 |
state.result = error; |
| 141 |
} |
| 142 |
}; |
| 143 |
|
| 144 |
let call; |
| 145 |
if (keepLast) { |
| 146 |
const keepLastInstance = new KeepLast(); |
| 147 |
call = (...args) => keepLastInstance.add(baseMethod(...args)); |
| 148 |
} else { |
| 149 |
call = useAsyncLockedMethod(baseMethod); |
| 150 |
} |
| 151 |
|
| 152 |
return { |
| 153 |
get status() { |
| 154 |
return state.status; |
| 155 |
}, |
| 156 |
get result() { |
| 157 |
return state.result; |
| 158 |
}, |
| 159 |
get lastArgs() { |
| 160 |
return state.lastArgs; |
| 161 |
}, |
| 162 |
call, |
| 163 |
}; |
| 164 |
} |
| 165 |
|
| 166 |
export function useIsChildLarger(container) { |
| 167 |
const state = useState({ |
| 168 |
isLarger: false, |
| 169 |
maxItems: 0, |
| 170 |
}); |
| 171 |
|
| 172 |
const computeSize = () => { |
| 173 |
if (!container.el || !container.el.children.length) { |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
let acc = 0; |
| 178 |
let nbrItems = 0; |
| 179 |
let isLarger = false; |
| 180 |
const oldLargerState = state.isLarger; |
| 181 |
const containerWidth = container.el.clientWidth - 10; |
| 182 |
|
| 183 |
for (const child of container.el.children) { |
| 184 |
acc += child.clientWidth; |
| 185 |
if (acc < containerWidth) { |
| 186 |
nbrItems++; |
| 187 |
} else { |
| 188 |
isLarger = true; |
| 189 |
break; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
state.isLarger = isLarger; |
| 194 |
state.maxItems = nbrItems; |
| 195 |
if (!oldLargerState && state.isLarger) { |
| 196 |
state.maxItems--; |
| 197 |
} |
| 198 |
}; |
| 199 |
|
| 200 |
useExternalListener(window, "resize", () => { |
| 201 |
computeSize(); |
| 202 |
}); |
| 203 |
|
| 204 |
return { |
| 205 |
get isLarger() { |
| 206 |
return state.isLarger; |
| 207 |
}, |
| 208 |
get maxItems() { |
| 209 |
return state.maxItems; |
| 210 |
}, |
| 211 |
reload: () => { |
| 212 |
computeSize(); |
| 213 |
}, |
| 214 |
}; |
| 215 |
} |