2019-10-09 20:50:03 +00:00
|
|
|
import AppHeader from "@saleor/components/AppHeader";
|
|
|
|
import { ConfirmButtonTransitionState } from "@saleor/components/ConfirmButton";
|
|
|
|
import Container from "@saleor/components/Container";
|
|
|
|
import Form from "@saleor/components/Form";
|
|
|
|
import FormSpacer from "@saleor/components/FormSpacer";
|
|
|
|
import Grid from "@saleor/components/Grid";
|
|
|
|
import PageHeader from "@saleor/components/PageHeader";
|
|
|
|
import SaveButtonBar from "@saleor/components/SaveButtonBar";
|
|
|
|
import { sectionNames } from "@saleor/intl";
|
|
|
|
import { maybe } from "@saleor/misc";
|
2019-11-19 15:23:17 +00:00
|
|
|
import { SearchServiceAccount_search_edges_node } from "@saleor/searches/types/SearchServiceAccount";
|
2019-10-10 11:40:31 +00:00
|
|
|
import { WebhookEventTypeEnum } from "@saleor/types/globalTypes";
|
2019-10-14 14:41:41 +00:00
|
|
|
import createSingleAutocompleteSelectHandler from "@saleor/utils/handlers/singleAutocompleteSelectChangeHandler";
|
2019-10-11 13:35:33 +00:00
|
|
|
import WebhookEvents from "@saleor/webhooks/components/WebhookEvents";
|
|
|
|
import WebhookInfo from "@saleor/webhooks/components/WebhookInfo";
|
|
|
|
import WebhookStatus from "@saleor/webhooks/components/WebhookStatus";
|
2019-10-18 12:01:26 +00:00
|
|
|
import { WebhookCreate_webhookCreate_webhookErrors } from "@saleor/webhooks/types/WebhookCreate";
|
2019-10-09 20:50:03 +00:00
|
|
|
import React from "react";
|
|
|
|
import { useIntl } from "react-intl";
|
|
|
|
|
|
|
|
export interface FormData {
|
2019-10-10 11:40:31 +00:00
|
|
|
events: WebhookEventTypeEnum[];
|
2019-10-09 20:50:03 +00:00
|
|
|
isActive: boolean;
|
|
|
|
name: string;
|
|
|
|
secretKey: string | null;
|
|
|
|
targetUrl: string;
|
|
|
|
serviceAccount: string;
|
2019-10-10 11:40:31 +00:00
|
|
|
allEvents: boolean;
|
2019-10-09 20:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface WebhookCreatePageProps {
|
|
|
|
disabled: boolean;
|
2019-10-18 12:01:26 +00:00
|
|
|
errors: WebhookCreate_webhookCreate_webhookErrors[];
|
2019-10-17 12:38:01 +00:00
|
|
|
services?: SearchServiceAccount_search_edges_node[];
|
2019-10-09 20:50:03 +00:00
|
|
|
saveButtonBarState: ConfirmButtonTransitionState;
|
2019-10-17 11:59:18 +00:00
|
|
|
fetchServiceAccounts: (data: string) => void;
|
2019-10-09 20:50:03 +00:00
|
|
|
onBack: () => void;
|
|
|
|
onSubmit: (data: FormData) => void;
|
|
|
|
}
|
|
|
|
|
2019-10-18 12:01:26 +00:00
|
|
|
const WebhookCreatePage: React.FC<WebhookCreatePageProps> = ({
|
2019-10-09 20:50:03 +00:00
|
|
|
disabled,
|
2019-10-18 12:01:26 +00:00
|
|
|
errors: apiErrors,
|
2019-10-09 20:50:03 +00:00
|
|
|
saveButtonBarState,
|
|
|
|
services,
|
2019-10-17 11:59:18 +00:00
|
|
|
fetchServiceAccounts,
|
2019-10-09 20:50:03 +00:00
|
|
|
onBack,
|
|
|
|
onSubmit
|
|
|
|
}) => {
|
|
|
|
const intl = useIntl();
|
|
|
|
const initialForm: FormData = {
|
2019-10-10 11:40:31 +00:00
|
|
|
allEvents: false,
|
2019-10-10 05:38:21 +00:00
|
|
|
events: [],
|
|
|
|
isActive: false,
|
2019-11-19 15:23:17 +00:00
|
|
|
name: "",
|
2019-10-10 05:38:21 +00:00
|
|
|
secretKey: "",
|
|
|
|
serviceAccount: "",
|
|
|
|
targetUrl: ""
|
2019-10-09 20:50:03 +00:00
|
|
|
};
|
2019-10-17 11:59:18 +00:00
|
|
|
const [selectedServiceAcccount, setSelectedServiceAcccount] = React.useState(
|
|
|
|
""
|
|
|
|
);
|
2019-10-14 14:41:41 +00:00
|
|
|
const servicesChoiceList = maybe(
|
|
|
|
() =>
|
|
|
|
services.map(node => ({
|
|
|
|
label: node.name,
|
|
|
|
value: node.id
|
|
|
|
})),
|
|
|
|
[]
|
|
|
|
);
|
2019-10-09 20:50:03 +00:00
|
|
|
|
|
|
|
return (
|
2019-10-18 12:01:26 +00:00
|
|
|
<Form errors={apiErrors} initial={initialForm} onSubmit={onSubmit}>
|
2019-10-14 14:41:41 +00:00
|
|
|
{({ data, errors, hasChanged, submit, change }) => {
|
|
|
|
const handleServiceSelect = createSingleAutocompleteSelectHandler(
|
|
|
|
change,
|
2019-10-17 11:59:18 +00:00
|
|
|
setSelectedServiceAcccount,
|
2019-10-14 14:41:41 +00:00
|
|
|
servicesChoiceList
|
|
|
|
);
|
|
|
|
return (
|
|
|
|
<Container>
|
|
|
|
<AppHeader onBack={onBack}>
|
|
|
|
{intl.formatMessage(sectionNames.webhooks)}
|
|
|
|
</AppHeader>
|
|
|
|
<PageHeader
|
|
|
|
title={intl.formatMessage({
|
|
|
|
defaultMessage: "Create Webhook",
|
|
|
|
description: "header"
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
<Grid>
|
|
|
|
<div>
|
|
|
|
<WebhookInfo
|
|
|
|
data={data}
|
|
|
|
disabled={disabled}
|
2019-10-17 11:59:18 +00:00
|
|
|
serviceDisplayValue={selectedServiceAcccount}
|
2019-10-14 14:41:41 +00:00
|
|
|
services={servicesChoiceList}
|
2019-10-17 11:59:18 +00:00
|
|
|
fetchServiceAccounts={fetchServiceAccounts}
|
2019-10-18 12:01:26 +00:00
|
|
|
apiErrors={apiErrors}
|
2019-10-14 14:41:41 +00:00
|
|
|
errors={errors}
|
|
|
|
serviceOnChange={handleServiceSelect}
|
|
|
|
onChange={change}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<WebhookEvents
|
|
|
|
data={data}
|
|
|
|
disabled={disabled}
|
|
|
|
onChange={change}
|
|
|
|
/>
|
|
|
|
<FormSpacer />
|
|
|
|
<WebhookStatus
|
|
|
|
data={data.isActive}
|
|
|
|
disabled={disabled}
|
|
|
|
onChange={change}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Grid>
|
|
|
|
<SaveButtonBar
|
|
|
|
disabled={disabled || !hasChanged}
|
|
|
|
state={saveButtonBarState}
|
|
|
|
onCancel={onBack}
|
|
|
|
onSave={submit}
|
|
|
|
/>
|
|
|
|
</Container>
|
|
|
|
);
|
|
|
|
}}
|
2019-10-09 20:50:03 +00:00
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
WebhookCreatePage.displayName = "WebhookCreatePage";
|
|
|
|
export default WebhookCreatePage;
|