2023-12-26 07:58:47 +00:00
|
|
|
import { useState } from "react";
|
2023-11-15 10:26:57 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import { observer } from "mobx-react-lite";
|
2023-11-27 11:45:48 +00:00
|
|
|
import useSWR from "swr";
|
|
|
|
// mobx store
|
|
|
|
import { useMobxStore } from "lib/mobx/store-provider";
|
|
|
|
// layouts
|
2023-11-15 10:26:57 +00:00
|
|
|
import { AppLayout } from "layouts/app-layout";
|
|
|
|
import { WorkspaceSettingLayout } from "layouts/settings-layout";
|
2023-12-26 07:58:47 +00:00
|
|
|
// hooks
|
|
|
|
import useToast from "hooks/use-toast";
|
2023-11-15 10:26:57 +00:00
|
|
|
// components
|
|
|
|
import { WorkspaceSettingHeader } from "components/headers";
|
2023-11-27 11:45:48 +00:00
|
|
|
import { DeleteWebhookModal, WebhookDeleteSection, WebhookForm } from "components/web-hooks";
|
|
|
|
// ui
|
2023-11-15 10:26:57 +00:00
|
|
|
import { Spinner } from "@plane/ui";
|
2023-11-27 11:45:48 +00:00
|
|
|
// types
|
|
|
|
import { NextPageWithLayout } from "types/app";
|
2023-12-26 07:58:47 +00:00
|
|
|
import { IWebhook } from "types";
|
2023-11-15 10:26:57 +00:00
|
|
|
|
2023-11-27 11:45:48 +00:00
|
|
|
const WebhookDetailsPage: NextPageWithLayout = observer(() => {
|
|
|
|
// states
|
|
|
|
const [deleteWebhookModal, setDeleteWebhookModal] = useState(false);
|
|
|
|
// router
|
2023-11-15 10:26:57 +00:00
|
|
|
const router = useRouter();
|
2023-12-26 07:58:47 +00:00
|
|
|
const { workspaceSlug, webhookId } = router.query;
|
2023-11-27 11:45:48 +00:00
|
|
|
// mobx store
|
|
|
|
const {
|
2023-12-26 07:58:47 +00:00
|
|
|
webhook: { currentWebhook, fetchWebhookById, updateWebhook },
|
2023-11-27 11:45:48 +00:00
|
|
|
user: { currentWorkspaceRole },
|
|
|
|
} = useMobxStore();
|
2023-12-26 07:58:47 +00:00
|
|
|
// toast
|
|
|
|
const { setToastAlert } = useToast();
|
2023-11-15 10:26:57 +00:00
|
|
|
|
2023-11-27 11:45:48 +00:00
|
|
|
const isAdmin = currentWorkspaceRole === 20;
|
|
|
|
|
|
|
|
useSWR(
|
|
|
|
workspaceSlug && webhookId && isAdmin ? `WEBHOOK_DETAILS_${workspaceSlug}_${webhookId}` : null,
|
|
|
|
workspaceSlug && webhookId && isAdmin
|
|
|
|
? () => fetchWebhookById(workspaceSlug.toString(), webhookId.toString())
|
2023-11-15 10:26:57 +00:00
|
|
|
: null
|
|
|
|
);
|
|
|
|
|
2023-12-26 07:58:47 +00:00
|
|
|
const handleUpdateWebhook = async (formData: IWebhook) => {
|
|
|
|
if (!workspaceSlug || !formData || !formData.id) return;
|
|
|
|
const payload = {
|
|
|
|
url: formData?.url,
|
|
|
|
is_active: formData?.is_active,
|
|
|
|
project: formData?.project,
|
|
|
|
cycle: formData?.cycle,
|
|
|
|
module: formData?.module,
|
|
|
|
issue: formData?.issue,
|
|
|
|
issue_comment: formData?.issue_comment,
|
|
|
|
};
|
|
|
|
await updateWebhook(workspaceSlug.toString(), formData.id, payload)
|
|
|
|
.then(() => {
|
|
|
|
setToastAlert({
|
|
|
|
type: "success",
|
|
|
|
title: "Success!",
|
|
|
|
message: "Webhook updated successfully.",
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: error?.error ?? "Something went wrong. Please try again.",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-11-27 11:45:48 +00:00
|
|
|
if (!isAdmin)
|
|
|
|
return (
|
2023-12-10 10:18:10 +00:00
|
|
|
<div className="mt-10 flex h-full w-full justify-center p-4">
|
|
|
|
<p className="text-sm text-custom-text-300">You are not authorized to access this page.</p>
|
2023-11-27 11:45:48 +00:00
|
|
|
</div>
|
|
|
|
);
|
2023-11-15 10:26:57 +00:00
|
|
|
|
2023-11-27 11:45:48 +00:00
|
|
|
if (!currentWebhook)
|
|
|
|
return (
|
2023-12-10 10:18:10 +00:00
|
|
|
<div className="grid h-full w-full place-items-center p-4">
|
2023-11-27 11:45:48 +00:00
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
);
|
2023-11-15 10:26:57 +00:00
|
|
|
|
|
|
|
return (
|
2023-11-27 11:45:48 +00:00
|
|
|
<>
|
|
|
|
<DeleteWebhookModal isOpen={deleteWebhookModal} onClose={() => setDeleteWebhookModal(false)} />
|
2023-12-10 10:18:10 +00:00
|
|
|
<div className="w-full space-y-8 overflow-y-auto py-8 pr-9">
|
2023-12-26 07:58:47 +00:00
|
|
|
<WebhookForm onSubmit={async (data) => await handleUpdateWebhook(data)} data={currentWebhook} />
|
2023-11-27 11:45:48 +00:00
|
|
|
{currentWebhook && <WebhookDeleteSection openDeleteModal={() => setDeleteWebhookModal(true)} />}
|
|
|
|
</div>
|
|
|
|
</>
|
2023-11-15 10:26:57 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-11-27 11:45:48 +00:00
|
|
|
WebhookDetailsPage.getLayout = function getLayout(page: React.ReactElement) {
|
|
|
|
return (
|
|
|
|
<AppLayout header={<WorkspaceSettingHeader title="Webhook settings" />}>
|
|
|
|
<WorkspaceSettingLayout>{page}</WorkspaceSettingLayout>
|
|
|
|
</AppLayout>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default WebhookDetailsPage;
|