saleor-dashboard/src/webhooks/views/WebhooksDetails.tsx

101 lines
3.3 KiB
TypeScript
Raw Normal View History

2019-10-09 06:01:52 +00:00
import { WindowTitle } from "@saleor/components/WindowTitle";
import useNavigator from "@saleor/hooks/useNavigator";
import useNotifier from "@saleor/hooks/useNotifier";
import React from "react";
import { useIntl } from "react-intl";
import { getMutationState, maybe } from "../../misc";
import WebhooksDetailsPage from "../components/WebhooksDetailsPage";
2019-10-09 06:56:46 +00:00
import { TypedWebhookUpdate } from "../mutations";
import { TypedWebhooksDetailsQuery } from "../queries";
import { webhooksListUrl, WebhooksListUrlQueryParams } from "../urls";
2019-10-09 06:01:52 +00:00
2019-10-09 06:56:46 +00:00
export interface WebhooksDetailsProps {
2019-10-09 06:01:52 +00:00
id: string;
2019-10-09 06:56:46 +00:00
params: WebhooksListUrlQueryParams;
2019-10-09 06:01:52 +00:00
}
2019-10-09 06:56:46 +00:00
export const WebhooksDetails: React.StatelessComponent<
WebhooksDetailsProps
> = ({ id }) => {
2019-10-09 06:01:52 +00:00
const navigate = useNavigator();
const notify = useNotifier();
const intl = useIntl();
return (
2019-10-09 06:56:46 +00:00
<TypedWebhookUpdate>
{(webhookUpdate, webhookUpdateOpts) => (
<TypedWebhooksDetailsQuery variables={{ id }}>
{WebhookDetails => {
2019-10-09 06:01:52 +00:00
const formTransitionState = getMutationState(
2019-10-09 06:56:46 +00:00
webhookUpdateOpts.called,
webhookUpdateOpts.loading,
maybe(() => webhookUpdateOpts.data.webhookUpdate.errors)
2019-10-09 06:01:52 +00:00
);
const formErrors = maybe(
2019-10-09 06:56:46 +00:00
() => webhookUpdateOpts.data.webhookUpdate.errors,
2019-10-09 06:01:52 +00:00
[]
);
if (formErrors.length) {
formErrors.map(error => {
notify({
text: error.message
});
});
} else {
2019-10-09 06:56:46 +00:00
if (webhookUpdateOpts.data) {
2019-10-09 06:01:52 +00:00
notify({
text: intl.formatMessage({
defaultMessage: "Succesfully updated plugin settings",
description: "plugin success message"
})
});
}
}
return (
<>
<WindowTitle
2019-10-09 06:56:46 +00:00
title={maybe(() => WebhookDetails.data.webhook.name)}
2019-10-09 06:01:52 +00:00
/>
<WebhooksDetailsPage
2019-10-09 06:56:46 +00:00
disabled={WebhookDetails.loading}
2019-10-09 06:01:52 +00:00
errors={formErrors}
saveButtonBarState={formTransitionState}
2019-10-09 06:56:46 +00:00
webhook={maybe(() => WebhookDetails.data.webook)}
onBack={() => navigate(webhooksListUrl())}
2019-10-09 06:01:52 +00:00
onSubmit={formData => {
const configurationInput =
formData.configuration &&
formData.configuration.map(item => {
return {
name: item.name,
value: item.value.toString()
};
});
2019-10-09 06:56:46 +00:00
webhookUpdate({
2019-10-09 06:01:52 +00:00
variables: {
id,
input: {
active: formData.active,
configuration: configurationInput
? configurationInput
: null
}
}
});
}}
/>
</>
);
}}
2019-10-09 06:56:46 +00:00
</TypedWebhooksDetailsQuery>
2019-10-09 06:01:52 +00:00
)}
2019-10-09 06:56:46 +00:00
</TypedWebhookUpdate>
2019-10-09 06:01:52 +00:00
);
};
2019-10-09 06:56:46 +00:00
WebhooksDetails.displayName = "WebhooksDetails";
export default WebhooksDetails;