mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-05 04:26:33 +00:00
improvement: filter select for create identity role
This commit is contained in:
+25
-28
@@ -9,27 +9,24 @@ import { z } from "zod";
|
|||||||
import { createNotification } from "@app/components/notifications";
|
import { createNotification } from "@app/components/notifications";
|
||||||
import {
|
import {
|
||||||
Button,
|
Button,
|
||||||
|
FilterableSelect,
|
||||||
FormControl,
|
FormControl,
|
||||||
FormLabel,
|
FormLabel,
|
||||||
IconButton,
|
IconButton,
|
||||||
Input,
|
Input,
|
||||||
Modal,
|
Modal,
|
||||||
ModalContent,
|
ModalContent
|
||||||
Select,
|
|
||||||
SelectItem
|
|
||||||
} from "@app/components/v2";
|
} from "@app/components/v2";
|
||||||
import { useOrganization } from "@app/context";
|
import { useOrganization } from "@app/context";
|
||||||
|
import { isCustomOrgRole } from "@app/helpers/roles";
|
||||||
import { useCreateIdentity, useGetOrgRoles, useUpdateIdentity } from "@app/hooks/api";
|
import { useCreateIdentity, useGetOrgRoles, useUpdateIdentity } from "@app/hooks/api";
|
||||||
import {
|
import { useAddIdentityUniversalAuth } from "@app/hooks/api/identities";
|
||||||
// IdentityAuthMethod,
|
|
||||||
useAddIdentityUniversalAuth
|
|
||||||
} from "@app/hooks/api/identities";
|
|
||||||
import { UsePopUpState } from "@app/hooks/usePopUp";
|
import { UsePopUpState } from "@app/hooks/usePopUp";
|
||||||
|
|
||||||
const schema = z
|
const schema = z
|
||||||
.object({
|
.object({
|
||||||
name: z.string(),
|
name: z.string().min(1, "Required"),
|
||||||
role: z.string(),
|
role: z.object({ slug: z.string(), name: z.string() }),
|
||||||
metadata: z
|
metadata: z
|
||||||
.object({
|
.object({
|
||||||
key: z.string().trim().min(1),
|
key: z.string().trim().min(1),
|
||||||
@@ -101,13 +98,15 @@ export const IdentityModal = ({ popUp, handlePopUpToggle }: Props) => {
|
|||||||
if (identity) {
|
if (identity) {
|
||||||
reset({
|
reset({
|
||||||
name: identity.name,
|
name: identity.name,
|
||||||
role: identity?.customRole?.slug ?? identity.role,
|
role: identity?.customRole ?? roles.find((role) => role.slug === identity.role),
|
||||||
metadata: identity.metadata
|
metadata: identity.metadata
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
reset({
|
reset({
|
||||||
name: "",
|
name: "",
|
||||||
role: roles[0].slug
|
role: isCustomOrgRole(currentOrg?.defaultMembershipRole!)
|
||||||
|
? roles?.find((role) => role.id === currentOrg?.defaultMembershipRole)
|
||||||
|
: roles?.find((role) => role.slug === currentOrg?.defaultMembershipRole)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, [popUp?.identity?.data, roles]);
|
}, [popUp?.identity?.data, roles]);
|
||||||
@@ -126,7 +125,7 @@ export const IdentityModal = ({ popUp, handlePopUpToggle }: Props) => {
|
|||||||
await updateMutateAsync({
|
await updateMutateAsync({
|
||||||
identityId: identity.identityId,
|
identityId: identity.identityId,
|
||||||
name,
|
name,
|
||||||
role: role || undefined,
|
role: role.slug || undefined,
|
||||||
organizationId: orgId,
|
organizationId: orgId,
|
||||||
metadata
|
metadata
|
||||||
});
|
});
|
||||||
@@ -137,7 +136,7 @@ export const IdentityModal = ({ popUp, handlePopUpToggle }: Props) => {
|
|||||||
|
|
||||||
const { id: createdId } = await createMutateAsync({
|
const { id: createdId } = await createMutateAsync({
|
||||||
name,
|
name,
|
||||||
role: role || undefined,
|
role: role.slug || undefined,
|
||||||
organizationId: orgId,
|
organizationId: orgId,
|
||||||
metadata
|
metadata
|
||||||
});
|
});
|
||||||
@@ -184,7 +183,10 @@ export const IdentityModal = ({ popUp, handlePopUpToggle }: Props) => {
|
|||||||
reset();
|
reset();
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<ModalContent title={`${popUp?.identity?.data ? "Update" : "Create"} Identity`}>
|
<ModalContent
|
||||||
|
bodyClassName="overflow-visible"
|
||||||
|
title={`${popUp?.identity?.data ? "Update" : "Create"} Identity`}
|
||||||
|
>
|
||||||
<form onSubmit={handleSubmit(onFormSubmit)}>
|
<form onSubmit={handleSubmit(onFormSubmit)}>
|
||||||
<Controller
|
<Controller
|
||||||
control={control}
|
control={control}
|
||||||
@@ -199,26 +201,21 @@ export const IdentityModal = ({ popUp, handlePopUpToggle }: Props) => {
|
|||||||
<Controller
|
<Controller
|
||||||
control={control}
|
control={control}
|
||||||
name="role"
|
name="role"
|
||||||
defaultValue=""
|
render={({ field: { onChange, value }, fieldState: { error } }) => (
|
||||||
render={({ field: { onChange, ...field }, fieldState: { error } }) => (
|
|
||||||
<FormControl
|
<FormControl
|
||||||
label={`${popUp?.identity?.data ? "Update" : ""} Role`}
|
label={`${popUp?.identity?.data ? "Update" : ""} Role`}
|
||||||
errorText={error?.message}
|
errorText={error?.message}
|
||||||
isError={Boolean(error)}
|
isError={Boolean(error)}
|
||||||
className="mt-4"
|
className="mt-4"
|
||||||
>
|
>
|
||||||
<Select
|
<FilterableSelect
|
||||||
defaultValue={field.value}
|
placeholder="Select role..."
|
||||||
{...field}
|
options={roles}
|
||||||
onValueChange={(e) => onChange(e)}
|
onChange={onChange}
|
||||||
className="w-full"
|
value={value}
|
||||||
>
|
getOptionValue={(option) => option.slug}
|
||||||
{(roles || []).map(({ name, slug }) => (
|
getOptionLabel={(option) => option.name}
|
||||||
<SelectItem value={slug} key={`st-role-${slug}`}>
|
/>
|
||||||
{name}
|
|
||||||
</SelectItem>
|
|
||||||
))}
|
|
||||||
</Select>
|
|
||||||
</FormControl>
|
</FormControl>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
|||||||
Reference in New Issue
Block a user