saleor-dashboard/src/attributes/components/AttributeList/AttributeList.tsx

218 lines
6.5 KiB
TypeScript
Raw Normal View History

2019-10-28 16:16:49 +00:00
import { makeStyles } from "@material-ui/core/styles";
2019-08-09 10:17:04 +00:00
import TableBody from "@material-ui/core/TableBody";
import TableCell from "@material-ui/core/TableCell";
import TableFooter from "@material-ui/core/TableFooter";
import TableRow from "@material-ui/core/TableRow";
import React from "react";
2019-08-29 10:55:56 +00:00
import { FormattedMessage, useIntl } from "react-intl";
2019-08-09 10:17:04 +00:00
import Checkbox from "@saleor/components/Checkbox";
2019-11-04 14:25:23 +00:00
import ResponsiveTable from "@saleor/components/ResponsiveTable";
2019-08-09 10:17:04 +00:00
import Skeleton from "@saleor/components/Skeleton";
import TableHead from "@saleor/components/TableHead";
import TablePagination from "@saleor/components/TablePagination";
2019-08-29 10:55:56 +00:00
import { translateBoolean } from "@saleor/intl";
2019-09-25 09:26:46 +00:00
import { maybe, renderCollection } from "@saleor/misc";
2019-08-09 10:17:04 +00:00
import { ListActions, ListProps } from "@saleor/types";
import { AttributeList_attributes_edges_node } from "../../types/AttributeList";
export interface AttributeListProps extends ListProps, ListActions {
attributes: AttributeList_attributes_edges_node[];
}
2019-12-03 15:28:40 +00:00
const useStyles = makeStyles(
theme => ({
[theme.breakpoints.up("lg")]: {
colFaceted: {
width: 150
},
colName: {
width: "auto"
},
colSearchable: {
width: 150
},
colSlug: {
width: 200
},
colVisible: {
width: 150
}
2019-08-09 10:17:04 +00:00
},
2019-12-03 15:28:40 +00:00
colFaceted: {
textAlign: "center"
2019-08-09 10:17:04 +00:00
},
2019-12-03 15:28:40 +00:00
colName: {},
2019-08-09 10:17:04 +00:00
colSearchable: {
2019-12-03 15:28:40 +00:00
textAlign: "center"
2019-08-09 10:17:04 +00:00
},
colSlug: {
2019-12-03 15:28:40 +00:00
paddingLeft: 0
2019-08-09 10:17:04 +00:00
},
colVisible: {
2019-12-03 15:28:40 +00:00
textAlign: "center"
},
link: {
cursor: "pointer"
2019-08-09 10:17:04 +00:00
}
2019-12-03 15:28:40 +00:00
}),
{ name: "AttributeList" }
);
2019-08-09 10:17:04 +00:00
const numberOfColumns = 6;
const AttributeList: React.FC<AttributeListProps> = ({
2019-08-09 10:17:04 +00:00
attributes,
disabled,
isChecked,
onNextPage,
onPreviousPage,
onRowClick,
pageInfo,
selected,
toggle,
toggleAll,
toolbar
}) => {
const classes = useStyles({});
2019-08-29 10:55:56 +00:00
const intl = useIntl();
2019-08-09 10:17:04 +00:00
return (
2019-11-04 14:25:23 +00:00
<ResponsiveTable>
2019-08-09 10:17:04 +00:00
<TableHead
colSpan={numberOfColumns}
selected={selected}
disabled={disabled}
items={attributes}
toggleAll={toggleAll}
toolbar={toolbar}
>
<TableCell className={classes.colSlug}>
2019-08-22 16:25:55 +00:00
<FormattedMessage defaultMessage="Attribute Code" />
2019-08-09 10:17:04 +00:00
</TableCell>
<TableCell className={classes.colName}>
<FormattedMessage
defaultMessage="Default Label"
2019-08-20 09:17:06 +00:00
description="attribute's label'"
/>
2019-08-09 10:17:04 +00:00
</TableCell>
<TableCell className={classes.colVisible}>
<FormattedMessage
defaultMessage="Visible"
2019-08-20 09:17:06 +00:00
description="attribute is visible"
/>
2019-08-09 10:17:04 +00:00
</TableCell>
<TableCell className={classes.colSearchable}>
<FormattedMessage
defaultMessage="Searchable"
2019-08-20 09:17:06 +00:00
description="attribute can be searched in dashboard"
/>
2019-08-09 10:17:04 +00:00
</TableCell>
<TableCell className={classes.colFaceted}>
<FormattedMessage
defaultMessage="Use in faceted search"
2019-08-20 09:17:06 +00:00
description="attribute can be searched in storefront"
/>
2019-08-09 10:17:04 +00:00
</TableCell>
</TableHead>
<TableFooter>
<TableRow>
<TablePagination
colSpan={numberOfColumns}
hasNextPage={pageInfo && !disabled ? pageInfo.hasNextPage : false}
onNextPage={onNextPage}
hasPreviousPage={
pageInfo && !disabled ? pageInfo.hasPreviousPage : false
}
onPreviousPage={onPreviousPage}
/>
</TableRow>
</TableFooter>
<TableBody>
{renderCollection(
attributes,
attribute => {
const isSelected = attribute ? isChecked(attribute.id) : false;
return (
<TableRow
selected={isSelected}
hover={!!attribute}
key={attribute ? attribute.id : "skeleton"}
onClick={attribute && onRowClick(attribute.id)}
className={classes.link}
2019-09-25 09:26:46 +00:00
data-tc="id"
data-tc-id={maybe(() => attribute.id)}
data-tc-values={JSON.stringify(
maybe(() => attribute.values, [])
)}
2019-08-09 10:17:04 +00:00
>
<TableCell padding="checkbox">
<Checkbox
checked={isSelected}
disabled={disabled}
disableClickPropagation
onChange={() => toggle(attribute.id)}
/>
</TableCell>
2019-09-25 09:26:46 +00:00
<TableCell className={classes.colSlug} data-tc="slug">
2019-08-09 10:17:04 +00:00
{attribute ? attribute.slug : <Skeleton />}
</TableCell>
2019-09-25 09:26:46 +00:00
<TableCell className={classes.colName} data-tc="name">
2019-08-09 10:17:04 +00:00
{attribute ? attribute.name : <Skeleton />}
</TableCell>
2019-09-25 09:26:46 +00:00
<TableCell
className={classes.colVisible}
data-tc="visible"
data-tc-visible={maybe(() => attribute.visibleInStorefront)}
>
2019-08-09 10:17:04 +00:00
{attribute ? (
2019-08-29 10:55:56 +00:00
translateBoolean(attribute.visibleInStorefront, intl)
2019-08-09 10:17:04 +00:00
) : (
<Skeleton />
)}
</TableCell>
2019-09-25 09:26:46 +00:00
<TableCell
className={classes.colSearchable}
data-tc="searchable"
data-tc-searchable={maybe(
() => attribute.filterableInDashboard
)}
>
2019-08-09 10:17:04 +00:00
{attribute ? (
2019-08-29 10:55:56 +00:00
translateBoolean(attribute.filterableInDashboard, intl)
2019-08-09 10:17:04 +00:00
) : (
<Skeleton />
)}
</TableCell>
2019-09-25 09:26:46 +00:00
<TableCell
className={classes.colFaceted}
data-tc="use-in-faceted-search"
data-tc-use-in-faceted-search={maybe(
() => attribute.filterableInStorefront
)}
>
2019-08-09 10:17:04 +00:00
{attribute ? (
2019-08-29 10:55:56 +00:00
translateBoolean(attribute.filterableInStorefront, intl)
2019-08-09 10:17:04 +00:00
) : (
<Skeleton />
)}
</TableCell>
</TableRow>
);
},
() => (
<TableRow>
<TableCell colSpan={numberOfColumns}>
2019-08-22 16:25:55 +00:00
<FormattedMessage defaultMessage="No attributes found" />
2019-08-09 10:17:04 +00:00
</TableCell>
</TableRow>
)
)}
</TableBody>
2019-11-04 14:25:23 +00:00
</ResponsiveTable>
2019-08-09 10:17:04 +00:00
);
};
AttributeList.displayName = "AttributeList";
export default AttributeList;