keycloak-scim/src/clients/initial-access/CreateInitialAccessToken.tsx

144 lines
4.3 KiB
TypeScript
Raw Normal View History

2021-03-04 12:41:29 +00:00
import React, { useState } from "react";
2021-03-04 08:20:37 +00:00
import { useTranslation } from "react-i18next";
import { Controller, useForm } from "react-hook-form";
import {
ActionGroup,
AlertVariant,
Button,
FormGroup,
NumberInput,
PageSection,
} from "@patternfly/react-core";
import ClientInitialAccessPresentation from "keycloak-admin/lib/defs/clientInitialAccessPresentation";
import { FormAccess } from "../../components/form-access/FormAccess";
import { ViewHeader } from "../../components/view-header/ViewHeader";
import { HelpItem } from "../../components/help-enabler/HelpItem";
import { TimeSelector } from "../../components/time-selector/TimeSelector";
import { useHistory } from "react-router-dom";
import { useRealm } from "../../context/realm-context/RealmContext";
import { useAdminClient } from "../../context/auth/AdminClient";
import { useAlerts } from "../../components/alert/Alerts";
import { AccessTokenDialog } from "./AccessTokenDialog";
export const CreateInitialAccessToken = () => {
const { t } = useTranslation("clients");
const { handleSubmit, control } = useForm();
const adminClient = useAdminClient();
const { realm } = useRealm();
const { addAlert } = useAlerts();
const history = useHistory();
const [token, setToken] = useState("");
const save = async (clientToken: ClientInitialAccessPresentation) => {
try {
const access = await adminClient.realms.createClientsInitialAccess(
{ realm },
clientToken
);
2021-03-04 12:41:29 +00:00
setToken(access.token!);
2021-03-04 08:20:37 +00:00
} catch (error) {
addAlert(t("tokenSaveError", { error }), AlertVariant.danger);
}
};
return (
<>
{token && (
2021-03-04 08:37:46 +00:00
<AccessTokenDialog
token={token}
toggleDialog={() => {
setToken("");
addAlert(t("tokenSaveSuccess"), AlertVariant.success);
2021-03-04 08:37:46 +00:00
history.push(`/${realm}/clients/initialAccessToken`);
}}
/>
2021-03-04 08:20:37 +00:00
)}
<ViewHeader
titleKey="clients:createToken"
subKey="clients-help:createToken"
/>
<PageSection variant="light">
<FormAccess
isHorizontal
role="create-client"
onSubmit={handleSubmit(save)}
>
<FormGroup
label={t("expiration")}
fieldId="expiration"
labelIcon={
<HelpItem
helpText="clients-help:expiration"
forLabel={t("expiration")}
forID="expiration"
/>
}
>
<Controller
name="expiration"
defaultValue={86400}
2021-03-04 08:20:37 +00:00
control={control}
render={({ onChange, value }) => (
2021-03-05 13:47:59 +00:00
<TimeSelector
data-testid="expiration"
value={value}
onChange={onChange}
units={["days", "hours", "minutes", "seconds"]}
2021-03-05 13:47:59 +00:00
/>
2021-03-04 08:20:37 +00:00
)}
/>
</FormGroup>
<FormGroup
label={t("count")}
fieldId="count"
labelIcon={
<HelpItem
helpText="clients-help:count"
forLabel={t("count")}
forID="count"
/>
}
>
<Controller
name="count"
defaultValue={1}
control={control}
render={({ onChange, value }) => (
<NumberInput
2021-03-05 13:47:59 +00:00
data-testid="count"
2021-03-04 08:20:37 +00:00
inputName="count"
inputAriaLabel={t("count")}
min={1}
value={value}
onPlus={() => onChange(value + 1)}
onMinus={() => onChange(value - 1)}
onChange={(event) =>
onChange(Number((event.target as HTMLInputElement).value))
}
/>
)}
/>
</FormGroup>
<ActionGroup>
2021-03-05 13:47:59 +00:00
<Button variant="primary" type="submit" data-testid="save">
2021-03-04 08:20:37 +00:00
{t("common:save")}
</Button>
<Button
2021-03-05 13:47:59 +00:00
data-testid="cancel"
2021-03-04 08:20:37 +00:00
variant="link"
onClick={() =>
history.push(`/${realm}/clients/initialAccessToken`)
}
>
{t("common:cancel")}
</Button>
</ActionGroup>
</FormAccess>
</PageSection>
</>
);
};