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

43 lines
1.1 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-21 17:47:06 +00:00
import { maybe } 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 [
{
label: intl.formatMessage(messages.goToOrder, {
orderNumber: query
}),
2019-11-22 15:39:20 +00:00
onClick: () => navigate(orderUrl(gqlId)),
2019-11-21 12:13:41 +00:00
score: 1,
2019-11-22 15:39:20 +00:00
type: "action"
2019-11-21 12:13:41 +00:00
}
];
}
return [];
}
export default getOrdersModeActions;