Code cleanup
This commit is contained in:
parent
0311790550
commit
c231de44b5
5 changed files with 33 additions and 50 deletions
10
src/misc.ts
10
src/misc.ts
|
@ -4,13 +4,11 @@ import urlJoin from "url-join";
|
|||
|
||||
import { defineMessages, IntlShape } from "react-intl";
|
||||
import { ConfirmButtonTransitionState } from "./components/ConfirmButton/ConfirmButton";
|
||||
import { TableCellHeaderArrowDirection } from "./components/TableCellHeader";
|
||||
import { APP_MOUNT_URI } from "./config";
|
||||
import { AddressType } from "./customers/types";
|
||||
import { PartialMutationProviderOutput, UserError } from "./types";
|
||||
import {
|
||||
AuthorizationKeyType,
|
||||
OrderDirection,
|
||||
OrderStatus,
|
||||
PaymentChargeStatusEnum,
|
||||
TaxRateType
|
||||
|
@ -481,14 +479,6 @@ export function findInEnum<TEnum extends object>(
|
|||
throw new Error(`Key ${needle} not found in enum`);
|
||||
}
|
||||
|
||||
export function getOrderDirection(asc: boolean): OrderDirection {
|
||||
return asc ? OrderDirection.ASC : OrderDirection.DESC;
|
||||
}
|
||||
|
||||
export function getArrowDirection(asc: boolean): TableCellHeaderArrowDirection {
|
||||
return asc ? "asc" : "desc";
|
||||
}
|
||||
|
||||
export function parseBoolean(a: string): boolean {
|
||||
return a === "true";
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ import TableCellHeader from "@saleor/components/TableCellHeader";
|
|||
import TableHead from "@saleor/components/TableHead";
|
||||
import TablePagination from "@saleor/components/TablePagination";
|
||||
import { ProductListColumns } from "@saleor/config";
|
||||
import { getArrowDirection, maybe, renderCollection } from "@saleor/misc";
|
||||
import { maybe, renderCollection } from "@saleor/misc";
|
||||
import {
|
||||
getAttributeIdFromColumnValue,
|
||||
isAttributeColumnValue
|
||||
|
@ -36,6 +36,7 @@ import { ListActions, ListProps, SortPage } from "@saleor/types";
|
|||
import TDisplayColumn, {
|
||||
DisplayColumnProps
|
||||
} from "@saleor/utils/columns/DisplayColumn";
|
||||
import { getArrowDirection } from "@saleor/utils/sort";
|
||||
|
||||
const styles = (theme: Theme) =>
|
||||
createStyles({
|
||||
|
|
|
@ -24,6 +24,7 @@ import { commonMessages } from "@saleor/intl";
|
|||
import { getMutationState, maybe } from "@saleor/misc";
|
||||
import { ProductListVariables } from "@saleor/products/types/ProductList";
|
||||
import { ListViews } from "@saleor/types";
|
||||
import { getSortUrlVariables } from "@saleor/utils/sort";
|
||||
import ProductListPage from "../../components/ProductListPage";
|
||||
import {
|
||||
TypedProductBulkDeleteMutation,
|
||||
|
@ -53,7 +54,7 @@ import {
|
|||
getFilterVariables,
|
||||
saveFilterTab
|
||||
} from "./filters";
|
||||
import { getSortQueryVariables, getSortUrlVariables } from "./sort";
|
||||
import { getSortQueryVariables } from "./sort";
|
||||
|
||||
interface ProductListProps {
|
||||
params: ProductListUrlQueryParams;
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
import { getOrderDirection } from "@saleor/misc";
|
||||
import {
|
||||
ProductListUrlQueryParams,
|
||||
ProductListUrlSortField
|
||||
} from "@saleor/products/urls";
|
||||
import { Sort } from "@saleor/types";
|
||||
import { ProductOrder, ProductOrderField } from "@saleor/types/globalTypes";
|
||||
import { getOrderDirection } from "@saleor/utils/sort";
|
||||
|
||||
export function getSortQueryField(
|
||||
sort: ProductListUrlSortField
|
||||
|
@ -18,8 +17,6 @@ export function getSortQueryField(
|
|||
return ProductOrderField.TYPE;
|
||||
case ProductListUrlSortField.status:
|
||||
return ProductOrderField.PUBLISHED;
|
||||
default:
|
||||
return ProductOrderField.NAME;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -31,37 +28,3 @@ export function getSortQueryVariables(
|
|||
field: getSortQueryField(params.sort)
|
||||
};
|
||||
}
|
||||
|
||||
export function getSortUrlField(
|
||||
sort: ProductOrderField
|
||||
): ProductListUrlSortField {
|
||||
switch (sort) {
|
||||
case ProductOrderField.NAME:
|
||||
return ProductListUrlSortField.name;
|
||||
case ProductOrderField.PRICE:
|
||||
return ProductListUrlSortField.price;
|
||||
case ProductOrderField.TYPE:
|
||||
return ProductListUrlSortField.productType;
|
||||
case ProductOrderField.PUBLISHED:
|
||||
return ProductListUrlSortField.status;
|
||||
default:
|
||||
return ProductListUrlSortField.name;
|
||||
}
|
||||
}
|
||||
|
||||
export function getSortUrlVariables(
|
||||
field: ProductListUrlSortField,
|
||||
params: Sort<ProductListUrlSortField>
|
||||
): Sort<ProductListUrlSortField> {
|
||||
if (field === params.sort) {
|
||||
return {
|
||||
asc: !params.asc,
|
||||
sort: field
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
asc: true,
|
||||
sort: field
|
||||
};
|
||||
}
|
||||
|
|
28
src/utils/sort.ts
Normal file
28
src/utils/sort.ts
Normal file
|
@ -0,0 +1,28 @@
|
|||
import { TableCellHeaderArrowDirection } from "../components/TableCellHeader";
|
||||
import { Sort } from "../types";
|
||||
import { OrderDirection } from "../types/globalTypes";
|
||||
|
||||
export function getSortUrlVariables<TSortKey extends string>(
|
||||
field: TSortKey,
|
||||
params: Sort<TSortKey>
|
||||
): Sort<TSortKey> {
|
||||
if (field === params.sort) {
|
||||
return {
|
||||
asc: !params.asc,
|
||||
sort: field
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
asc: true,
|
||||
sort: field
|
||||
};
|
||||
}
|
||||
|
||||
export function getOrderDirection(asc: boolean): OrderDirection {
|
||||
return asc ? OrderDirection.ASC : OrderDirection.DESC;
|
||||
}
|
||||
|
||||
export function getArrowDirection(asc: boolean): TableCellHeaderArrowDirection {
|
||||
return asc ? "asc" : "desc";
|
||||
}
|
Loading…
Reference in a new issue