2021-08-10 11:49:08 +00:00
|
|
|
import React, { useState } from "react";
|
|
|
|
import { Control, Controller, FieldValues } from "react-hook-form";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import {
|
|
|
|
FormGroup,
|
|
|
|
Select,
|
|
|
|
SelectOption,
|
|
|
|
SelectVariant,
|
|
|
|
Split,
|
|
|
|
SplitItem,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
|
|
|
|
import {
|
|
|
|
TimeSelector,
|
|
|
|
Unit,
|
|
|
|
} from "../../components/time-selector/TimeSelector";
|
|
|
|
import { HelpItem } from "../../components/help-enabler/HelpItem";
|
|
|
|
|
|
|
|
type TokenLifespanProps = {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
defaultValue: string;
|
|
|
|
control: Control<FieldValues>;
|
|
|
|
units?: Unit[];
|
|
|
|
};
|
|
|
|
|
|
|
|
const never = "tokenLifespan.never";
|
|
|
|
const expires = "tokenLifespan.expires";
|
|
|
|
|
|
|
|
export const TokenLifespan = ({
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
defaultValue,
|
|
|
|
control,
|
|
|
|
units,
|
|
|
|
}: TokenLifespanProps) => {
|
|
|
|
const { t } = useTranslation("clients");
|
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
|
2021-08-16 09:40:14 +00:00
|
|
|
const [focused, setFocused] = useState(false);
|
|
|
|
const onFocus = () => setFocused(true);
|
|
|
|
const onBlur = () => setFocused(false);
|
|
|
|
|
|
|
|
const isExpireSet = (value: string | number) =>
|
2022-02-16 18:08:23 +00:00
|
|
|
(typeof value === "number" && value !== -1) ||
|
|
|
|
(typeof value === "string" && value !== "" && value !== "-1") ||
|
|
|
|
focused;
|
2021-08-16 09:40:14 +00:00
|
|
|
|
2021-08-10 11:49:08 +00:00
|
|
|
return (
|
|
|
|
<FormGroup
|
|
|
|
label={t(id)}
|
|
|
|
fieldId={id}
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText={`clients-help:${id}`}
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId={`clients:${id}`}
|
2021-08-10 11:49:08 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name={name}
|
|
|
|
defaultValue={defaultValue}
|
|
|
|
control={control}
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Split hasGutter>
|
|
|
|
<SplitItem>
|
|
|
|
<Select
|
|
|
|
variant={SelectVariant.single}
|
2021-11-30 13:07:44 +00:00
|
|
|
onToggle={setOpen}
|
2021-08-10 11:49:08 +00:00
|
|
|
isOpen={open}
|
|
|
|
onSelect={(_, value) => {
|
|
|
|
onChange(value);
|
|
|
|
setOpen(false);
|
|
|
|
}}
|
2021-08-16 09:40:14 +00:00
|
|
|
selections={[isExpireSet(value) ? t(expires) : t(never)]}
|
2021-08-10 11:49:08 +00:00
|
|
|
>
|
|
|
|
<SelectOption value={-1}>{t(never)}</SelectOption>
|
|
|
|
<SelectOption value={60}>{t(expires)}</SelectOption>
|
|
|
|
</Select>
|
|
|
|
</SplitItem>
|
|
|
|
<SplitItem>
|
2021-08-16 09:40:14 +00:00
|
|
|
{isExpireSet(value) && (
|
|
|
|
<TimeSelector
|
|
|
|
units={units}
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
|
|
|
onFocus={onFocus}
|
|
|
|
onBlur={onBlur}
|
|
|
|
min={1}
|
|
|
|
/>
|
2021-08-10 11:49:08 +00:00
|
|
|
)}
|
|
|
|
</SplitItem>
|
|
|
|
</Split>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
);
|
|
|
|
};
|