saleor-dashboard/src/components/Navigator/modes/orders.ts

46 lines
1.2 KiB
TypeScript
Raw Normal View History

2019-11-21 12:13:41 +00:00
import { IntlShape } from "react-intl";
2019-11-22 15:39:20 +00:00
import { UseNavigatorResult } from "@saleor/hooks/useNavigator";
2019-11-25 13:51:46 +00:00
import { maybe, transformOrderStatus } from "@saleor/misc";
2019-11-21 12:13:41 +00:00
import { orderUrl } from "@saleor/orders/urls";
2019-11-21 17:47:06 +00:00
import { CheckIfOrderExists_order } from "../queries/types/CheckIfOrderExists";
2019-11-21 12:13:41 +00:00
import { QuickSearchAction } from "../types";
import messages from "./messages";
2019-11-21 17:47:06 +00:00
export function isQueryValidOrderNumber(query: string): boolean {
return query === parseInt(query, 0).toString();
}
export function getGqlOrderId(orderNumber: string): string {
return btoa(`Order:${orderNumber}`);
}
2019-11-21 12:13:41 +00:00
function getOrdersModeActions(
query: string,
2019-11-21 17:47:06 +00:00
intl: IntlShape,
2019-11-22 15:39:20 +00:00
navigate: UseNavigatorResult,
2019-11-21 17:47:06 +00:00
order: CheckIfOrderExists_order
2019-11-21 12:13:41 +00:00
): QuickSearchAction[] {
2019-11-21 17:47:06 +00:00
const gqlId = getGqlOrderId(query);
if (isQueryValidOrderNumber(query) && maybe(() => order.id === gqlId)) {
2019-11-21 12:13:41 +00:00
return [
{
2019-11-25 13:51:46 +00:00
extraInfo: transformOrderStatus(order.status, intl).localized,
2019-11-21 12:13:41 +00:00
label: intl.formatMessage(messages.goToOrder, {
orderNumber: query
}),
2019-11-26 14:14:21 +00:00
onClick: () => {
navigate(orderUrl(gqlId));
return false;
},
2019-11-22 15:39:20 +00:00
type: "action"
2019-11-21 12:13:41 +00:00
}
];
}
return [];
}
export default getOrdersModeActions;