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

130 lines
3.7 KiB
TypeScript
Raw Normal View History

2019-08-26 17:08:32 +00:00
import Card from "@material-ui/core/Card";
import CardContent from "@material-ui/core/CardContent";
import TextField from "@material-ui/core/TextField";
2020-08-19 11:35:35 +00:00
import Tooltip from "@material-ui/core/Tooltip";
import Typography from "@material-ui/core/Typography";
import InfoIcon from "@material-ui/icons/Info";
2019-08-26 17:08:32 +00:00
import CardTitle from "@saleor/components/CardTitle";
2020-08-19 11:35:35 +00:00
import ControlledSwitch from "@saleor/components/ControlledSwitch";
import { Plugin_plugin_configuration } from "@saleor/plugins/types/Plugin";
import { makeStyles } from "@saleor/theme";
2020-02-24 14:14:48 +00:00
import { UserError } from "@saleor/types";
2019-08-28 13:08:48 +00:00
import { ConfigurationTypeFieldEnum } from "@saleor/types/globalTypes";
import { getFieldError } from "@saleor/utils/errors";
2019-08-27 12:36:19 +00:00
import React from "react";
2019-08-30 13:10:07 +00:00
import { useIntl } from "react-intl";
2020-10-22 11:33:29 +00:00
import { PluginDetailsPageFormData } from "../PluginsDetailsPage";
2019-08-26 17:08:32 +00:00
interface PluginSettingsProps {
2020-10-22 11:33:29 +00:00
data: PluginDetailsPageFormData;
2020-02-24 14:14:48 +00:00
errors: UserError[];
2019-08-26 17:08:32 +00:00
disabled: boolean;
onChange: (event: React.ChangeEvent<any>) => void;
2019-11-07 15:54:05 +00:00
fields: Plugin_plugin_configuration[];
2019-08-26 17:08:32 +00:00
}
2019-12-03 15:28:40 +00:00
const useStyles = makeStyles(
theme => ({
authItem: {
display: "flex"
},
button: {
marginRight: theme.spacing()
},
item: {
"&:not(:last-child)": {
marginBottom: theme.spacing(3)
2020-08-19 11:35:35 +00:00
},
alignItems: "center",
display: "flex"
2019-12-03 15:28:40 +00:00
},
itemLabel: {
fontWeight: 500
},
spacer: {
flex: 1
2019-11-07 15:54:05 +00:00
}
2019-12-03 15:28:40 +00:00
}),
{ name: "PluginSettings" }
);
2019-08-26 17:08:32 +00:00
const PluginSettings: React.FC<PluginSettingsProps> = ({
2019-08-30 11:58:48 +00:00
data,
disabled,
errors,
onChange,
2019-08-30 13:10:07 +00:00
fields
2019-08-30 11:58:48 +00:00
}) => {
const classes = useStyles({});
2019-08-30 13:10:07 +00:00
const intl = useIntl();
2019-11-07 15:54:05 +00:00
2019-08-30 11:58:48 +00:00
return (
<Card>
<CardTitle
2019-08-30 13:10:07 +00:00
title={intl.formatMessage({
defaultMessage: "Plugin Settings",
2019-08-30 14:15:32 +00:00
description: "section header"
2019-08-30 11:58:48 +00:00
})}
/>
<CardContent>
2019-11-07 15:54:05 +00:00
{data.configuration.map(field => {
const fieldData = fields.find(
configField => configField.name === field.name
);
return (
<div className={classes.item} key={field.name}>
{fieldData.type === ConfigurationTypeFieldEnum.BOOLEAN ? (
2020-08-19 11:35:35 +00:00
<>
<ControlledSwitch
name={field.name}
label={fieldData.label}
checked={
typeof field.value !== "boolean"
? field.value === "true"
: field.value
}
onChange={onChange}
disabled={disabled}
/>
{fieldData.helpText && (
<Tooltip
title={
<Typography variant="body2">
{fieldData.helpText}
</Typography>
}
>
<InfoIcon />
</Tooltip>
)}
</>
2019-11-07 15:54:05 +00:00
) : (
<TextField
disabled={disabled}
2020-02-24 14:14:48 +00:00
error={!!getFieldError(errors, "name")}
2019-11-07 15:54:05 +00:00
helperText={fieldData.helpText}
label={fieldData.label}
name={field.name}
multiline={
fieldData.type === ConfigurationTypeFieldEnum.MULTILINE
}
InputProps={{
rowsMax: 6
}}
2019-11-07 15:54:05 +00:00
fullWidth
value={field.value}
onChange={onChange}
/>
)}
</div>
);
})}
2019-08-30 11:58:48 +00:00
</CardContent>
</Card>
);
};
2019-08-26 17:08:32 +00:00
PluginSettings.displayName = "PluginSettings";
export default PluginSettings;