2022-01-28 12:34:20 +00:00
|
|
|
import { ConfirmButtonTransitionState, ThemeType } from "@saleor/macaw-ui";
|
2022-01-24 13:58:27 +00:00
|
|
|
import uniqBy from "lodash/uniqBy";
|
2019-08-09 11:14:35 +00:00
|
|
|
import moment from "moment-timezone";
|
2022-02-01 09:58:06 +00:00
|
|
|
import {
|
|
|
|
MutationFetchResult,
|
|
|
|
MutationFunction,
|
|
|
|
MutationResult
|
|
|
|
} from "react-apollo";
|
2021-11-24 10:13:53 +00:00
|
|
|
import { IntlShape } from "react-intl";
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2022-01-24 13:58:27 +00:00
|
|
|
import { MultiAutocompleteChoiceType } from "./components/MultiAutocompleteSelectField";
|
2019-10-25 12:18:52 +00:00
|
|
|
import { AddressType, AddressTypeInput } from "./customers/types";
|
2021-11-24 10:13:53 +00:00
|
|
|
import {
|
|
|
|
commonStatusMessages,
|
|
|
|
orderStatusMessages,
|
|
|
|
paymentStatusMessages
|
|
|
|
} from "./intl";
|
2021-12-23 15:00:37 +00:00
|
|
|
import { OrderDetails_order_shippingAddress } from "./orders/types/OrderDetails";
|
2019-12-06 17:14:19 +00:00
|
|
|
import {
|
2020-05-14 09:30:32 +00:00
|
|
|
MutationResultAdditionalProps,
|
2019-12-06 17:14:19 +00:00
|
|
|
PartialMutationProviderOutput,
|
2022-02-17 11:08:17 +00:00
|
|
|
StatusType,
|
2020-05-14 09:30:32 +00:00
|
|
|
UserError
|
2019-12-06 17:14:19 +00:00
|
|
|
} from "./types";
|
2019-06-19 14:40:52 +00:00
|
|
|
import {
|
2019-10-25 12:18:52 +00:00
|
|
|
AddressInput,
|
|
|
|
CountryCode,
|
2021-04-26 07:49:55 +00:00
|
|
|
DateRangeInput,
|
2019-06-19 14:40:52 +00:00
|
|
|
OrderStatus,
|
2019-10-17 11:47:11 +00:00
|
|
|
PaymentChargeStatusEnum
|
2019-06-19 14:40:52 +00:00
|
|
|
} from "./types/globalTypes";
|
|
|
|
|
|
|
|
export type RequireAtLeastOne<T, Keys extends keyof T = keyof T> = Pick<
|
|
|
|
T,
|
|
|
|
Exclude<keyof T, Keys>
|
|
|
|
> &
|
|
|
|
{ [K in Keys]-?: Required<Pick<T, K>> }[Keys];
|
|
|
|
|
|
|
|
export type RequireOnlyOne<T, Keys extends keyof T = keyof T> = Pick<
|
|
|
|
T,
|
|
|
|
Exclude<keyof T, Keys>
|
|
|
|
> &
|
|
|
|
{
|
|
|
|
[K in Keys]-?: Required<Pick<T, K>> &
|
2019-08-29 10:55:56 +00:00
|
|
|
Partial<Record<Exclude<Keys, K>, undefined>>;
|
2019-06-19 14:40:52 +00:00
|
|
|
}[Keys];
|
|
|
|
|
|
|
|
export function renderCollection<T>(
|
|
|
|
collection: T[],
|
|
|
|
renderItem: (
|
|
|
|
item: T | undefined,
|
|
|
|
index: number | undefined,
|
|
|
|
collection: T[]
|
|
|
|
) => any,
|
|
|
|
renderEmpty?: (collection: T[]) => any
|
|
|
|
) {
|
|
|
|
if (collection === undefined) {
|
|
|
|
return renderItem(undefined, undefined, collection);
|
|
|
|
}
|
|
|
|
if (collection.length === 0) {
|
|
|
|
return !!renderEmpty ? renderEmpty(collection) : null;
|
|
|
|
}
|
|
|
|
return collection.map(renderItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function decimal(value: string | number) {
|
|
|
|
if (typeof value === "string") {
|
|
|
|
return value === "" ? null : value;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-07-13 16:51:05 +00:00
|
|
|
export function weight(value: string) {
|
|
|
|
return value === "" ? null : parseFloat(value);
|
|
|
|
}
|
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
export const removeDoubleSlashes = (url: string) =>
|
|
|
|
url.replace(/([^:]\/)\/+/g, "$1");
|
|
|
|
|
2021-06-28 13:51:19 +00:00
|
|
|
export const transformPaymentStatus = (
|
|
|
|
status: string,
|
|
|
|
intl: IntlShape
|
2022-02-17 11:08:17 +00:00
|
|
|
): { localized: string; status: StatusType } => {
|
2019-06-19 14:40:52 +00:00
|
|
|
switch (status) {
|
|
|
|
case PaymentChargeStatusEnum.PARTIALLY_CHARGED:
|
2019-08-29 10:55:56 +00:00
|
|
|
return {
|
|
|
|
localized: intl.formatMessage(paymentStatusMessages.partiallyPaid),
|
2021-11-24 10:13:53 +00:00
|
|
|
status: StatusType.ERROR
|
2019-08-29 10:55:56 +00:00
|
|
|
};
|
2019-06-19 14:40:52 +00:00
|
|
|
case PaymentChargeStatusEnum.FULLY_CHARGED:
|
2019-08-29 10:55:56 +00:00
|
|
|
return {
|
|
|
|
localized: intl.formatMessage(paymentStatusMessages.paid),
|
2021-11-24 10:13:53 +00:00
|
|
|
status: StatusType.SUCCESS
|
2019-08-29 10:55:56 +00:00
|
|
|
};
|
2019-06-19 14:40:52 +00:00
|
|
|
case PaymentChargeStatusEnum.PARTIALLY_REFUNDED:
|
2019-08-29 10:55:56 +00:00
|
|
|
return {
|
|
|
|
localized: intl.formatMessage(paymentStatusMessages.partiallyRefunded),
|
2022-02-17 11:08:17 +00:00
|
|
|
status: StatusType.INFO
|
2019-08-29 10:55:56 +00:00
|
|
|
};
|
2019-06-19 14:40:52 +00:00
|
|
|
case PaymentChargeStatusEnum.FULLY_REFUNDED:
|
2019-08-29 10:55:56 +00:00
|
|
|
return {
|
|
|
|
localized: intl.formatMessage(paymentStatusMessages.refunded),
|
2022-02-17 11:08:17 +00:00
|
|
|
status: StatusType.INFO
|
2021-11-24 10:13:53 +00:00
|
|
|
};
|
|
|
|
case PaymentChargeStatusEnum.PENDING:
|
|
|
|
return {
|
|
|
|
localized: intl.formatMessage(paymentStatusMessages.pending),
|
2022-02-17 11:08:17 +00:00
|
|
|
status: StatusType.WARNING
|
2019-08-29 10:55:56 +00:00
|
|
|
};
|
2021-11-24 10:13:53 +00:00
|
|
|
case PaymentChargeStatusEnum.REFUSED:
|
|
|
|
return {
|
|
|
|
localized: intl.formatMessage(paymentStatusMessages.refused),
|
|
|
|
status: StatusType.ERROR
|
|
|
|
};
|
|
|
|
case PaymentChargeStatusEnum.CANCELLED:
|
|
|
|
return {
|
|
|
|
localized: intl.formatMessage(commonStatusMessages.cancelled),
|
|
|
|
status: StatusType.ERROR
|
|
|
|
};
|
|
|
|
case PaymentChargeStatusEnum.NOT_CHARGED:
|
2019-08-29 10:55:56 +00:00
|
|
|
return {
|
|
|
|
localized: intl.formatMessage(paymentStatusMessages.unpaid),
|
2021-11-24 10:13:53 +00:00
|
|
|
status: StatusType.ERROR
|
2019-08-29 10:55:56 +00:00
|
|
|
};
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
2021-11-24 10:13:53 +00:00
|
|
|
return {
|
|
|
|
localized: status,
|
|
|
|
status: StatusType.ERROR
|
|
|
|
};
|
2019-06-19 14:40:52 +00:00
|
|
|
};
|
|
|
|
|
2020-11-30 13:19:57 +00:00
|
|
|
export const transformOrderStatus = (
|
|
|
|
status: string,
|
|
|
|
intl: IntlShape
|
|
|
|
): { localized: string; status: StatusType } => {
|
2019-06-19 14:40:52 +00:00
|
|
|
switch (status) {
|
|
|
|
case OrderStatus.FULFILLED:
|
2019-08-29 10:55:56 +00:00
|
|
|
return {
|
|
|
|
localized: intl.formatMessage(orderStatusMessages.fulfilled),
|
2020-11-30 13:19:57 +00:00
|
|
|
status: StatusType.SUCCESS
|
2019-08-29 10:55:56 +00:00
|
|
|
};
|
2019-06-19 14:40:52 +00:00
|
|
|
case OrderStatus.PARTIALLY_FULFILLED:
|
2019-08-29 10:55:56 +00:00
|
|
|
return {
|
|
|
|
localized: intl.formatMessage(orderStatusMessages.partiallyFulfilled),
|
2022-02-17 11:08:17 +00:00
|
|
|
status: StatusType.WARNING
|
2019-08-29 10:55:56 +00:00
|
|
|
};
|
2019-06-19 14:40:52 +00:00
|
|
|
case OrderStatus.UNFULFILLED:
|
2019-08-29 10:55:56 +00:00
|
|
|
return {
|
|
|
|
localized: intl.formatMessage(orderStatusMessages.unfulfilled),
|
2020-11-30 13:19:57 +00:00
|
|
|
status: StatusType.ERROR
|
2019-08-29 10:55:56 +00:00
|
|
|
};
|
2019-06-19 14:40:52 +00:00
|
|
|
case OrderStatus.CANCELED:
|
2019-08-29 10:55:56 +00:00
|
|
|
return {
|
2021-11-24 10:13:53 +00:00
|
|
|
localized: intl.formatMessage(commonStatusMessages.cancelled),
|
2020-11-30 13:19:57 +00:00
|
|
|
status: StatusType.ERROR
|
2019-08-29 10:55:56 +00:00
|
|
|
};
|
2019-06-19 14:40:52 +00:00
|
|
|
case OrderStatus.DRAFT:
|
2019-08-29 10:55:56 +00:00
|
|
|
return {
|
|
|
|
localized: intl.formatMessage(orderStatusMessages.draft),
|
2022-02-17 11:08:17 +00:00
|
|
|
status: StatusType.INFO
|
2020-11-30 13:19:57 +00:00
|
|
|
};
|
|
|
|
case OrderStatus.UNCONFIRMED:
|
|
|
|
return {
|
|
|
|
localized: intl.formatMessage(orderStatusMessages.unconfirmed),
|
2022-02-17 11:08:17 +00:00
|
|
|
status: StatusType.INFO
|
2019-08-29 10:55:56 +00:00
|
|
|
};
|
2021-01-20 16:16:43 +00:00
|
|
|
case OrderStatus.PARTIALLY_RETURNED:
|
|
|
|
return {
|
|
|
|
localized: intl.formatMessage(orderStatusMessages.partiallyReturned),
|
2022-02-17 11:08:17 +00:00
|
|
|
status: StatusType.INFO
|
2021-01-20 16:16:43 +00:00
|
|
|
};
|
|
|
|
case OrderStatus.RETURNED:
|
|
|
|
return {
|
|
|
|
localized: intl.formatMessage(orderStatusMessages.returned),
|
2022-02-17 11:08:17 +00:00
|
|
|
status: StatusType.INFO
|
2021-01-20 16:16:43 +00:00
|
|
|
};
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
localized: status,
|
2020-11-30 13:19:57 +00:00
|
|
|
status: StatusType.ERROR
|
2019-06-19 14:40:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-05-06 11:38:15 +00:00
|
|
|
export const transformAddressToForm = (data?: AddressType) => ({
|
2021-01-07 10:17:40 +00:00
|
|
|
city: data?.city || "",
|
|
|
|
cityArea: data?.cityArea || "",
|
|
|
|
companyName: data?.companyName || "",
|
|
|
|
country: data?.country?.code || "",
|
|
|
|
countryArea: data?.countryArea || "",
|
|
|
|
firstName: data?.firstName || "",
|
|
|
|
lastName: data?.lastName || "",
|
|
|
|
phone: data?.phone || "",
|
|
|
|
postalCode: data?.postalCode || "",
|
|
|
|
streetAddress1: data?.streetAddress1 || "",
|
|
|
|
streetAddress2: data?.streetAddress2 || ""
|
2019-06-19 14:40:52 +00:00
|
|
|
});
|
|
|
|
|
2019-08-09 11:14:35 +00:00
|
|
|
export function maybe<T>(exp: () => T): T | undefined;
|
|
|
|
export function maybe<T>(exp: () => T, d: T): T;
|
|
|
|
export function maybe(exp: any, d?: any) {
|
2019-06-19 14:40:52 +00:00
|
|
|
try {
|
|
|
|
const result = exp();
|
|
|
|
return result === undefined ? d : result;
|
|
|
|
} catch {
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function only<T>(obj: T, key: keyof T): boolean {
|
|
|
|
return Object.keys(obj).every(objKey =>
|
|
|
|
objKey === key ? obj[key] !== undefined : obj[key] === undefined
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-09 08:44:09 +00:00
|
|
|
export function empty(obj: {}): boolean {
|
2019-06-19 14:40:52 +00:00
|
|
|
return Object.keys(obj).every(key => obj[key] === undefined);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hasErrors(errorList: UserError[] | null): boolean {
|
|
|
|
return !(
|
|
|
|
errorList === undefined ||
|
|
|
|
errorList === null ||
|
|
|
|
errorList.length === 0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getMutationState(
|
|
|
|
called: boolean,
|
|
|
|
loading: boolean,
|
2020-09-01 16:22:54 +00:00
|
|
|
...errorList: any[][]
|
2019-06-19 14:40:52 +00:00
|
|
|
): ConfirmButtonTransitionState {
|
|
|
|
if (loading) {
|
|
|
|
return "loading";
|
|
|
|
}
|
|
|
|
if (called) {
|
|
|
|
return errorList.map(hasErrors).reduce((acc, curr) => acc || curr, false)
|
|
|
|
? "error"
|
|
|
|
: "success";
|
|
|
|
}
|
|
|
|
return "default";
|
|
|
|
}
|
|
|
|
|
2022-02-01 09:58:06 +00:00
|
|
|
export interface SaleorMutationResult {
|
|
|
|
errors?: any[];
|
2020-12-01 15:42:25 +00:00
|
|
|
}
|
2022-02-01 09:58:06 +00:00
|
|
|
|
|
|
|
type InferPromiseResult<T> = T extends Promise<infer V> ? V : never;
|
|
|
|
|
|
|
|
export const extractMutationErrors = async <
|
|
|
|
TData extends InferPromiseResult<TPromise>,
|
|
|
|
TPromise extends Promise<MutationFetchResult<TData>>,
|
|
|
|
TErrors extends ReturnType<typeof getMutationErrors>
|
|
|
|
>(
|
|
|
|
submitPromise: TPromise
|
|
|
|
): Promise<TErrors> => {
|
|
|
|
const result = await submitPromise;
|
|
|
|
|
|
|
|
const e = getMutationErrors(result);
|
|
|
|
|
|
|
|
return e as TErrors;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getMutationErrors = <
|
|
|
|
T extends MutationFetchResult<any>,
|
|
|
|
TData extends T["data"],
|
|
|
|
TErrors extends TData[keyof TData]["errors"]
|
|
|
|
>(
|
|
|
|
result: T
|
|
|
|
): TErrors[] => {
|
|
|
|
if (!result?.data) {
|
|
|
|
return [] as TErrors;
|
|
|
|
}
|
|
|
|
return Object.values(result.data).reduce(
|
|
|
|
(acc: TErrors[], mut: TData) => [...acc, ...(mut.errors || [])],
|
|
|
|
[] as TErrors[]
|
|
|
|
) as TErrors;
|
|
|
|
};
|
|
|
|
|
2019-12-06 17:11:28 +00:00
|
|
|
export function getMutationStatus<
|
|
|
|
TData extends Record<string, SaleorMutationResult | any>
|
|
|
|
>(opts: MutationResult<TData>): ConfirmButtonTransitionState {
|
2022-02-01 09:58:06 +00:00
|
|
|
const errors = getMutationErrors(opts);
|
2019-12-06 17:11:28 +00:00
|
|
|
|
|
|
|
return getMutationState(opts.called, opts.loading, errors);
|
|
|
|
}
|
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
export function getMutationProviderData<TData, TVariables>(
|
2019-08-12 11:47:38 +00:00
|
|
|
mutateFn: MutationFunction<TData, TVariables>,
|
2019-12-06 17:14:19 +00:00
|
|
|
opts: MutationResult<TData> & MutationResultAdditionalProps
|
2019-06-19 14:40:52 +00:00
|
|
|
): PartialMutationProviderOutput<TData, TVariables> {
|
|
|
|
return {
|
|
|
|
mutate: variables => mutateFn({ variables }),
|
|
|
|
opts
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
interface User {
|
|
|
|
email: string;
|
|
|
|
firstName?: string;
|
|
|
|
lastName?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getUserName(user?: User, returnEmail?: boolean) {
|
|
|
|
return user && (user.email || (user.firstName && user.lastName))
|
|
|
|
? user.firstName && user.lastName
|
|
|
|
? [user.firstName, user.lastName].join(" ")
|
|
|
|
: returnEmail
|
|
|
|
? user.email
|
|
|
|
: user.email.split("@")[0]
|
|
|
|
: undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getUserInitials(user?: User) {
|
|
|
|
return user && (user.email || (user.firstName && user.lastName))
|
|
|
|
? (user.firstName && user.lastName
|
|
|
|
? user.firstName[0] + user.lastName[0]
|
|
|
|
: user.email.slice(0, 2)
|
|
|
|
).toUpperCase()
|
|
|
|
: undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface AnyEvent {
|
|
|
|
stopPropagation: () => void;
|
|
|
|
}
|
2020-08-17 14:44:40 +00:00
|
|
|
export function stopPropagation(cb: (event?: AnyEvent) => void) {
|
2019-06-19 14:40:52 +00:00
|
|
|
return (event: AnyEvent) => {
|
|
|
|
event.stopPropagation();
|
2020-08-17 14:44:40 +00:00
|
|
|
cb(event);
|
2019-06-19 14:40:52 +00:00
|
|
|
};
|
|
|
|
}
|
2019-08-09 11:14:35 +00:00
|
|
|
|
2021-07-29 12:15:14 +00:00
|
|
|
export interface DateTime {
|
|
|
|
date: string;
|
|
|
|
time: string;
|
|
|
|
}
|
|
|
|
|
2019-08-09 11:14:35 +00:00
|
|
|
export function joinDateTime(date: string, time?: string) {
|
|
|
|
if (!date) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const setTime = time || "00:00";
|
|
|
|
const dateTime = moment(date + " " + setTime).format();
|
|
|
|
return dateTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function splitDateTime(dateTime: string) {
|
|
|
|
if (!dateTime) {
|
|
|
|
return {
|
|
|
|
date: "",
|
|
|
|
time: ""
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// Default html input format YYYY-MM-DD HH:mm
|
|
|
|
const splitDateTime = moment(dateTime)
|
|
|
|
.format("YYYY-MM-DD HH:mm")
|
|
|
|
.split(" ");
|
|
|
|
return {
|
|
|
|
date: splitDateTime[0],
|
|
|
|
time: splitDateTime[1]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function generateCode(charNum: number) {
|
|
|
|
let result = "";
|
|
|
|
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
|
|
for (let i = 0; i < charNum; i++) {
|
|
|
|
result += characters.charAt(Math.floor(Math.random() * characters.length));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2019-09-05 13:05:12 +00:00
|
|
|
|
2022-01-25 12:44:19 +00:00
|
|
|
export function isInEnum<TEnum extends {}>(needle: string, haystack: TEnum) {
|
|
|
|
return Object.keys(haystack).includes(needle);
|
|
|
|
}
|
|
|
|
|
2021-03-09 08:44:09 +00:00
|
|
|
export function findInEnum<TEnum extends {}>(needle: string, haystack: TEnum) {
|
2019-09-05 13:05:12 +00:00
|
|
|
const match = Object.keys(haystack).find(key => key === needle);
|
|
|
|
if (!!match) {
|
|
|
|
return haystack[needle as keyof TEnum];
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(`Key ${needle} not found in enum`);
|
|
|
|
}
|
2019-09-13 11:33:42 +00:00
|
|
|
|
2021-12-23 15:00:37 +00:00
|
|
|
export function addressToAddressInput<T>(
|
|
|
|
address: T & OrderDetails_order_shippingAddress
|
|
|
|
): AddressInput {
|
|
|
|
const { id, __typename, ...rest } = address;
|
|
|
|
return {
|
|
|
|
...rest,
|
|
|
|
country: findInEnum(address.country.code, CountryCode)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-03-09 08:44:09 +00:00
|
|
|
export function findValueInEnum<TEnum extends {}>(
|
2019-12-17 17:13:56 +00:00
|
|
|
needle: string,
|
|
|
|
haystack: TEnum
|
2019-12-20 15:53:03 +00:00
|
|
|
): TEnum[keyof TEnum] {
|
2019-12-17 17:13:56 +00:00
|
|
|
const match = Object.entries(haystack).find(([_, value]) => value === needle);
|
|
|
|
|
2019-12-20 15:53:03 +00:00
|
|
|
if (!match) {
|
|
|
|
throw new Error(`Value ${needle} not found in enum`);
|
2019-12-17 17:13:56 +00:00
|
|
|
}
|
|
|
|
|
2019-12-20 15:53:03 +00:00
|
|
|
return (needle as unknown) as TEnum[keyof TEnum];
|
2019-12-17 17:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function parseBoolean(a: string, defaultValue: boolean): boolean {
|
2019-09-26 10:14:07 +00:00
|
|
|
if (a === undefined) {
|
2019-12-17 17:13:56 +00:00
|
|
|
return defaultValue;
|
2019-09-26 10:14:07 +00:00
|
|
|
}
|
2019-09-13 11:33:42 +00:00
|
|
|
return a === "true";
|
|
|
|
}
|
2019-10-18 12:29:01 +00:00
|
|
|
|
|
|
|
export function capitalize(s: string) {
|
|
|
|
return s.charAt(0).toLocaleUpperCase() + s.slice(1);
|
|
|
|
}
|
2019-10-25 12:18:52 +00:00
|
|
|
|
2021-05-06 11:38:15 +00:00
|
|
|
export function transformFormToAddressInput<T>(
|
2019-11-13 13:50:38 +00:00
|
|
|
address: T & AddressTypeInput
|
|
|
|
): T & AddressInput {
|
2019-10-25 12:18:52 +00:00
|
|
|
return {
|
|
|
|
...address,
|
|
|
|
country: findInEnum(address.country, CountryCode)
|
|
|
|
};
|
|
|
|
}
|
2020-02-20 14:18:22 +00:00
|
|
|
|
2021-08-16 13:44:00 +00:00
|
|
|
export function getStringOrPlaceholder(
|
|
|
|
s: string | undefined,
|
|
|
|
placeholder?: string
|
|
|
|
): string {
|
|
|
|
return s || placeholder || "...";
|
2020-02-20 14:18:22 +00:00
|
|
|
}
|
2021-04-26 07:49:55 +00:00
|
|
|
|
|
|
|
export const getDatePeriod = (days: number): DateRangeInput => {
|
|
|
|
if (days < 1) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const end = moment().startOf("day");
|
|
|
|
const start = end.subtract(days - 1);
|
|
|
|
const format = "YYYY-MM-DD";
|
|
|
|
|
|
|
|
return {
|
|
|
|
gte: start.format(format),
|
|
|
|
lte: end.format(format)
|
|
|
|
};
|
|
|
|
};
|
2021-05-06 11:38:15 +00:00
|
|
|
|
2021-08-16 13:44:00 +00:00
|
|
|
export const isDarkTheme = (themeType: ThemeType) => themeType === "dark";
|
|
|
|
|
2021-05-06 11:38:15 +00:00
|
|
|
export const transformAddressToAddressInput = (data?: AddressType) => ({
|
|
|
|
city: data?.city || "",
|
|
|
|
cityArea: data?.cityArea || "",
|
|
|
|
companyName: data?.companyName || "",
|
|
|
|
country: findInEnum(data?.country?.code || "", CountryCode),
|
|
|
|
countryArea: data?.countryArea || "",
|
|
|
|
firstName: data?.firstName || "",
|
|
|
|
lastName: data?.lastName || "",
|
|
|
|
phone: data?.phone || "",
|
|
|
|
postalCode: data?.postalCode || "",
|
|
|
|
streetAddress1: data?.streetAddress1 || "",
|
|
|
|
streetAddress2: data?.streetAddress2 || ""
|
|
|
|
});
|
2021-08-16 13:44:00 +00:00
|
|
|
|
|
|
|
export function getFullName<T extends { firstName: string; lastName: string }>(
|
|
|
|
data: T
|
|
|
|
) {
|
|
|
|
if (!data || !data.firstName || !data.lastName) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${data.firstName} ${data.lastName}`;
|
|
|
|
}
|
2021-12-03 11:59:29 +00:00
|
|
|
export const flatten = (obj: unknown) => {
|
|
|
|
// Be cautious that repeated keys are overwritten
|
|
|
|
|
|
|
|
const result = {};
|
|
|
|
|
|
|
|
Object.keys(obj).forEach(key => {
|
|
|
|
if (typeof obj[key] === "object" && obj[key] !== null) {
|
|
|
|
Object.assign(result, flatten(obj[key]));
|
|
|
|
} else {
|
|
|
|
result[key] = obj[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
2022-01-10 12:34:17 +00:00
|
|
|
|
|
|
|
export function PromiseQueue() {
|
|
|
|
let queue = Promise.resolve();
|
|
|
|
|
|
|
|
function add<T>(operation: (value: T | void) => PromiseLike<T>) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
queue = queue
|
|
|
|
.then(operation)
|
|
|
|
.then(resolve)
|
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return { queue, add };
|
|
|
|
}
|
2022-01-24 13:58:27 +00:00
|
|
|
|
|
|
|
export const combinedMultiAutocompleteChoices = (
|
|
|
|
selected: MultiAutocompleteChoiceType[],
|
|
|
|
choices: MultiAutocompleteChoiceType[]
|
|
|
|
) => uniqBy([...selected, ...choices], "value");
|