saleor-dashboard/src/webhooks/components/WebhookInfo/WebhookInfo.tsx

161 lines
4.9 KiB
TypeScript
Raw Normal View History

2019-10-09 06:01:52 +00:00
import Card from "@material-ui/core/Card";
import CardContent from "@material-ui/core/CardContent";
2019-10-28 16:16:49 +00:00
import makeStyles from "@material-ui/core/styles/makeStyles";
2019-10-09 20:50:03 +00:00
import TextField from "@material-ui/core/TextField";
2019-10-09 06:01:52 +00:00
import Typography from "@material-ui/core/Typography";
import CardTitle from "@saleor/components/CardTitle";
import FormSpacer from "@saleor/components/FormSpacer";
import Hr from "@saleor/components/Hr";
2019-10-17 12:38:01 +00:00
import SingleAutocompleteSelectField, {
SingleAutocompleteChoiceType
} from "@saleor/components/SingleAutocompleteSelectField";
import { ChangeEvent } from "@saleor/hooks/useForm";
2019-10-11 13:35:33 +00:00
import { commonMessages } from "@saleor/intl";
2020-03-18 11:03:20 +00:00
import { getFormErrors } from "@saleor/utils/errors";
import getWebhookErrorMessage from "@saleor/utils/errors/webhooks";
import { WebhookErrorFragment } from "@saleor/webhooks/types/WebhookErrorFragment";
import React from "react";
import { useIntl } from "react-intl";
2019-10-09 06:01:52 +00:00
import { FormData } from "../WebhooksDetailsPage";
interface WebhookInfoProps {
data: FormData;
2019-10-09 20:50:03 +00:00
disabled: boolean;
2020-03-18 11:03:20 +00:00
errors: WebhookErrorFragment[];
serviceDisplayValue: string;
services: SingleAutocompleteChoiceType[];
2019-10-09 06:01:52 +00:00
onChange: (event: React.ChangeEvent<any>) => void;
serviceOnChange: (event: ChangeEvent) => void;
2019-10-17 11:59:18 +00:00
fetchServiceAccounts: (data: string) => void;
2019-10-09 06:01:52 +00:00
}
2019-12-03 15:28:40 +00:00
const useStyles = makeStyles(
() => ({
status: {
paddingTop: 20
},
title: {
fontSize: 16,
lineHeight: 1.9,
paddingBottom: 10
}
}),
{ name: "WebhookInfo" }
);
2019-10-09 06:01:52 +00:00
const WebhookInfo: React.FC<WebhookInfoProps> = ({
2019-10-09 06:01:52 +00:00
data,
2019-10-09 20:50:03 +00:00
disabled,
services,
serviceDisplayValue,
2019-10-17 11:59:18 +00:00
fetchServiceAccounts,
2019-10-09 20:50:03 +00:00
errors,
onChange,
serviceOnChange
2019-10-09 06:01:52 +00:00
}) => {
const classes = useStyles({});
const intl = useIntl();
2020-03-18 11:03:20 +00:00
const serviceAccountError = errors.find(error => error.field === null);
const formErrors = getFormErrors(["name", "targetUrl", "secretKey"], errors);
2019-10-18 12:01:26 +00:00
2019-10-09 06:01:52 +00:00
return (
<Card>
<CardTitle
title={intl.formatMessage({
2019-10-09 20:50:03 +00:00
defaultMessage: "Webhook Information",
2019-10-09 06:01:52 +00:00
description: "section header"
})}
/>
2019-10-09 20:50:03 +00:00
<CardContent>
<Typography className={classes.title}>
2019-10-11 13:35:33 +00:00
{intl.formatMessage(commonMessages.generalInformations)}
2019-10-09 20:50:03 +00:00
</Typography>
<TextField
disabled={disabled}
2020-03-18 11:03:20 +00:00
error={!!formErrors.name}
helperText={getWebhookErrorMessage(formErrors.name, intl)}
2019-10-09 20:50:03 +00:00
label={intl.formatMessage({
defaultMessage: "Webhook Name",
description: "webhook"
})}
fullWidth
name="name"
value={data.name}
onChange={onChange}
/>
<FormSpacer />
<Hr />
<FormSpacer />
<Typography className={classes.title}>
{intl.formatMessage({
defaultMessage: "Webhook specific information",
description: "webhook specific information"
})}
</Typography>
2019-10-11 13:35:33 +00:00
<SingleAutocompleteSelectField
disabled={disabled}
displayValue={serviceDisplayValue}
2019-10-09 20:50:03 +00:00
label={intl.formatMessage({
defaultMessage: "Assign to Service Account"
})}
2020-03-18 11:03:20 +00:00
error={!!serviceAccountError}
helperText={getWebhookErrorMessage(serviceAccountError, intl)}
2019-10-11 13:35:33 +00:00
name="serviceAccount"
onChange={serviceOnChange}
2019-10-11 13:35:33 +00:00
value={data.serviceAccount}
choices={services}
2019-10-17 11:59:18 +00:00
fetchChoices={fetchServiceAccounts}
2019-10-11 13:35:33 +00:00
InputProps={{
autoComplete: "off"
}}
2019-10-09 20:50:03 +00:00
/>
<FormSpacer />
<TextField
disabled={disabled}
2020-03-18 11:03:20 +00:00
error={!!formErrors.targetUrl}
2020-02-24 14:14:48 +00:00
helperText={
2020-03-18 11:03:20 +00:00
getWebhookErrorMessage(formErrors.targetUrl, intl) ||
2020-02-24 14:14:48 +00:00
intl.formatMessage({
defaultMessage: "This URL will receive webhook POST requests",
description: "webhook target url help text"
})
}
2019-10-09 20:50:03 +00:00
label={intl.formatMessage({
defaultMessage: "Target URL",
description: "webhook"
})}
fullWidth
name="targetUrl"
value={data.targetUrl}
onChange={onChange}
/>
<FormSpacer />
<TextField
disabled={disabled}
2020-03-18 11:03:20 +00:00
error={!!formErrors.secretKey}
2020-02-24 14:14:48 +00:00
helperText={
2020-03-18 11:03:20 +00:00
getWebhookErrorMessage(formErrors.secretKey, intl) ||
2020-02-24 14:14:48 +00:00
intl.formatMessage({
defaultMessage:
"secret key is used to create a hash signature with each payload. *optional field",
description: "webhook secret key help text"
})
}
2019-10-09 20:50:03 +00:00
label={intl.formatMessage({
defaultMessage: "Secrect Key",
description: "webhook"
})}
fullWidth
name="secretKey"
value={data.secretKey}
onChange={onChange}
/>
</CardContent>
2019-10-09 06:01:52 +00:00
</Card>
);
};
WebhookInfo.displayName = "WebhookInfo";
export default WebhookInfo;