-
Notifications
You must be signed in to change notification settings - Fork 291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Development
: Use directive for programming test status details and diff report
#9259
Development
: Use directive for programming test status details and diff report
#9259
Conversation
Development
: Use directive for programming test status detailsDevelopment
: Use directive for programming test status details and diff report and diff report
Development
: Use directive for programming test status details and diff report and diff reportDevelopment
: Use directive for programming test status details and diff report
…amming-test-status-details
WalkthroughThe changes introduce two new Angular components, Changes
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 10
Outside diff range comments (7)
src/main/webapp/app/shared/language/translate.directive.ts (1)
Line range hint
14-55
: Consider these minor improvements to enhance the directive.The overall implementation is solid, but here are a few suggestions to further improve the code:
- Enhance error handling in the
getTranslation
method.- Consider using Angular's
DomSanitizer
when setting innerHTML to mitigate potential XSS risks.- Add more JSDoc comments to improve code documentation.
Here's how you could implement these improvements:
- Improve error handling:
private getTranslation(): void { this.translateService .get(this.jhiTranslate, this.translateValues) .pipe(takeUntil(this.directiveDestroyed)) .subscribe({ next: (value) => { this.el.nativeElement.innerHTML = value; }, error: (error) => { console.error(`Translation error for key [${this.jhiTranslate}]:`, error); this.el.nativeElement.innerHTML = `${translationNotFoundMessage}[${this.jhiTranslate}]`; }, }); }
- Use DomSanitizer (add this import:
import { DomSanitizer } from '@angular/platform-browser';
):constructor( private el: ElementRef, private translateService: TranslateService, private sanitizer: DomSanitizer ) {} // In getTranslation method: this.el.nativeElement.innerHTML = this.sanitizer.sanitize(SecurityContext.HTML, value) || '';
- Add JSDoc comments:
/** * A directive that wraps the translate pipe from ngx-translate. * It provides a more concise way to translate content in templates. */ @Directive({ selector: '[jhiTranslate]', standalone: true, }) export class TranslateDirective implements OnChanges, OnInit, OnDestroy { /** The translation key */ @Input() jhiTranslate!: string; /** Optional values to be used in the translation */ @Input() translateValues?: { [key: string]: unknown }; // ... rest of the code ... /** * Fetches and sets the translated content. * If an error occurs during translation, it displays an error message. */ private getTranslation(): void { // ... implementation ... } }These changes will improve error handling, security, and code documentation.
src/main/webapp/app/shared/shared-common.module.ts (1)
Line range hint
18-45
: Consider converting other directives to standaloneWhile
TranslateDirective
has been converted to standalone, there are other directives in this module that could potentially be converted as well. This would further improve modularity and tree-shaking capabilities.Consider converting the following directives to standalone:
SortByDirective
SortDirective
Here's an example of how you might refactor
SortByDirective
:import { Directive, Input, ... } from '@angular/core'; @Directive({ selector: '[jhiSortBy]', standalone: true }) export class SortByDirective { // ... existing implementation }After converting these directives to standalone, update the module definition accordingly:
@NgModule({ imports: [ ArtemisSharedLibsModule, TranslateDirective, SortByDirective, SortDirective ], declarations: [ ArtemisDatePipe, ArtemisDateRangePipe, FindLanguageFromKeyPipe, AlertOverlayComponent, ArtemisTranslatePipe, ArtemisTimeAgoPipe, ArtemisDurationFromSecondsPipe, DurationPipe, CloseCircleComponent, ], // ... rest of the module definition }) export class ArtemisSharedCommonModule {}This refactoring would make your module more aligned with Angular's latest best practices.
src/main/webapp/app/detail-overview-list/exercise-detail.directive.ts (1)
Line range hint
1-78
: Consider improving future extensibility.The changes are well-implemented and maintain the existing structure and patterns. However, as the number of detail types grows, the current approach might become harder to maintain.
Consider refactoring the detailTypeToComponent mapping to use a more dynamic approach. This could involve creating a registry of detail components that can be easily extended without modifying this directive. For example:
// In a separate file, e.g., detail-component-registry.ts export const DetailComponentRegistry = new Map<DetailType, Type<any>>([ [DetailType.Text, TextDetailComponent], [DetailType.Date, DateDetailComponent], // ... other mappings ]); // In the directive import { DetailComponentRegistry } from './detail-component-registry'; // ... ngOnInit() { if (!this.isShownDetail()) { return; } this.detail = this.detail as ShownDetail; const detailComponent = DetailComponentRegistry.get(this.detail.type); if (detailComponent) { this.componentRef = this.viewContainerRef.createComponent(detailComponent); this.assignAttributes(); } }This approach would make it easier to add new detail types in the future without modifying the directive itself.
src/main/webapp/app/detail-overview-list/detail-overview-list.component.ts (1)
Line range hint
1-101
: Suggestions for code improvementsWhile the remaining code is well-structured, consider the following improvements:
- Type safety: Add explicit return types to methods, including lifecycle hooks.
- Code organization: Group related properties and methods together for better readability.
- Constant enums: Consider using const enums for
DetailType
to improve performance.- Readonly properties: Mark input properties and those that don't change after initialization as readonly.
Here's an example of how you could apply these suggestions:
import { Component, Input, OnDestroy, OnInit, ViewEncapsulation } from '@angular/core'; // ... other imports ... const enum DetailType { Link = 'detail-link', Text = 'detail-text', // ... other enum values ... } @Component({ selector: 'jhi-detail-overview-list', templateUrl: './detail-overview-list.component.html', styleUrls: ['./detail-overview-list.component.scss'], encapsulation: ViewEncapsulation.None, }) export class DetailOverviewListComponent implements OnInit, OnDestroy { // Constants and readonly properties protected readonly isEmpty = isEmpty; protected readonly DetailType = DetailType; protected readonly FeatureToggle = FeatureToggle; protected readonly ButtonSize = ButtonSize; protected readonly ProgrammingExerciseParticipationType = ProgrammingExerciseParticipationType; protected readonly TooltipPlacement = TooltipPlacement; readonly CHAT = IrisSubSettingsType.CHAT; // Inputs @Input() readonly sections!: DetailOverviewSection[]; // Component properties headlines: { id: string; translationKey: string }[] = []; headlinesRecord: Record<string, string> = {}; isLocalVC = false; private profileSubscription?: Subscription; constructor( private readonly modelingExerciseService: ModelingExerciseService, private readonly alertService: AlertService, private readonly profileService: ProfileService, ) {} ngOnInit(): void { this.initializeHeadlines(); this.subscribeToProfileInfo(); } ngOnDestroy(): void { this.profileSubscription?.unsubscribe(); } downloadApollonDiagramAsPDf(umlModel?: UMLModel, title?: string): void { // ... method implementation ... } private initializeHeadlines(): void { this.headlines = this.sections.map((section) => ({ id: section.headline.replaceAll('.', '-'), translationKey: section.headline, })); this.headlinesRecord = this.headlines.reduce((acc, curr) => ({ ...acc, [curr.translationKey]: curr.id, }), {}); } private subscribeToProfileInfo(): void { this.profileSubscription = this.profileService.getProfileInfo().subscribe((profileInfo) => { this.isLocalVC = profileInfo.activeProfiles.includes(PROFILE_LOCALVC); }); } }This refactored version improves type safety, organizes the code better, and uses const enums and readonly properties where appropriate.
src/test/javascript/spec/component/exercise-detail.directive.spec.ts (2)
Line range hint
56-107
: Consider enhancing test coverage.The test suite is well-structured and covers various detail types and components. To further improve it:
- Add test cases for error scenarios (e.g., invalid detail types).
- Consider testing the directive with different input combinations.
- Verify that the correct properties are passed to the created components.
These additions would enhance the robustness and completeness of your test suite.
Line range hint
110-127
: Enhance jest expectations for more specific assertions.While the current use of jest is good, consider the following improvements:
- Replace
expect(createComponentSpy).not.toHaveBeenCalled()
withexpect(createComponentSpy).not.toHaveBeenCalled()
for more explicit assertions.- For positive cases, use
toHaveBeenCalledExactlyOnceWith(expectedComponent)
instead oftoHaveBeenCalledWith(expectedComponent)
to ensure the component is created exactly once with the correct parameters.These changes will make your tests more precise and easier to debug if they fail.
src/main/webapp/app/detail-overview-list/detail-overview-list.component.html (1)
Line range hint
1-114
: Consider enhancing consistency and clarity in the template.
Translation approach: The template uses both
jhiTranslate
directive andartemisTranslate
pipe. Consider standardizing the translation approach for better maintainability.Component documentation: For components like
jhi-programming-exercise-instructions
andjhi-programming-exercise-lifecycle
, consider adding comments to explain their purpose and expected inputs.
isEmpty
function: The usage ofisEmpty(detail.data.umlModel?.elements)
could be replaced with a more explicit check, e.g.,detail.data.umlModel?.elements?.length > 0
, for better readability.
...list/components/programming-diff-report-detail/programming-diff-report-detail.component.html
Show resolved
Hide resolved
...w-list/components/programming-test-status-detail/programming-test-status-detail.component.ts
Outdated
Show resolved
Hide resolved
...list/components/programming-test-status-detail/programming-test-status-detail.component.html
Show resolved
Hide resolved
...list/components/programming-test-status-detail/programming-test-status-detail.component.html
Show resolved
Hide resolved
...w-list/components/programming-diff-report-detail/programming-diff-report-detail.component.ts
Outdated
Show resolved
Hide resolved
src/main/webapp/app/detail-overview-list/detail-overview-list.component.ts
Show resolved
Hide resolved
…amming-test-status-details
f43d6e5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reapprove after merge
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code reapprove
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code
…amming-test-status-details
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works on TS3 👍
…amming-test-status-details
…amming-test-status-details
…amming-test-status-details
…amming-test-status-details
…amming-test-status-details
…amming-test-status-details
Checklist
General
Client
Motivation and Context
Follow up on Development: Add directive to fix types in exercise detail overview list #8644
Description
Steps for Testing
Prerequisites:
Testserver States
Note
These badges show the state of the test servers.
Green = Currently available, Red = Currently locked
Click on the badges to get to the test servers.
Review Progress
Code Review
Manual Tests
Screenshots
The UI should not have changed
Summary by CodeRabbit
New Features
Bug Fixes
Documentation
Chores