2019-10-09 20:50:03 +00:00
|
|
|
import { WindowTitle } from "@saleor/components/WindowTitle";
|
2019-11-21 14:59:06 +00:00
|
|
|
import { DEFAULT_INITIAL_SEARCH_DATA } from "@saleor/config";
|
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-11-19 15:23:17 +00:00
|
|
|
import useServiceAccountSearch from "@saleor/searches/useServiceAccountSearch";
|
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";
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-11-07 11:34:54 +00:00
|
|
|
export const WebhooksCreate: React.FC<WebhooksCreateProps> = () => {
|
2019-10-09 20:50:03 +00:00
|
|
|
const navigate = useNavigator();
|
|
|
|
const notify = useNotifier();
|
|
|
|
const intl = useIntl();
|
2019-11-19 15:23:17 +00:00
|
|
|
const {
|
|
|
|
search: searchServiceAccount,
|
|
|
|
result: searchServiceAccountOpt
|
|
|
|
} = useServiceAccountSearch({
|
|
|
|
variables: DEFAULT_INITIAL_SEARCH_DATA
|
|
|
|
});
|
2019-10-09 20:50:03 +00:00
|
|
|
|
|
|
|
const onSubmit = (data: WebhookCreateData) => {
|
2019-10-18 12:01:26 +00:00
|
|
|
if (data.webhookCreate.webhookErrors.length === 0) {
|
2019-10-09 20:50:03 +00:00
|
|
|
notify({
|
|
|
|
text: intl.formatMessage(commonMessages.savedChanges)
|
|
|
|
});
|
|
|
|
navigate(webhooksUrl(data.webhookCreate.webhook.id));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleBack = () => navigate(webhooksListUrl());
|
|
|
|
|
|
|
|
return (
|
2019-11-19 15:23:17 +00:00
|
|
|
<TypedWebhookCreate onCompleted={onSubmit}>
|
|
|
|
{(webhookCreate, webhookCreateOpts) => {
|
|
|
|
const handleSubmit = (data: FormData) =>
|
|
|
|
webhookCreate({
|
|
|
|
variables: {
|
|
|
|
input: {
|
|
|
|
events: data.allEvents
|
|
|
|
? [WebhookEventTypeEnum.ANY_EVENTS]
|
|
|
|
: 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-11-19 15:23:17 +00:00
|
|
|
const formTransitionState = getMutationState(
|
|
|
|
webhookCreateOpts.called,
|
|
|
|
webhookCreateOpts.loading,
|
|
|
|
maybe(() => webhookCreateOpts.data.webhookCreate.webhookErrors)
|
|
|
|
);
|
2019-10-09 20:50:03 +00:00
|
|
|
|
2019-11-19 15:23:17 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<WindowTitle
|
|
|
|
title={intl.formatMessage({
|
|
|
|
defaultMessage: "Create Webhook",
|
|
|
|
description: "window title"
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
<WebhookCreatePage
|
|
|
|
disabled={false}
|
|
|
|
errors={maybe(
|
|
|
|
() => webhookCreateOpts.data.webhookCreate.webhookErrors,
|
|
|
|
[]
|
|
|
|
)}
|
|
|
|
fetchServiceAccounts={searchServiceAccount}
|
|
|
|
services={maybe(() =>
|
|
|
|
searchServiceAccountOpt.data.search.edges.map(edge => edge.node)
|
|
|
|
)}
|
|
|
|
onBack={handleBack}
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
saveButtonBarState={formTransitionState}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</TypedWebhookCreate>
|
2019-10-09 20:50:03 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
WebhooksCreate.displayName = "WebhooksCreate";
|
|
|
|
export default WebhooksCreate;
|