From 1054e69caa878acc796a743cea0c1013c69e254f Mon Sep 17 00:00:00 2001 From: Yashwin Date: Tue, 12 May 2026 17:04:39 +0530 Subject: [PATCH] refactor: make UpdateRoleRequest fields optional, disable auto-code generation in EditRoleModal, and update WorkflowDefinitionModal to use role code as value --- src/components/shared/EditRoleModal.tsx | 40 ++++++++++--------- .../shared/WorkflowDefinitionModal.tsx | 2 +- src/types/role.ts | 6 +-- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/components/shared/EditRoleModal.tsx b/src/components/shared/EditRoleModal.tsx index 4f77abd..53e93de 100644 --- a/src/components/shared/EditRoleModal.tsx +++ b/src/components/shared/EditRoleModal.tsx @@ -15,15 +15,15 @@ import type { Role, UpdateRoleRequest } from "@/types/role"; import { useAppSelector } from "@/hooks/redux-hooks"; // Utility function to generate code from name -const generateCodeFromName = (name: string): string => { - return name - .toLowerCase() - .trim() - .replace(/[^a-z0-9\s]/g, "") // Remove special characters except spaces - .replace(/\s+/g, "_") // Replace spaces with underscores - .replace(/_+/g, "_") // Replace multiple underscores with single underscore - .replace(/^_|_$/g, ""); // Remove leading/trailing underscores -}; +// const generateCodeFromName = (name: string): string => { +// return name +// .toLowerCase() +// .trim() +// .replace(/[^a-z0-9\s]/g, "") // Remove special characters except spaces +// .replace(/\s+/g, "_") // Replace spaces with underscores +// .replace(/_+/g, "_") // Replace multiple underscores with single underscore +// .replace(/^_|_$/g, ""); // Remove leading/trailing underscores +// }; // All available resources const ALL_RESOURCES = [ @@ -124,7 +124,7 @@ export const EditRoleModal = ({ register, handleSubmit, setValue, - watch, + // watch, reset, setError, clearErrors, @@ -136,15 +136,16 @@ export const EditRoleModal = ({ }, }); - const nameValue = watch("name"); + // const nameValue = watch("name"); - // Auto-generate code from name - useEffect(() => { - if (nameValue) { - const generatedCode = generateCodeFromName(nameValue); - setValue("code", generatedCode, { shouldValidate: true }); - } - }, [nameValue, setValue]); + // Auto-generate code from name - Only during creation (handled in parent or different component) + // For EditRoleModal, we want to keep the code unchanged. + // useEffect(() => { + // if (nameValue) { + // const generatedCode = generateCodeFromName(nameValue); + // setValue("code", generatedCode, { shouldValidate: true }); + // } + // }, [nameValue, setValue]); // Build available resources and actions based on user permissions const availableResourcesAndActions = useMemo(() => { @@ -352,8 +353,9 @@ export const EditRoleModal = ({ clearErrors(); try { + const { code, ...rest } = data; const submitData = { - ...data, + ...rest, // For super_admin, always include tenant_id if defaultTenantId is provided tenant_id: isSuperAdmin ? defaultTenantId || undefined diff --git a/src/components/shared/WorkflowDefinitionModal.tsx b/src/components/shared/WorkflowDefinitionModal.tsx index 0b0391f..44fd47c 100644 --- a/src/components/shared/WorkflowDefinitionModal.tsx +++ b/src/components/shared/WorkflowDefinitionModal.tsx @@ -525,7 +525,7 @@ export const WorkflowDefinitionModal = ({ if (response.success) { return { options: response.data.map((r: any) => ({ - value: r.name, + value: r.code, label: r.name, })), pagination: response.pagination, diff --git a/src/types/role.ts b/src/types/role.ts index a915f44..a8950de 100644 --- a/src/types/role.ts +++ b/src/types/role.ts @@ -55,9 +55,9 @@ export interface GetRoleResponse { } export interface UpdateRoleRequest { - name: string; - code: string; - description: string; + name?: string; + code?: string; + description?: string; tenant_id?: string | null; module_ids?: string[] | null; modules?: string[] | null;