2021-05-14 08:15:15 +00:00
|
|
|
import {
|
|
|
|
Card,
|
|
|
|
CardActions,
|
|
|
|
CardContent,
|
|
|
|
Table,
|
|
|
|
TableBody,
|
|
|
|
TableCell,
|
|
|
|
TableHead,
|
|
|
|
TableRow,
|
|
|
|
TextField,
|
|
|
|
Typography
|
|
|
|
} from "@material-ui/core";
|
2020-08-28 12:45:11 +00:00
|
|
|
import { FormChange } from "@saleor/hooks/useForm";
|
2022-01-28 12:34:20 +00:00
|
|
|
import { Button, DeleteIcon, ExpandIcon, IconButton } from "@saleor/macaw-ui";
|
2020-08-28 12:45:11 +00:00
|
|
|
import { MetadataInput } from "@saleor/types/globalTypes";
|
2022-01-28 12:34:20 +00:00
|
|
|
import classNames from "classnames";
|
2020-08-28 12:45:11 +00:00
|
|
|
import React, { useEffect } from "react";
|
|
|
|
import { useIntl } from "react-intl";
|
|
|
|
import { FormattedMessage } from "react-intl";
|
|
|
|
|
|
|
|
import CardTitle from "../CardTitle";
|
|
|
|
import Skeleton from "../Skeleton";
|
|
|
|
import useStyles from "./styles";
|
|
|
|
import { EventDataAction, EventDataField } from "./types";
|
|
|
|
|
|
|
|
export interface MetadataCardProps {
|
|
|
|
data: MetadataInput[];
|
|
|
|
isPrivate: boolean;
|
|
|
|
onChange: FormChange;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const nameSeparator = ":";
|
|
|
|
export const nameInputPrefix = EventDataField.name;
|
|
|
|
export const valueInputPrefix = EventDataField.value;
|
|
|
|
|
|
|
|
const MetadataCard: React.FC<MetadataCardProps> = ({
|
|
|
|
data,
|
|
|
|
isPrivate,
|
|
|
|
onChange
|
|
|
|
}) => {
|
|
|
|
const intl = useIntl();
|
|
|
|
const loaded = React.useRef(false);
|
|
|
|
const [expanded, setExpanded] = React.useState(true);
|
|
|
|
const classes = useStyles({});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (data !== undefined) {
|
|
|
|
loaded.current = true;
|
|
|
|
if (data.length > 0) {
|
|
|
|
setExpanded(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [data === undefined]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Card
|
2022-02-11 11:28:55 +00:00
|
|
|
data-test-id="metadata-editor"
|
2020-08-28 12:45:11 +00:00
|
|
|
data-test-is-private={isPrivate}
|
|
|
|
data-test-expanded={expanded}
|
|
|
|
>
|
|
|
|
<CardTitle
|
2022-01-28 12:34:20 +00:00
|
|
|
className={classes.header}
|
2020-08-28 12:45:11 +00:00
|
|
|
title={
|
2022-01-28 12:34:20 +00:00
|
|
|
<>
|
|
|
|
{isPrivate
|
|
|
|
? intl.formatMessage({
|
|
|
|
defaultMessage: "Private Metadata",
|
|
|
|
description: "header"
|
|
|
|
})
|
|
|
|
: intl.formatMessage({
|
|
|
|
defaultMessage: "Metadata",
|
|
|
|
description: "header"
|
|
|
|
})}
|
|
|
|
<IconButton
|
|
|
|
className={classNames(classes.expandBtn, {
|
|
|
|
[classes.rotate]: expanded
|
|
|
|
})}
|
|
|
|
hoverOutline={false}
|
|
|
|
variant="secondary"
|
2022-02-11 11:28:55 +00:00
|
|
|
data-test-id="expand"
|
2022-01-28 12:34:20 +00:00
|
|
|
onClick={() => setExpanded(!expanded)}
|
|
|
|
>
|
|
|
|
<ExpandIcon />
|
|
|
|
</IconButton>
|
|
|
|
</>
|
2020-08-28 12:45:11 +00:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
{data === undefined ? (
|
|
|
|
<CardContent>
|
|
|
|
<Skeleton />
|
|
|
|
</CardContent>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<CardContent className={classes.content}>
|
|
|
|
{data.length > 0 && (
|
2022-01-28 12:34:20 +00:00
|
|
|
<Typography color="textSecondary" variant="body2">
|
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="{number,plural,one{{number} string} other{{number} strings}}"
|
|
|
|
description="number of metadata fields in model"
|
|
|
|
values={{
|
|
|
|
number: data.length
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Typography>
|
2020-08-28 12:45:11 +00:00
|
|
|
)}
|
|
|
|
</CardContent>
|
|
|
|
{expanded && (
|
|
|
|
<>
|
|
|
|
{data.length === 0 ? (
|
2022-01-28 12:34:20 +00:00
|
|
|
<CardContent className={classes.emptyContainer}>
|
|
|
|
<Typography variant="body2" color="textSecondary">
|
2020-08-28 12:45:11 +00:00
|
|
|
<FormattedMessage
|
2022-01-28 12:34:20 +00:00
|
|
|
defaultMessage="No metadata created for this element. Use the button below to add new metadata field."
|
2020-08-28 12:45:11 +00:00
|
|
|
description="empty metadata text"
|
|
|
|
/>
|
|
|
|
</Typography>
|
2022-01-28 12:34:20 +00:00
|
|
|
</CardContent>
|
2020-08-28 12:45:11 +00:00
|
|
|
) : (
|
|
|
|
<Table className={classes.table}>
|
|
|
|
<TableHead>
|
|
|
|
<TableRow>
|
|
|
|
<TableCell className={classes.colNameHeader}>
|
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="Field"
|
|
|
|
description="metadata field name, header"
|
|
|
|
/>
|
|
|
|
</TableCell>
|
|
|
|
<TableCell className={classes.colValue}>
|
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="Value"
|
|
|
|
description="metadata field value, header"
|
|
|
|
/>
|
|
|
|
</TableCell>
|
|
|
|
<TableCell className={classes.colActionHeader}>
|
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="Actions"
|
|
|
|
description="table action"
|
|
|
|
/>
|
|
|
|
</TableCell>
|
|
|
|
</TableRow>
|
|
|
|
</TableHead>
|
|
|
|
<TableBody>
|
|
|
|
{data.map((field, fieldIndex) => (
|
2022-02-11 11:28:55 +00:00
|
|
|
<TableRow data-test-id="field" key={fieldIndex}>
|
2020-08-28 12:45:11 +00:00
|
|
|
<TableCell className={classes.colName}>
|
|
|
|
<TextField
|
|
|
|
InputProps={{
|
|
|
|
classes: {
|
|
|
|
input: classes.nameInput
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
name={`${nameInputPrefix}${nameSeparator}${fieldIndex}`}
|
|
|
|
fullWidth
|
|
|
|
onChange={onChange}
|
|
|
|
value={field.key}
|
|
|
|
/>
|
|
|
|
</TableCell>
|
|
|
|
<TableCell className={classes.colValue}>
|
|
|
|
<TextField
|
|
|
|
InputProps={{
|
|
|
|
classes: {
|
|
|
|
root: classes.input
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
multiline
|
|
|
|
name={`${valueInputPrefix}${nameSeparator}${fieldIndex}`}
|
|
|
|
fullWidth
|
|
|
|
onChange={onChange}
|
|
|
|
value={field.value}
|
|
|
|
/>
|
|
|
|
</TableCell>
|
|
|
|
<TableCell className={classes.colAction}>
|
|
|
|
<IconButton
|
2022-01-28 12:34:20 +00:00
|
|
|
variant="secondary"
|
2022-02-11 11:28:55 +00:00
|
|
|
data-test-id={"delete-field-" + fieldIndex}
|
2020-08-28 12:45:11 +00:00
|
|
|
onClick={() =>
|
|
|
|
onChange({
|
|
|
|
target: {
|
|
|
|
name: EventDataAction.delete,
|
|
|
|
value: fieldIndex
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<DeleteIcon />
|
|
|
|
</IconButton>
|
|
|
|
</TableCell>
|
|
|
|
</TableRow>
|
|
|
|
))}
|
|
|
|
</TableBody>
|
|
|
|
</Table>
|
|
|
|
)}
|
2022-01-28 12:34:20 +00:00
|
|
|
<CardActions className={classes.actions}>
|
2020-08-28 12:45:11 +00:00
|
|
|
<Button
|
2022-01-28 12:34:20 +00:00
|
|
|
variant="secondary"
|
2022-02-11 11:28:55 +00:00
|
|
|
data-test-id="add-field"
|
2020-08-28 12:45:11 +00:00
|
|
|
onClick={() =>
|
|
|
|
onChange({
|
|
|
|
target: {
|
|
|
|
name: EventDataAction.add,
|
|
|
|
value: null
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="Add Field"
|
|
|
|
description="add metadata field,button"
|
|
|
|
/>
|
|
|
|
</Button>
|
|
|
|
</CardActions>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
MetadataCard.displayName = "MetadataCard";
|
|
|
|
export default MetadataCard;
|