refactor: make UpdateRoleRequest fields optional, disable auto-code generation in EditRoleModal, and update WorkflowDefinitionModal to use role code as value

This commit is contained in:
Yashwin 2026-05-12 17:04:39 +05:30
parent eefd4f995a
commit 1054e69caa
3 changed files with 25 additions and 23 deletions

View File

@ -15,15 +15,15 @@ import type { Role, UpdateRoleRequest } from "@/types/role";
import { useAppSelector } from "@/hooks/redux-hooks"; import { useAppSelector } from "@/hooks/redux-hooks";
// Utility function to generate code from name // Utility function to generate code from name
const generateCodeFromName = (name: string): string => { // const generateCodeFromName = (name: string): string => {
return name // return name
.toLowerCase() // .toLowerCase()
.trim() // .trim()
.replace(/[^a-z0-9\s]/g, "") // Remove special characters except spaces // .replace(/[^a-z0-9\s]/g, "") // Remove special characters except spaces
.replace(/\s+/g, "_") // Replace spaces with underscores // .replace(/\s+/g, "_") // Replace spaces with underscores
.replace(/_+/g, "_") // Replace multiple underscores with single underscore // .replace(/_+/g, "_") // Replace multiple underscores with single underscore
.replace(/^_|_$/g, ""); // Remove leading/trailing underscores // .replace(/^_|_$/g, ""); // Remove leading/trailing underscores
}; // };
// All available resources // All available resources
const ALL_RESOURCES = [ const ALL_RESOURCES = [
@ -124,7 +124,7 @@ export const EditRoleModal = ({
register, register,
handleSubmit, handleSubmit,
setValue, setValue,
watch, // watch,
reset, reset,
setError, setError,
clearErrors, clearErrors,
@ -136,15 +136,16 @@ export const EditRoleModal = ({
}, },
}); });
const nameValue = watch("name"); // const nameValue = watch("name");
// Auto-generate code from name // Auto-generate code from name - Only during creation (handled in parent or different component)
useEffect(() => { // For EditRoleModal, we want to keep the code unchanged.
if (nameValue) { // useEffect(() => {
const generatedCode = generateCodeFromName(nameValue); // if (nameValue) {
setValue("code", generatedCode, { shouldValidate: true }); // const generatedCode = generateCodeFromName(nameValue);
} // setValue("code", generatedCode, { shouldValidate: true });
}, [nameValue, setValue]); // }
// }, [nameValue, setValue]);
// Build available resources and actions based on user permissions // Build available resources and actions based on user permissions
const availableResourcesAndActions = useMemo(() => { const availableResourcesAndActions = useMemo(() => {
@ -352,8 +353,9 @@ export const EditRoleModal = ({
clearErrors(); clearErrors();
try { try {
const { code, ...rest } = data;
const submitData = { const submitData = {
...data, ...rest,
// For super_admin, always include tenant_id if defaultTenantId is provided // For super_admin, always include tenant_id if defaultTenantId is provided
tenant_id: isSuperAdmin tenant_id: isSuperAdmin
? defaultTenantId || undefined ? defaultTenantId || undefined

View File

@ -525,7 +525,7 @@ export const WorkflowDefinitionModal = ({
if (response.success) { if (response.success) {
return { return {
options: response.data.map((r: any) => ({ options: response.data.map((r: any) => ({
value: r.name, value: r.code,
label: r.name, label: r.name,
})), })),
pagination: response.pagination, pagination: response.pagination,

View File

@ -55,9 +55,9 @@ export interface GetRoleResponse {
} }
export interface UpdateRoleRequest { export interface UpdateRoleRequest {
name: string; name?: string;
code: string; code?: string;
description: string; description?: string;
tenant_id?: string | null; tenant_id?: string | null;
module_ids?: string[] | null; module_ids?: string[] | null;
modules?: string[] | null; modules?: string[] | null;