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

41 lines
1,013 B
TypeScript
Raw Normal View History

2019-11-21 12:13:41 +00:00
import { IntlShape } from "react-intl";
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,
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
}),
score: 1,
type: "action",
2019-11-21 17:47:06 +00:00
url: orderUrl(gqlId)
2019-11-21 12:13:41 +00:00
}
];
}
return [];
}
export default getOrdersModeActions;