2019-10-09 20:50:03 +00:00
|
|
|
import { WindowTitle } from "@saleor/components/WindowTitle";
|
2019-10-14 14:41:41 +00:00
|
|
|
import SearchServiceAccount from "@saleor/containers/SearchServiceAccount";
|
2019-10-09 20:50:03 +00:00
|
|
|
import useNavigator from "@saleor/hooks/useNavigator";
|
|
|
|
import useNotifier from "@saleor/hooks/useNotifier";
|
|
|
|
import { commonMessages } from "@saleor/intl";
|
2019-10-10 14:19:06 +00:00
|
|
|
import { WebhookEventTypeEnum } from "@saleor/types/globalTypes";
|
2019-10-10 11:40:31 +00:00
|
|
|
import { WebhookCreate as WebhookCreateData } from "@saleor/webhooks/types/WebhookCreate";
|
2019-10-09 20:50:03 +00:00
|
|
|
import React from "react";
|
|
|
|
import { useIntl } from "react-intl";
|
2019-10-14 14:41:41 +00:00
|
|
|
import { DEFAULT_INITIAL_SEARCH_DATA } from "../../config";
|
2019-10-09 20:50:03 +00:00
|
|
|
import { getMutationState, maybe } from "../../misc";
|
|
|
|
import WebhookCreatePage, { FormData } from "../components/WebhookCreatePage";
|
|
|
|
import { TypedWebhookCreate } from "../mutations";
|
|
|
|
import {
|
|
|
|
webhooksListUrl,
|
|
|
|
WebhooksListUrlQueryParams,
|
|
|
|
webhooksUrl
|
|
|
|
} from "../urls";
|
|
|
|
|
|
|
|
export interface WebhooksCreateProps {
|
|
|
|
id: string;
|
|
|
|
params: WebhooksListUrlQueryParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const WebhooksCreate: React.StatelessComponent<
|
|
|
|
WebhooksCreateProps
|
|
|
|
> = () => {
|
|
|
|
const navigate = useNavigator();
|
|
|
|
const notify = useNotifier();
|
|
|
|
const intl = useIntl();
|
|
|
|
|
|
|
|
const onSubmit = (data: WebhookCreateData) => {
|
|
|
|
if (data.webhookCreate.errors.length === 0) {
|
|
|
|
notify({
|
|
|
|
text: intl.formatMessage(commonMessages.savedChanges)
|
|
|
|
});
|
|
|
|
navigate(webhooksUrl(data.webhookCreate.webhook.id));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleBack = () => navigate(webhooksListUrl());
|
|
|
|
|
|
|
|
return (
|
2019-10-14 14:41:41 +00:00
|
|
|
<SearchServiceAccount variables={DEFAULT_INITIAL_SEARCH_DATA}>
|
|
|
|
{({ search: searchServiceAccount, result: searchServiceAccountOpt }) => (
|
|
|
|
<TypedWebhookCreate onCompleted={onSubmit}>
|
2019-10-17 11:59:18 +00:00
|
|
|
{(webhookCreate, webhookCreateOpts) => {
|
2019-10-14 14:41:41 +00:00
|
|
|
const handleSubmit = (data: FormData) =>
|
2019-10-17 11:59:18 +00:00
|
|
|
webhookCreate({
|
2019-10-14 14:41:41 +00:00
|
|
|
variables: {
|
|
|
|
input: {
|
|
|
|
events: data.allEvents
|
2019-10-15 10:06:19 +00:00
|
|
|
? [WebhookEventTypeEnum.ANY_EVENTS]
|
2019-10-14 14:41:41 +00:00
|
|
|
: data.events,
|
|
|
|
isActive: data.isActive,
|
|
|
|
name: data.name,
|
|
|
|
secretKey: data.secretKey,
|
|
|
|
serviceAccount: data.serviceAccount,
|
|
|
|
targetUrl: data.targetUrl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-10-09 20:50:03 +00:00
|
|
|
|
2019-10-14 14:41:41 +00:00
|
|
|
const formTransitionState = getMutationState(
|
|
|
|
webhookCreateOpts.called,
|
|
|
|
webhookCreateOpts.loading,
|
|
|
|
maybe(() => webhookCreateOpts.data.webhookCreate.errors)
|
|
|
|
);
|
2019-10-09 20:50:03 +00:00
|
|
|
|
2019-10-14 14:41:41 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<WindowTitle
|
|
|
|
title={intl.formatMessage({
|
|
|
|
defaultMessage: "Create Webhook",
|
|
|
|
description: "window title"
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
<WebhookCreatePage
|
|
|
|
disabled={false}
|
|
|
|
errors={maybe(
|
|
|
|
() => webhookCreateOpts.data.webhookCreate.errors,
|
|
|
|
[]
|
|
|
|
)}
|
2019-10-17 11:59:18 +00:00
|
|
|
fetchServiceAccounts={searchServiceAccount}
|
2019-10-14 14:41:41 +00:00
|
|
|
services={maybe(() =>
|
2019-10-17 12:38:01 +00:00
|
|
|
searchServiceAccountOpt.data.search.edges.map(
|
2019-10-14 14:41:41 +00:00
|
|
|
edge => edge.node
|
|
|
|
)
|
|
|
|
)}
|
|
|
|
onBack={handleBack}
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
saveButtonBarState={formTransitionState}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</TypedWebhookCreate>
|
|
|
|
)}
|
|
|
|
</SearchServiceAccount>
|
2019-10-09 20:50:03 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
WebhooksCreate.displayName = "WebhooksCreate";
|
|
|
|
export default WebhooksCreate;
|