saleor-dashboard/src/attributes/components/AttributeValues/AttributeValues.tsx
Piotr Grundas ebe56da583
Add swatch attributes (#1301)
* Initial work

* #

* Base color logic

* Handle file urls

* Improvements, fix snapshots

* Add messages

* Update changelog

* Update storybook

* Improvements

* Messages

* Review corrections

* Bugfixes
2021-09-21 15:16:21 +02:00

214 lines
6.3 KiB
TypeScript

import {
Button,
Card,
IconButton,
TableCell,
TableFooter,
TableHead,
TableRow
} from "@material-ui/core";
import DeleteIcon from "@material-ui/icons/Delete";
import CardTitle from "@saleor/components/CardTitle";
import ResponsiveTable from "@saleor/components/ResponsiveTable";
import Skeleton from "@saleor/components/Skeleton";
import {
SortableTableBody,
SortableTableRow
} from "@saleor/components/SortableTable";
import TablePagination from "@saleor/components/TablePagination";
import { AttributeValueListFragment_edges_node } from "@saleor/fragments/types/AttributeValueListFragment";
import { makeStyles } from "@saleor/macaw-ui";
import { renderCollection, stopPropagation } from "@saleor/misc";
import { ListProps, ReorderAction } from "@saleor/types";
import { AttributeInputTypeEnum } from "@saleor/types/globalTypes";
import React from "react";
import { FormattedMessage, useIntl } from "react-intl";
export interface AttributeValuesProps
extends Pick<ListProps, Exclude<keyof ListProps, "onRowClick">> {
disabled: boolean;
values: AttributeValueListFragment_edges_node[];
onValueAdd: () => void;
onValueDelete: (id: string) => void;
onValueReorder: ReorderAction;
onValueUpdate: (id: string) => void;
inputType: AttributeInputTypeEnum;
}
const useStyles = makeStyles(
theme => ({
columnSwatch: {
width: 100
},
columnAdmin: {
width: 300
},
columnDrag: {
width: theme.spacing(6 + 1.5)
},
columnStore: {
width: "auto"
},
dragIcon: {
cursor: "grab"
},
iconCell: {
"&:last-child": {
paddingRight: theme.spacing()
},
width: 80
},
link: {
cursor: "pointer"
},
swatch: {
width: 32,
height: 32,
borderRadius: 4,
backgroundSize: "cover",
backgroundPosition: "center"
}
}),
{ name: "AttributeValues" }
);
const AttributeValues: React.FC<AttributeValuesProps> = ({
disabled,
onValueAdd,
onValueDelete,
onValueReorder,
onValueUpdate,
values,
settings,
onUpdateListSettings,
pageInfo,
onNextPage,
onPreviousPage,
inputType
}) => {
const classes = useStyles({});
const intl = useIntl();
const isSwatch = inputType === AttributeInputTypeEnum.SWATCH;
const numberOfColumns = isSwatch ? 5 : 4;
return (
<Card>
<CardTitle
title={intl.formatMessage({
defaultMessage: "Attribute Values",
description: "section header"
})}
toolbar={
<Button
color="primary"
variant="text"
onClick={onValueAdd}
data-test-id="assignValueButton"
>
<FormattedMessage
defaultMessage="Assign value"
description="assign attribute value button"
/>
</Button>
}
/>
<ResponsiveTable>
<TableHead>
<TableRow>
<TableCell className={classes.columnDrag} />
{isSwatch && (
<TableCell className={classes.columnSwatch}>
<FormattedMessage
defaultMessage="Swatch"
description="attribute values list: slug column header"
/>
</TableCell>
)}
<TableCell className={classes.columnAdmin}>
<FormattedMessage
defaultMessage="Admin"
description="attribute values list: slug column header"
/>
</TableCell>
<TableCell className={classes.columnStore}>
<FormattedMessage
defaultMessage="Default Store View"
description="attribute values list: name column header"
/>
</TableCell>
<TableCell className={classes.iconCell} />
</TableRow>
</TableHead>
<TableFooter>
<TableRow>
<TablePagination
colSpan={numberOfColumns}
hasNextPage={pageInfo && !disabled ? pageInfo.hasNextPage : false}
onNextPage={onNextPage}
hasPreviousPage={
pageInfo && !disabled ? pageInfo.hasPreviousPage : false
}
onPreviousPage={onPreviousPage}
settings={settings}
onUpdateListSettings={onUpdateListSettings}
/>
</TableRow>
</TableFooter>
<SortableTableBody onSortEnd={onValueReorder}>
{renderCollection(
values,
(value, valueIndex) => (
<SortableTableRow
className={!!value ? classes.link : undefined}
hover={!!value}
onClick={!!value ? () => onValueUpdate(value.id) : undefined}
key={value?.id}
index={valueIndex || 0}
>
{isSwatch && (
<TableCell className={classes.columnSwatch}>
<div
className={classes.swatch}
style={
value?.file
? { backgroundImage: `url(${value.file.url})` }
: { backgroundColor: value.value }
}
/>
</TableCell>
)}
<TableCell className={classes.columnAdmin}>
{value?.slug ?? <Skeleton />}
</TableCell>
<TableCell className={classes.columnStore}>
{value?.name ?? <Skeleton />}
</TableCell>
<TableCell className={classes.iconCell}>
<IconButton
disabled={disabled}
onClick={stopPropagation(() => onValueDelete(value.id))}
>
<DeleteIcon color="primary" />
</IconButton>
</TableCell>
</SortableTableRow>
),
() => (
<TableRow>
<TableCell colSpan={numberOfColumns}>
<FormattedMessage
defaultMessage="No values found"
description="No attribute values found"
/>
</TableCell>
</TableRow>
)
)}
</SortableTableBody>
</ResponsiveTable>
</Card>
);
};
AttributeValues.displayName = "AttributeValues";
export default AttributeValues;