231 lines
8.7 KiB
TypeScript
231 lines
8.7 KiB
TypeScript
import { useState } from "react";
|
|
import { Form } from "react-router";
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import type { KwpSetupData, KwpSetupFeedback, KwpSetupVM } from "@hivegrid/view-models";
|
|
|
|
import { AuthShell } from "~/components/auth/AuthShell";
|
|
import { Alert } from "~/ui/components/Alert";
|
|
import { Button } from "~/ui/components/Button";
|
|
import { ButtonGroup } from "~/ui/components/ButtonGroup";
|
|
import { EmptyState } from "~/ui/components/EmptyState";
|
|
import { LinkButton } from "~/ui/components/LinkButton";
|
|
import { RadioGroup } from "~/ui/components/RadioGroup";
|
|
import { TextInput } from "~/ui/components/TextInput";
|
|
import { kwpMandantSelectionPath, kwpSetupPath } from "~/utils/integrations/integrationsRoutePaths";
|
|
|
|
const MAX_CONTEXT_CODE_LENGTH = 128;
|
|
|
|
export type KwpSetupPageProps = {
|
|
readonly vm: KwpSetupVM;
|
|
readonly csrf: string;
|
|
readonly submitting: boolean;
|
|
};
|
|
|
|
function FeedbackAlert({ feedback }: { readonly feedback: KwpSetupFeedback }) {
|
|
const { t } = useTranslation();
|
|
if (!feedback) return null;
|
|
const key =
|
|
feedback === "configuration-error"
|
|
? "configurationError"
|
|
: feedback === "selection-error"
|
|
? "selectionError"
|
|
: feedback === "selection-expired"
|
|
? "selectionExpired"
|
|
: "error";
|
|
return (
|
|
<Alert
|
|
variant="error"
|
|
title={t(`integrations.kwp.oauth.${key}`)}
|
|
description={t(`integrations.kwp.oauth.${key}Body`)}
|
|
data-ui-id="ui.integrations.kwp.oauth.feedback"
|
|
/>
|
|
);
|
|
}
|
|
|
|
function MandantSelectionForm({ data, csrf, submitting }: {
|
|
readonly data: Extract<KwpSetupData, { readonly mode: "mandant" }>;
|
|
readonly csrf: string;
|
|
readonly submitting: boolean;
|
|
}) {
|
|
const { t } = useTranslation();
|
|
const onlyOption = data.discoveryMode === "available" && data.mandants.length === 1 ? data.mandants[0] : undefined;
|
|
const [choice, setChoice] = useState(onlyOption ? `selected:${onlyOption.code}` : "");
|
|
const [manualCode, setManualCode] = useState("");
|
|
const selectedCode = choice.startsWith("selected:") ? choice.slice("selected:".length) : choice === "manual" ? manualCode.trim() : "";
|
|
const selectionKind = choice === "none" ? "none" : "selected";
|
|
const canSubmit = choice === "none" || (choice.startsWith("selected:") && selectedCode.length > 0) || (choice === "manual" && selectedCode.length > 0);
|
|
const discovered = data.discoveryMode === "available" && data.mandants.length > 0;
|
|
|
|
return (
|
|
<Form method="post" action={kwpMandantSelectionPath(data.tenantSlug, data.connectionId)}>
|
|
<input type="hidden" name="csrf" value={csrf} readOnly />
|
|
<input type="hidden" name="tenant" value={data.tenantSlug} readOnly />
|
|
<input type="hidden" name="connectionId" value={data.connectionId} readOnly />
|
|
<input type="hidden" name="connectionVersion" value={data.connectionVersion} readOnly />
|
|
<input type="hidden" name="selectionKind" value={selectionKind} readOnly />
|
|
<input type="hidden" name="mandantCode" value={selectedCode} readOnly />
|
|
<ButtonGroup align="stack">
|
|
<Alert
|
|
variant="information"
|
|
title={t("integrations.kwp.oauth.customerCodeSummary", { customerCode: data.customerCode })}
|
|
description={t("integrations.kwp.oauth.mandantBindingBody")}
|
|
/>
|
|
{!discovered ? (
|
|
<Alert
|
|
variant="warning"
|
|
title={t("integrations.kwp.oauth.mandantManualTitle")}
|
|
description={t("integrations.kwp.oauth.mandantManualBody")}
|
|
/>
|
|
) : null}
|
|
<RadioGroup
|
|
value={choice}
|
|
onValueChange={setChoice}
|
|
label={t("integrations.kwp.oauth.mandantChoiceLabel")}
|
|
helpText={t("integrations.kwp.oauth.mandantChoiceHelp")}
|
|
data-ui-id="ui.integrations.kwp.oauth.mandants"
|
|
>
|
|
{discovered
|
|
? data.mandants.map((mandant) => (
|
|
<RadioGroup.Option
|
|
key={`${mandant.id}:${mandant.code}`}
|
|
value={`selected:${mandant.code}`}
|
|
label={`${mandant.label} (${mandant.code})`}
|
|
/>
|
|
))
|
|
: (
|
|
<>
|
|
<RadioGroup.Option value="none" label={t("integrations.kwp.oauth.noMandant")} />
|
|
<RadioGroup.Option value="manual" label={t("integrations.kwp.oauth.manualMandant")} />
|
|
</>
|
|
)}
|
|
</RadioGroup>
|
|
{!discovered && choice === "manual" ? (
|
|
<TextInput
|
|
fillContainer
|
|
showLabel
|
|
label={t("integrations.kwp.oauth.mandantCode")}
|
|
helpText={t("integrations.kwp.oauth.mandantCodeHelp")}
|
|
>
|
|
<TextInput.Input
|
|
value={manualCode}
|
|
onChange={(event) => setManualCode(event.target.value)}
|
|
maxLength={MAX_CONTEXT_CODE_LENGTH}
|
|
autoCapitalize="none"
|
|
autoCorrect="off"
|
|
required
|
|
data-ui-id="ui.integrations.kwp.oauth.mandantCode"
|
|
/>
|
|
</TextInput>
|
|
) : null}
|
|
<Button type="submit" name="intent" value="confirm-mandant" loading={submitting} disabled={submitting || !canSubmit}>
|
|
{t("integrations.kwp.oauth.confirmMandant")}
|
|
</Button>
|
|
<Button
|
|
type="submit"
|
|
name="intent"
|
|
value="cancel-mandant"
|
|
variant="tertiary"
|
|
formNoValidate
|
|
disabled={submitting}
|
|
>
|
|
{t("integrations.kwp.oauth.cancel")}
|
|
</Button>
|
|
</ButtonGroup>
|
|
</Form>
|
|
);
|
|
}
|
|
|
|
export function KwpSetupPage({ vm, csrf, submitting }: KwpSetupPageProps) {
|
|
const { t } = useTranslation();
|
|
if (vm.status !== "ready") return null;
|
|
const data = vm.data;
|
|
|
|
if (data.mode === "entry") {
|
|
if (!data.authenticated) {
|
|
return (
|
|
<AuthShell
|
|
title={t("integrations.kwp.oauth.title")}
|
|
subtitle={t("integrations.kwp.oauth.loginBody")}
|
|
dataUiId="ui.integrations.kwp.oauth.setup"
|
|
>
|
|
<FeedbackAlert feedback={data.feedback} />
|
|
<Button onClick={() => window.location.assign(data.loginHref)}>{t("integrations.kwp.oauth.login")}</Button>
|
|
</AuthShell>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<AuthShell
|
|
title={t("integrations.kwp.oauth.title")}
|
|
subtitle={t("integrations.kwp.oauth.selectTenantBody")}
|
|
dataUiId="ui.integrations.kwp.oauth.setup"
|
|
>
|
|
<FeedbackAlert feedback={data.feedback} />
|
|
{data.tenants.length > 0 ? (
|
|
<ButtonGroup align="stack" data-ui-id="ui.integrations.kwp.oauth.tenants">
|
|
{data.tenants.map((tenant) => (
|
|
<Button key={tenant.id} variant="secondary" onClick={() => window.location.assign(tenant.setupHref)}>
|
|
{tenant.name}
|
|
</Button>
|
|
))}
|
|
</ButtonGroup>
|
|
) : (
|
|
<EmptyState title={t("integrations.kwp.oauth.noTenants")} body={t("integrations.kwp.oauth.noTenantsBody")} />
|
|
)}
|
|
</AuthShell>
|
|
);
|
|
}
|
|
|
|
if (data.mode === "mandant") {
|
|
return (
|
|
<AuthShell
|
|
title={t("integrations.kwp.oauth.mandantTitle")}
|
|
subtitle={t("integrations.kwp.oauth.mandantBody")}
|
|
dataUiId="ui.integrations.kwp.oauth.mandantSetup"
|
|
titleAlign="start"
|
|
>
|
|
<FeedbackAlert feedback={data.feedback} />
|
|
<MandantSelectionForm data={data} csrf={csrf} submitting={submitting} />
|
|
</AuthShell>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<AuthShell
|
|
title={t("integrations.kwp.oauth.title")}
|
|
subtitle={t("integrations.kwp.oauth.confirmBody")}
|
|
dataUiId="ui.integrations.kwp.oauth.setup"
|
|
titleAlign="start"
|
|
>
|
|
<FeedbackAlert feedback={data.feedback} />
|
|
<Form method="post" action={kwpSetupPath(data.tenantSlug)}>
|
|
<input type="hidden" name="csrf" value={csrf} readOnly />
|
|
<input type="hidden" name="tenant" value={data.tenantSlug} readOnly />
|
|
<input type="hidden" name="intent" value="connect" readOnly />
|
|
<ButtonGroup align="stack">
|
|
<TextInput
|
|
fillContainer
|
|
showLabel
|
|
label={t("integrations.kwp.oauth.customerCode")}
|
|
helpText={t("integrations.kwp.oauth.customerCodeHelp")}
|
|
>
|
|
<TextInput.Input
|
|
name="customerCode"
|
|
maxLength={MAX_CONTEXT_CODE_LENGTH}
|
|
autoCapitalize="none"
|
|
autoCorrect="off"
|
|
required
|
|
data-ui-id="ui.integrations.kwp.oauth.customerCode"
|
|
/>
|
|
</TextInput>
|
|
<Button type="submit" loading={submitting} disabled={submitting}>
|
|
{t("integrations.kwp.oauth.continue")}
|
|
</Button>
|
|
<LinkButton onClick={() => window.location.assign(data.settingsHref)}>{t("integrations.kwp.oauth.cancel")}</LinkButton>
|
|
</ButtonGroup>
|
|
</Form>
|
|
</AuthShell>
|
|
);
|
|
}
|