diff options
author | Sebastian <sebasjm@gmail.com> | 2022-12-19 12:23:39 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2022-12-19 12:23:39 -0300 |
commit | 72b429321553841ac1ff48cf974bfc65da01bb06 (patch) | |
tree | 7db9a4462f02de6cb86de695a1e64772b00ead5f /packages/merchant-backoffice-ui/src/utils | |
parent | 770ab6f01dc81a16f384f314982bd761540f8e65 (diff) | |
download | wallet-core-72b429321553841ac1ff48cf974bfc65da01bb06.tar.xz |
pretty
Diffstat (limited to 'packages/merchant-backoffice-ui/src/utils')
4 files changed, 98 insertions, 74 deletions
diff --git a/packages/merchant-backoffice-ui/src/utils/amount.ts b/packages/merchant-backoffice-ui/src/utils/amount.ts index 23ced34bd..bdc37952f 100644 --- a/packages/merchant-backoffice-ui/src/utils/amount.ts +++ b/packages/merchant-backoffice-ui/src/utils/amount.ts @@ -13,7 +13,11 @@ You should have received a copy of the GNU General Public License along with GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -import { amountFractionalBase, AmountJson, Amounts } from "@gnu-taler/taler-util"; +import { + amountFractionalBase, + AmountJson, + Amounts, +} from "@gnu-taler/taler-util"; import { MerchantBackend } from "../declaration.js"; /** @@ -23,10 +27,10 @@ import { MerchantBackend } from "../declaration.js"; * @returns */ const sumPrices = (one: string, two: string) => { - const [currency, valueOne] = one.split(':') - const [, valueTwo] = two.split(':') - return `${currency}:${parseInt(valueOne, 10) + parseInt(valueTwo, 10)}` -} + const [currency, valueOne] = one.split(":"); + const [, valueTwo] = two.split(":"); + return `${currency}:${parseInt(valueOne, 10) + parseInt(valueTwo, 10)}`; +}; /** * merge refund with the same description and a difference less than one minute @@ -34,37 +38,43 @@ const sumPrices = (one: string, two: string) => { * @param cur new refund to add to the list * @returns list with the new refund, may be merged with the last */ -export function mergeRefunds(prev: MerchantBackend.Orders.RefundDetails[], cur: MerchantBackend.Orders.RefundDetails) { +export function mergeRefunds( + prev: MerchantBackend.Orders.RefundDetails[], + cur: MerchantBackend.Orders.RefundDetails, +) { let tail; - if (prev.length === 0 || //empty list - cur.timestamp.t_s === 'never' || //current does not have timestamp - (tail = prev[prev.length - 1]).timestamp.t_s === 'never' || // last does not have timestamp + if ( + prev.length === 0 || //empty list + cur.timestamp.t_s === "never" || //current does not have timestamp + (tail = prev[prev.length - 1]).timestamp.t_s === "never" || // last does not have timestamp cur.reason !== tail.reason || //different reason cur.pending !== tail.pending || //different pending state - Math.abs(cur.timestamp.t_s - tail.timestamp.t_s) > 1000 * 60) {//more than 1 minute difference + Math.abs(cur.timestamp.t_s - tail.timestamp.t_s) > 1000 * 60 + ) { + //more than 1 minute difference - prev.push(cur) - return prev + prev.push(cur); + return prev; } prev[prev.length - 1] = { ...tail, - amount: sumPrices(tail.amount, cur.amount) - } + amount: sumPrices(tail.amount, cur.amount), + }; - return prev + return prev; } export const rate = (one: string, two: string) => { - const a = Amounts.parseOrThrow(one) - const b = Amounts.parseOrThrow(two) - const af = toFloat(a) - const bf = toFloat(b) - if (bf === 0) return 0 - return af / bf -} + const a = Amounts.parseOrThrow(one); + const b = Amounts.parseOrThrow(two); + const af = toFloat(a); + const bf = toFloat(b); + if (bf === 0) return 0; + return af / bf; +}; function toFloat(amount: AmountJson) { - return amount.value + (amount.fraction / amountFractionalBase); + return amount.value + amount.fraction / amountFractionalBase; } diff --git a/packages/merchant-backoffice-ui/src/utils/constants.ts b/packages/merchant-backoffice-ui/src/utils/constants.ts index 90f133c6a..fea9cb7e2 100644 --- a/packages/merchant-backoffice-ui/src/utils/constants.ts +++ b/packages/merchant-backoffice-ui/src/utils/constants.ts @@ -15,26 +15,30 @@ */ /** -* -* @author Sebastian Javier Marchano (sebasjm) -*/ + * + * @author Sebastian Javier Marchano (sebasjm) + */ //https://tools.ietf.org/html/rfc8905 -export const PAYTO_REGEX = /^payto:\/\/[a-zA-Z][a-zA-Z0-9-.]+(\/[a-zA-Z0-9\-\.\~\(\)@_%:!$&'*+,;=]*)*\??((amount|receiver-name|sender-name|instruction|message)=[a-zA-Z0-9\-\.\~\(\)@_%:!$'*+,;=]*&?)*$/ -export const PAYTO_WIRE_METHOD_LOOKUP = /payto:\/\/([a-zA-Z][a-zA-Z0-9-.]+)\/.*/ +export const PAYTO_REGEX = + /^payto:\/\/[a-zA-Z][a-zA-Z0-9-.]+(\/[a-zA-Z0-9\-\.\~\(\)@_%:!$&'*+,;=]*)*\??((amount|receiver-name|sender-name|instruction|message)=[a-zA-Z0-9\-\.\~\(\)@_%:!$'*+,;=]*&?)*$/; +export const PAYTO_WIRE_METHOD_LOOKUP = + /payto:\/\/([a-zA-Z][a-zA-Z0-9-.]+)\/.*/; -export const AMOUNT_REGEX = /^[a-zA-Z][a-zA-Z]*:[0-9][0-9,]*\.?[0-9,]*$/ +export const AMOUNT_REGEX = /^[a-zA-Z][a-zA-Z]*:[0-9][0-9,]*\.?[0-9,]*$/; -export const INSTANCE_ID_LOOKUP = /\/instances\/([^/]*)\/?$/ +export const INSTANCE_ID_LOOKUP = /\/instances\/([^/]*)\/?$/; -export const AMOUNT_ZERO_REGEX = /^[a-zA-Z][a-zA-Z]*:0$/ +export const AMOUNT_ZERO_REGEX = /^[a-zA-Z][a-zA-Z]*:0$/; -export const CROCKFORD_BASE32_REGEX = /^[0123456789ABCDEFGHJKMNPQRSTVWXYZ]+[*~$=U]*$/ +export const CROCKFORD_BASE32_REGEX = + /^[0123456789ABCDEFGHJKMNPQRSTVWXYZ]+[*~$=U]*$/; -export const URL_REGEX = /^((https?:)(\/\/\/?)([\w]*(?::[\w]*)?@)?([\d\w\.-]+)(?::(\d+))?)\/$/ +export const URL_REGEX = + /^((https?:)(\/\/\/?)([\w]*(?::[\w]*)?@)?([\d\w\.-]+)(?::(\d+))?)\/$/; // how much rows we add every time user hit load more -export const PAGE_SIZE = 20 +export const PAGE_SIZE = 20; // how bigger can be the result set // after this threshold, load more with move the cursor export const MAX_RESULT_SIZE = PAGE_SIZE * 2 - 1; @@ -44,7 +48,7 @@ export const DEFAULT_REQUEST_TIMEOUT = 10; export const MAX_IMAGE_SIZE = 1024 * 1024; -export const INSTANCE_ID_REGEX = /^[a-zA-Z0-9][a-zA-Z0-9_.@-]+$/ +export const INSTANCE_ID_REGEX = /^[a-zA-Z0-9][a-zA-Z0-9_.@-]+$/; export const COUNTRY_TABLE = { AE: "U.A.E.", @@ -189,6 +193,5 @@ export const COUNTRY_TABLE = { VN: "Viet Nam", YE: "Yemen", ZA: "South Africa", - ZW: "Zimbabwe" -} - + ZW: "Zimbabwe", +}; diff --git a/packages/merchant-backoffice-ui/src/utils/switchableAxios.ts b/packages/merchant-backoffice-ui/src/utils/switchableAxios.ts index 1010dd7fb..20ce7043e 100644 --- a/packages/merchant-backoffice-ui/src/utils/switchableAxios.ts +++ b/packages/merchant-backoffice-ui/src/utils/switchableAxios.ts @@ -23,44 +23,51 @@ import axios, { AxiosPromise, AxiosRequestConfig } from "axios"; export let removeAxiosCancelToken = false; -export let axiosHandler = function doAxiosRequest(config: AxiosRequestConfig): AxiosPromise<any> { - return axios(config) -} +export let axiosHandler = function doAxiosRequest( + config: AxiosRequestConfig, +): AxiosPromise<any> { + return axios(config); +}; /** * Set this backend library to testing mode. * Instead of calling the axios library the @handler will be called - * - * @param handler callback that will mock axios + * + * @param handler callback that will mock axios */ -export function setAxiosRequestAsTestingEnvironment(handler: AxiosHandler): void { +export function setAxiosRequestAsTestingEnvironment( + handler: AxiosHandler, +): void { removeAxiosCancelToken = true; axiosHandler = function defaultTestingHandler(config) { - const currentHanlder = listOfHandlersToUseOnce.shift() + const currentHanlder = listOfHandlersToUseOnce.shift(); if (!currentHanlder) { - return handler(config) + return handler(config); } - return currentHanlder(config) - } + return currentHanlder(config); + }; } type AxiosHandler = (config: AxiosRequestConfig) => AxiosPromise<any>; -type AxiosArguments = { args: AxiosRequestConfig | undefined } +type AxiosArguments = { args: AxiosRequestConfig | undefined }; - -const listOfHandlersToUseOnce = new Array<AxiosHandler>() +const listOfHandlersToUseOnce = new Array<AxiosHandler>(); /** - * + * * @param handler mock function * @returns savedArgs */ -export function mockAxiosOnce(handler: AxiosHandler): { args: AxiosRequestConfig | undefined } { - const savedArgs: AxiosArguments = { args: undefined } - listOfHandlersToUseOnce.push((config: AxiosRequestConfig): AxiosPromise<any> => { - savedArgs.args = config; - return handler(config) - }) +export function mockAxiosOnce(handler: AxiosHandler): { + args: AxiosRequestConfig | undefined; +} { + const savedArgs: AxiosArguments = { args: undefined }; + listOfHandlersToUseOnce.push( + (config: AxiosRequestConfig): AxiosPromise<any> => { + savedArgs.args = config; + return handler(config); + }, + ); return savedArgs; } diff --git a/packages/merchant-backoffice-ui/src/utils/table.ts b/packages/merchant-backoffice-ui/src/utils/table.ts index a79edfdcc..71358e25f 100644 --- a/packages/merchant-backoffice-ui/src/utils/table.ts +++ b/packages/merchant-backoffice-ui/src/utils/table.ts @@ -17,37 +17,41 @@ import { WithId } from "../declaration.js"; /** -* -* @author Sebastian Javier Marchano (sebasjm) -*/ + * + * @author Sebastian Javier Marchano (sebasjm) + */ export interface Actions<T extends WithId> { element: T; - type: 'DELETE' | 'UPDATE'; + type: "DELETE" | "UPDATE"; } function notEmpty<TValue>(value: TValue | null | undefined): value is TValue { return value !== null && value !== undefined; } -export function buildActions<T extends WithId>(instances: T[], selected: string[], action: 'DELETE'): Actions<T>[] { - return selected.map(id => instances.find(i => i.id === id)) +export function buildActions<T extends WithId>( + instances: T[], + selected: string[], + action: "DELETE", +): Actions<T>[] { + return selected + .map((id) => instances.find((i) => i.id === id)) .filter(notEmpty) - .map(id => ({ element: id, type: action })) + .map((id) => ({ element: id, type: action })); } /** * For any object or array, return the same object if is not empty. - * not empty: + * not empty: * - for arrays: at least one element not undefined * - for objects: at least one property not undefined - * @param obj - * @returns + * @param obj + * @returns */ -export function undefinedIfEmpty<T extends Record<string, unknown>|Array<unknown>>(obj: T): T | undefined { - if (obj === undefined) return undefined - return Object.values(obj).some((v) => v !== undefined) - ? obj - : undefined; +export function undefinedIfEmpty< + T extends Record<string, unknown> | Array<unknown>, +>(obj: T): T | undefined { + if (obj === undefined) return undefined; + return Object.values(obj).some((v) => v !== undefined) ? obj : undefined; } - |