saleor-dashboard/src/plugins/components/PluginsList/PluginsList.tsx

170 lines
5.1 KiB
TypeScript
Raw Normal View History

2019-10-30 14:34:24 +00:00
import { makeStyles } from "@material-ui/core/styles";
2019-08-21 14:30:56 +00:00
import TableBody from "@material-ui/core/TableBody";
import TableCell from "@material-ui/core/TableCell";
import TableFooter from "@material-ui/core/TableFooter";
2019-09-02 11:24:21 +00:00
import TableHead from "@material-ui/core/TableHead";
2019-08-21 14:30:56 +00:00
import TableRow from "@material-ui/core/TableRow";
import EditIcon from "@material-ui/icons/Edit";
import React from "react";
2019-08-30 13:10:07 +00:00
import { useIntl } from "react-intl";
2019-08-21 14:30:56 +00:00
2019-11-04 14:25:23 +00:00
import ResponsiveTable from "@saleor/components/ResponsiveTable";
2019-08-21 14:30:56 +00:00
import Skeleton from "@saleor/components/Skeleton";
import StatusLabel from "@saleor/components/StatusLabel";
import TablePagination from "@saleor/components/TablePagination";
2019-08-30 13:10:07 +00:00
import { translateBoolean } from "@saleor/intl";
2019-08-21 14:30:56 +00:00
import { maybe, renderCollection } from "@saleor/misc";
2019-12-17 17:13:56 +00:00
import { ListProps, SortPage } from "@saleor/types";
import { PluginListUrlSortField } from "@saleor/plugins/urls";
import TableCellHeader from "@saleor/components/TableCellHeader";
import { getArrowDirection } from "@saleor/utils/sort";
2019-08-28 14:53:57 +00:00
import { Plugins_plugins_edges_node } from "../../types/Plugins";
2019-08-21 14:30:56 +00:00
2019-12-17 17:13:56 +00:00
export interface PluginListProps
extends ListProps,
SortPage<PluginListUrlSortField> {
2019-08-28 14:53:57 +00:00
plugins: Plugins_plugins_edges_node[];
2019-08-21 14:30:56 +00:00
}
2019-12-03 15:28:40 +00:00
const useStyles = makeStyles(
theme => ({
[theme.breakpoints.up("lg")]: {
colAction: {
"& svg": {
color: theme.palette.primary.main
},
textAlign: "right" as "right"
2019-08-27 12:36:19 +00:00
},
2019-12-03 15:28:40 +00:00
colActive: {},
colName: {}
2019-08-21 14:30:56 +00:00
},
2019-12-03 15:28:40 +00:00
colAction: {},
2019-08-27 12:36:19 +00:00
colActive: {},
2019-12-03 15:28:40 +00:00
colName: {},
link: {
cursor: "pointer"
}
}),
{ name: "PluginsList" }
);
2019-08-21 14:30:56 +00:00
const numberOfColumns = 4;
2019-10-30 14:34:24 +00:00
const PluginList: React.FC<PluginListProps> = props => {
const {
2019-08-21 14:30:56 +00:00
settings,
plugins,
disabled,
onNextPage,
pageInfo,
2019-12-17 17:13:56 +00:00
sort,
2019-08-21 14:30:56 +00:00
onRowClick,
2019-12-17 17:13:56 +00:00
onSort,
2019-08-21 14:30:56 +00:00
onUpdateListSettings,
onPreviousPage
2019-10-30 14:34:24 +00:00
} = props;
const classes = useStyles(props);
const intl = useIntl();
2020-01-10 16:30:42 +00:00
2019-10-30 14:34:24 +00:00
return (
2020-01-10 16:30:42 +00:00
<ResponsiveTable>
<TableHead>
<TableCellHeader
direction={
sort.sort === PluginListUrlSortField.name
? getArrowDirection(sort.asc)
: undefined
}
arrowPosition="right"
onClick={() => onSort(PluginListUrlSortField.name)}
className={classes.colName}
>
{intl.formatMessage({
defaultMessage: "Name",
description: "plugin name"
})}
</TableCellHeader>
<TableCellHeader
direction={
sort.sort === PluginListUrlSortField.active
? getArrowDirection(sort.asc)
: undefined
}
onClick={() => onSort(PluginListUrlSortField.active)}
className={classes.colActive}
>
{intl.formatMessage({
defaultMessage: "Active",
description: "plugin status"
})}
</TableCellHeader>
<TableCell className={classes.colAction}>
{intl.formatMessage({
defaultMessage: "Action",
description: "user action bar"
})}
</TableCell>
</TableHead>
<TableFooter>
<TableRow>
<TablePagination
colSpan={numberOfColumns}
settings={settings}
hasNextPage={pageInfo && !disabled ? pageInfo.hasNextPage : false}
onNextPage={onNextPage}
onUpdateListSettings={onUpdateListSettings}
hasPreviousPage={
pageInfo && !disabled ? pageInfo.hasPreviousPage : false
2019-12-17 17:13:56 +00:00
}
2020-01-10 16:30:42 +00:00
onPreviousPage={onPreviousPage}
/>
</TableRow>
</TableFooter>
<TableBody>
{renderCollection(
plugins,
plugin => (
<TableRow
hover={!!plugin}
className={!!plugin ? classes.link : undefined}
onClick={plugin ? onRowClick(plugin.id) : undefined}
key={plugin ? plugin.id : "skeleton"}
>
<TableCell className={classes.colName}>
{maybe<React.ReactNode>(() => plugin.name, <Skeleton />)}
</TableCell>
<TableCell className={classes.colActive}>
{maybe<React.ReactNode>(
() => (
<StatusLabel
label={translateBoolean(plugin.active, intl)}
status={plugin.active ? "success" : "error"}
/>
),
<Skeleton />
)}
</TableCell>
<TableCell className={classes.colAction}>
<div onClick={plugin ? onRowClick(plugin.id) : undefined}>
<EditIcon />
</div>
</TableCell>
</TableRow>
),
() => (
<TableRow>
<TableCell colSpan={numberOfColumns}>
{intl.formatMessage({
defaultMessage: "No plugins found"
})}
</TableCell>
</TableRow>
)
)}
</TableBody>
</ResponsiveTable>
2019-10-30 14:34:24 +00:00
);
};
2019-08-21 14:30:56 +00:00
PluginList.displayName = "PluginList";
export default PluginList;