Hide edit button for user types with only credential fields#2004
Hide edit button for user types with only credential fields#2004ThaminduR wants to merge 1 commit intoasgardeo:mainfrom
Conversation
When a user type has only credential fields and no user attributes, the Edit button was shown but led to an empty form where clicking Save caused an error. This adds a hasEditableFields check that hides the Edit button when no non-credential fields exist. Fixes asgardeo#1959 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
📝 WalkthroughWalkthroughThe PR prevents the Edit button from displaying in the user view page when the schema contains only credential fields or is null, by adding a Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
Hides the Edit action on the user view page when the resolved user schema has no editable (non-credential) fields, preventing users from entering an empty/broken edit form state (fixes #1959).
Changes:
- Added a memoized
hasEditableFieldscheck based on the user schema contents. - Updated the Edit button render condition to require
hasEditableFields. - Updated unit tests to cover credential-only schemas, mixed schemas, and null schema cases.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
frontend/apps/thunder-console/src/features/users/pages/ViewUserPage.tsx |
Computes whether schema has editable fields and uses it to conditionally render the Edit button. |
frontend/apps/thunder-console/src/features/users/pages/__tests__/ViewUserPage.test.tsx |
Adds/updates tests for hiding/showing the Edit button based on schema contents. |
| return Object.entries(userSchema.schema).some( | ||
| ([, fieldDef]) => !((fieldDef.type === 'string' || fieldDef.type === 'number') && fieldDef.credential), | ||
| ); |
There was a problem hiding this comment.
hasEditableFields only excludes credential string/number fields. Schemas that contain only unsupported field types (e.g., type: 'object', which the API allows) will still make hasEditableFields true and show the Edit button, but the edit form will render no fields because renderSchemaField returns null for objects. Consider aligning the predicate with what the edit form can actually render (and ideally reuse the same helper predicate as the .filter(...) in edit mode to avoid drift).
| return Object.entries(userSchema.schema).some( | |
| ([, fieldDef]) => !((fieldDef.type === 'string' || fieldDef.type === 'number') && fieldDef.credential), | |
| ); | |
| return Object.entries(userSchema.schema).some(([, fieldDef]) => { | |
| const isSupportedType = | |
| fieldDef.type === 'string' || fieldDef.type === 'number' || fieldDef.type === 'boolean'; | |
| const isCredentialPrimitive = | |
| (fieldDef.type === 'string' || fieldDef.type === 'number') && fieldDef.credential; | |
| return isSupportedType && !isCredentialPrimitive; | |
| }); |
| it('hides edit button when schema has only credential fields', () => { | ||
| const credentialOnlySchema: ApiUserSchema = { | ||
| id: 'credential-only', | ||
| name: 'CredentialOnly', | ||
| schema: { | ||
| password: { | ||
| type: 'string', | ||
| required: true, | ||
| credential: true, | ||
| }, | ||
| pin: { | ||
| type: 'number', | ||
| credential: true, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| mockUseGetUserSchema.mockReturnValue({ | ||
| data: credentialOnlySchema, | ||
| isLoading: false, | ||
| error: null, | ||
| }); | ||
|
|
||
| render(<ViewUserPage />); | ||
|
|
||
| expect(screen.queryByRole('button', {name: /edit/i})).not.toBeInTheDocument(); | ||
| }); |
There was a problem hiding this comment.
The new tests cover credential-only and null schemas, but they don’t cover schemas that have no renderable editable fields (e.g., type: 'object'-only schemas, which are valid per the user schema spec). Adding a test for that case would help prevent regressions where the Edit button is shown but the edit form has no fields.
There was a problem hiding this comment.
🧹 Nitpick comments (1)
frontend/apps/thunder-console/src/features/users/pages/ViewUserPage.tsx (1)
339-342: Consider extracting the credential field filter into a shared helper.The same filter logic appears both in the
hasEditableFieldscomputation (line 107-108) and when rendering form fields (lines 339-342). Extracting this to a helper function would improve maintainability.♻️ Optional: Extract shared filter logic
// Add near top of file or in a utils module const isEditableField = (fieldDef: PropertyDefinition): boolean => !((fieldDef.type === 'string' || fieldDef.type === 'number') && fieldDef.credential);Then use in both locations:
const hasEditableFields = useMemo(() => { if (!userSchema?.schema) return false; - return Object.entries(userSchema.schema).some( - ([, fieldDef]) => !((fieldDef.type === 'string' || fieldDef.type === 'number') && fieldDef.credential), - ); + return Object.values(userSchema.schema).some(isEditableField); }, [userSchema]);{userSchema?.schema ? ( Object.entries(userSchema.schema) - .filter( - ([, fieldDef]) => - !((fieldDef.type === 'string' || fieldDef.type === 'number') && fieldDef.credential), - ) + .filter(([, fieldDef]) => isEditableField(fieldDef)) .map(([fieldName, fieldDef]) =>Also applies to: 107-108
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@frontend/apps/thunder-console/src/features/users/pages/ViewUserPage.tsx` around lines 339 - 342, Extract the repeated credential-field exclusion into a single helper (e.g., isEditableField) and use it in both the hasEditableFields computation and the form-rendering filter to avoid duplicated logic; implement a function that accepts a PropertyDefinition (the same type used by the existing filters) and returns false when (fieldDef.type === 'string' || fieldDef.type === 'number') && fieldDef.credential, then replace the inline predicate used in the hasEditableFields check and the array.filter([... , fieldDef]) call in ViewUserPage.tsx with this helper.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@frontend/apps/thunder-console/src/features/users/pages/ViewUserPage.tsx`:
- Around line 339-342: Extract the repeated credential-field exclusion into a
single helper (e.g., isEditableField) and use it in both the hasEditableFields
computation and the form-rendering filter to avoid duplicated logic; implement a
function that accepts a PropertyDefinition (the same type used by the existing
filters) and returns false when (fieldDef.type === 'string' || fieldDef.type ===
'number') && fieldDef.credential, then replace the inline predicate used in the
hasEditableFields check and the array.filter([... , fieldDef]) call in
ViewUserPage.tsx with this helper.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: db1c74a1-8daa-4375-a71d-288ace966a7b
📒 Files selected for processing (2)
frontend/apps/thunder-console/src/features/users/pages/ViewUserPage.tsxfrontend/apps/thunder-console/src/features/users/pages/__tests__/ViewUserPage.test.tsx
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2004 +/- ##
==========================================
- Coverage 89.77% 89.76% -0.01%
==========================================
Files 867 867
Lines 57471 57475 +4
==========================================
+ Hits 51592 51594 +2
- Misses 4347 4348 +1
- Partials 1532 1533 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Purpose
When a user type is created with only credential fields (no user attributes), the Edit button is displayed on the user view page, but there are no fields available to edit. Clicking the Save button in this empty edit form results in an error.
This fix hides the Edit button when the user schema contains no editable (non-credential) fields, preventing users from entering a broken edit state.
Before fix:
Approach
hasEditableFieldsmemoized value inViewUserPage.tsxthat checks whether the schema contains at least one non-credential field.!isEditModeto!isEditMode && hasEditableFields.Related Issues
Related PRs
Checklist
breaking changelabel added.Security checks
Summary by CodeRabbit
Bug Fixes
Tests