import { FC, useState } from "react"; import Link from "next/link"; import Image from "next/image"; import { useRouter } from "next/router"; import { mutate } from "swr"; // react-hook-form import { useForm } from "react-hook-form"; // services import GithubIntegrationService from "services/integration/github.service"; // hooks import useToast from "hooks/use-toast"; // components import { GithubImportConfigure, GithubImportData, GithubRepoDetails, GithubImportUsers, GithubImportConfirm, } from "components/integration"; // icons import { CogIcon, CloudUploadIcon, UsersIcon, CheckIcon } from "components/icons"; import { ArrowLeftIcon, ListBulletIcon } from "@heroicons/react/24/outline"; // images import GithubLogo from "public/logos/github-square.png"; // types import { IAppIntegrations, IGithubRepoCollaborator, IGithubServiceImportFormData, IWorkspaceIntegrations, } from "types"; // fetch-keys import { IMPORTER_SERVICES_LIST } from "constants/fetch-keys"; type Props = { provider: string | undefined; appIntegrations: IAppIntegrations[] | undefined; workspaceIntegrations: IWorkspaceIntegrations[] | undefined; }; export type TIntegrationSteps = | "import-configure" | "import-data" | "repo-details" | "import-users" | "import-confirm"; export interface IIntegrationData { state: TIntegrationSteps; } export interface IUserDetails { username: string; import: any; email: string; } export type TFormValues = { github: any; project: string | null; sync: boolean; collaborators: IGithubRepoCollaborator[]; users: IUserDetails[]; }; const defaultFormValues = { github: null, project: null, sync: false, }; const integrationWorkflowData = [ { title: "Configure", key: "import-configure", icon: CogIcon, }, { title: "Import Data", key: "import-data", icon: CloudUploadIcon, }, { title: "Issues", key: "repo-details", icon: ListBulletIcon }, { title: "Users", key: "import-users", icon: UsersIcon, }, { title: "Confirm", key: "import-confirm", icon: CheckIcon, }, ]; export const GithubIntegrationRoot: FC = ({ provider, appIntegrations, workspaceIntegrations, }) => { const [currentStep, setCurrentStep] = useState({ state: "import-configure", }); const [users, setUsers] = useState([]); const router = useRouter(); const { workspaceSlug } = router.query; const { setToastAlert } = useToast(); const { handleSubmit, control, setValue, watch } = useForm({ defaultValues: defaultFormValues, }); const activeIntegrationState = () => { const currentElementIndex = integrationWorkflowData.findIndex( (i) => i?.key === currentStep?.state ); return currentElementIndex; }; const handleStepChange = (value: TIntegrationSteps) => { setCurrentStep((prevData) => ({ ...prevData, state: value })); }; // current integration from all the integrations available const integration = appIntegrations && appIntegrations.length > 0 && appIntegrations.find((i) => i.provider === provider); // current integration from workspace integrations const workspaceIntegration = integration && workspaceIntegrations?.find((i: any) => i.integration_detail.id === integration.id); const createGithubImporterService = async (formData: TFormValues) => { if (!formData.github || !formData.project) return; const payload: IGithubServiceImportFormData = { metadata: { owner: formData.github.owner.login, name: formData.github.name, repository_id: formData.github.id, url: formData.github.html_url, }, data: { users: users, }, config: { sync: formData.sync, }, project_id: formData.project, }; await GithubIntegrationService.createGithubServiceImport(workspaceSlug as string, payload) .then(() => { router.push(`/${workspaceSlug}/settings/import-export`); mutate(IMPORTER_SERVICES_LIST(workspaceSlug as string)); }) .catch(() => setToastAlert({ type: "error", title: "Error!", message: "Import was unsuccessful. Please try again.", }) ); }; return (
Cancel import & go back
GithubLogo
{integrationWorkflowData.map((integration, index) => ( <>
{index < integrationWorkflowData.length - 1 && (
{" "}
)} ))}
{currentStep?.state === "import-configure" && ( )} {currentStep?.state === "import-data" && ( )} {currentStep?.state === "repo-details" && ( )} {currentStep?.state === "import-users" && ( )} {currentStep?.state === "import-confirm" && ( )}
); };