Skip to content

Commit

Permalink
Lectures: Validate the date for lecture units (#9765)
Browse files Browse the repository at this point in the history
  • Loading branch information
florian-glombik authored Nov 24, 2024
1 parent fdc6755 commit bd9295c
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Component, ElementRef, OnChanges, ViewChild, computed, inject, input, output, signal } from '@angular/core';
import { Component, ElementRef, OnChanges, ViewChild, computed, inject, input, output, signal, viewChild } from '@angular/core';
import dayjs from 'dayjs/esm';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { faQuestionCircle, faTimes } from '@fortawesome/free-solid-svg-icons';
import { ACCEPTED_FILE_EXTENSIONS_FILE_BROWSER, ALLOWED_FILE_EXTENSIONS_HUMAN_READABLE } from 'app/shared/constants/file-extensions.constants';
import { CompetencyLectureUnitLink } from 'app/entities/competency.model';
import { MAX_FILE_SIZE } from 'app/shared/constants/input.constants';
import { toSignal } from '@angular/core/rxjs-interop';
import { FormDateTimePickerComponent } from 'app/shared/date-time-picker/date-time-picker.component';

export interface AttachmentUnitFormData {
formProperties: FormProperties;
Expand Down Expand Up @@ -47,6 +48,8 @@ export class AttachmentUnitFormComponent implements OnChanges {
hasCancelButton = input<boolean>(false);
onCancel = output<void>();

datePickerComponent = viewChild(FormDateTimePickerComponent);

// have to handle the file input as a special case at is not part of the reactive form
@ViewChild('fileInput', { static: false })
fileInput: ElementRef;
Expand All @@ -68,7 +71,7 @@ export class AttachmentUnitFormComponent implements OnChanges {
private readonly statusChanges = toSignal(this.form.statusChanges ?? 'INVALID');

isFormValid = computed(() => {
return (this.statusChanges() === 'VALID' || this.fileName()) && !this.isFileTooBig();
return (this.statusChanges() === 'VALID' || this.fileName()) && !this.isFileTooBig() && this.datePickerComponent()?.isValid();
});

ngOnChanges(): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dayjs from 'dayjs/esm';
import { Component, OnChanges, computed, inject, input, output } from '@angular/core';
import { Component, OnChanges, computed, inject, input, output, viewChild } from '@angular/core';
import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
import { faArrowLeft, faTimes } from '@fortawesome/free-solid-svg-icons';
import { map } from 'rxjs';
Expand All @@ -8,6 +8,7 @@ import { OnlineResourceDTO } from 'app/lecture/lecture-unit/lecture-unit-managem
import { OnlineUnitService } from 'app/lecture/lecture-unit/lecture-unit-management/onlineUnit.service';
import { CompetencyLectureUnitLink } from 'app/entities/competency.model';
import { toSignal } from '@angular/core/rxjs-interop';
import { FormDateTimePickerComponent } from 'app/shared/date-time-picker/date-time-picker.component';

export interface OnlineUnitFormData {
name?: string;
Expand Down Expand Up @@ -45,6 +46,8 @@ export class OnlineUnitFormComponent implements OnChanges {
hasCancelButton = input<boolean>(false);
onCancel = output<void>();

datePickerComponent = viewChild(FormDateTimePickerComponent);

urlValidator = urlValidator;

private readonly formBuilder = inject(FormBuilder);
Expand All @@ -59,7 +62,7 @@ export class OnlineUnitFormComponent implements OnChanges {
});

private readonly statusChanges = toSignal(this.form.statusChanges ?? 'INVALID');
isFormValid = computed(() => this.statusChanges() === 'VALID');
isFormValid = computed(() => this.statusChanges() === 'VALID' && this.datePickerComponent()?.isValid());

get nameControl() {
return this.form.get('name');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, OnChanges, OnDestroy, OnInit, computed, inject, input, output } from '@angular/core';
import { Component, OnChanges, OnDestroy, OnInit, computed, inject, input, output, viewChild } from '@angular/core';
import dayjs from 'dayjs/esm';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
Expand All @@ -8,6 +8,7 @@ import { TranslateService } from '@ngx-translate/core';
import { faTimes } from '@fortawesome/free-solid-svg-icons';
import { CompetencyLectureUnitLink } from 'app/entities/competency.model';
import { toSignal } from '@angular/core/rxjs-interop';
import { FormDateTimePickerComponent } from 'app/shared/date-time-picker/date-time-picker.component';

export interface TextUnitFormData {
name?: string;
Expand All @@ -32,6 +33,8 @@ export class TextUnitFormComponent implements OnInit, OnChanges, OnDestroy {
hasCancelButton = input<boolean>(false);
onCancel = output<void>();

datePickerComponent = viewChild(FormDateTimePickerComponent);

// not included in reactive form
content: string | undefined;
contentLoadedFromCache = false;
Expand All @@ -46,7 +49,7 @@ export class TextUnitFormComponent implements OnInit, OnChanges, OnDestroy {
});

private readonly statusChanges = toSignal(this.form.statusChanges ?? 'INVALID');
isFormValid = computed(() => this.statusChanges() === 'VALID');
isFormValid = computed(() => this.statusChanges() === 'VALID' && this.datePickerComponent()?.isValid());

private markdownChanges = new Subject<string>();
private markdownChangesSubscription: Subscription;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import dayjs from 'dayjs/esm';
import { Component, computed, effect, inject, input, output, untracked } from '@angular/core';
import { Component, computed, effect, inject, input, output, untracked, viewChild } from '@angular/core';
import { AbstractControl, FormBuilder, FormGroup, ValidationErrors, Validators } from '@angular/forms';
import urlParser from 'js-video-url-parser';
import { faArrowLeft, faTimes } from '@fortawesome/free-solid-svg-icons';
import { CompetencyLectureUnitLink } from 'app/entities/competency.model';
import { toSignal } from '@angular/core/rxjs-interop';
import { FormDateTimePickerComponent } from 'app/shared/date-time-picker/date-time-picker.component';

export interface VideoUnitFormData {
name?: string;
Expand Down Expand Up @@ -72,6 +73,8 @@ export class VideoUnitFormComponent {
hasCancelButton = input<boolean>();
onCancel = output<void>();

datePickerComponent = viewChild(FormDateTimePickerComponent);

videoSourceUrlValidator = videoSourceUrlValidator;
videoSourceTransformUrlValidator = videoSourceTransformUrlValidator;

Expand All @@ -84,7 +87,7 @@ export class VideoUnitFormComponent {
competencyLinks: [undefined as CompetencyLectureUnitLink[] | undefined],
});
private readonly statusChanges = toSignal(this.form.statusChanges ?? 'INVALID');
isFormValid = computed(() => this.statusChanges() === 'VALID');
isFormValid = computed(() => this.statusChanges() === 'VALID' && this.datePickerComponent()?.isValid());

constructor() {
effect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ import { AttachmentUnitFormComponent, AttachmentUnitFormData } from 'app/lecture
import { FormDateTimePickerComponent } from 'app/shared/date-time-picker/date-time-picker.component';
import { ArtemisTranslatePipe } from 'app/shared/pipes/artemis-translate.pipe';
import dayjs from 'dayjs/esm';
import { MockComponent, MockDirective, MockPipe } from 'ng-mocks';
import { MockComponent, MockDirective, MockModule, MockPipe } from 'ng-mocks';
import { NgbTooltip } from '@ng-bootstrap/ng-bootstrap';
import { CompetencySelectionComponent } from 'app/shared/competency-selection/competency-selection.component';
import { MAX_FILE_SIZE } from 'app/shared/constants/input.constants';
import { OwlDateTimeModule, OwlNativeDateTimeModule } from '@danielmoncada/angular-datetime-picker';

describe('AttachmentUnitFormComponent', () => {
let attachmentUnitFormComponentFixture: ComponentFixture<AttachmentUnitFormComponent>;
let attachmentUnitFormComponent: AttachmentUnitFormComponent;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [ReactiveFormsModule, FormsModule, MockDirective(NgbTooltip)],
imports: [ReactiveFormsModule, FormsModule, MockDirective(NgbTooltip), MockModule(OwlDateTimeModule), MockModule(OwlNativeDateTimeModule)],
declarations: [
AttachmentUnitFormComponent,
FormDateTimePickerComponent,
MockPipe(ArtemisTranslatePipe),
MockComponent(FormDateTimePickerComponent),
MockComponent(FaIconComponent),
MockComponent(CompetencySelectionComponent),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,29 @@ import { OnlineUnitFormComponent, OnlineUnitFormData } from 'app/lecture/lecture
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { ArtemisTranslatePipe } from 'app/shared/pipes/artemis-translate.pipe';
import { MockComponent, MockPipe, MockProvider } from 'ng-mocks';
import { MockComponent, MockModule, MockPipe, MockProvider } from 'ng-mocks';
import { FormDateTimePickerComponent } from 'app/shared/date-time-picker/date-time-picker.component';
import { FaIconComponent } from '@fortawesome/angular-fontawesome';
import { OnlineUnitService } from 'app/lecture/lecture-unit/lecture-unit-management/onlineUnit.service';
import { OnlineResourceDTO } from 'app/lecture/lecture-unit/lecture-unit-management/online-resource-dto.model';
import { HttpResponse } from '@angular/common/http';
import { of } from 'rxjs';
import { CompetencySelectionComponent } from 'app/shared/competency-selection/competency-selection.component';
import { OwlDateTimeModule, OwlNativeDateTimeModule } from '@danielmoncada/angular-datetime-picker';
import { ArtemisTestModule } from '../../../test.module';
import { NgbTooltipModule } from '@ng-bootstrap/ng-bootstrap';

describe('OnlineUnitFormComponent', () => {
let onlineUnitFormComponentFixture: ComponentFixture<OnlineUnitFormComponent>;
let onlineUnitFormComponent: OnlineUnitFormComponent;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [ReactiveFormsModule, FormsModule],
imports: [ArtemisTestModule, ReactiveFormsModule, FormsModule, MockModule(NgbTooltipModule), MockModule(OwlDateTimeModule), MockModule(OwlNativeDateTimeModule)],
declarations: [
OnlineUnitFormComponent,
FormDateTimePickerComponent,
MockPipe(ArtemisTranslatePipe),
MockComponent(FormDateTimePickerComponent),
MockComponent(FaIconComponent),
MockComponent(CompetencySelectionComponent),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import { TextUnitFormComponent, TextUnitFormData } from 'app/lecture/lecture-uni
import { FormDateTimePickerComponent } from 'app/shared/date-time-picker/date-time-picker.component';
import { ArtemisTranslatePipe } from 'app/shared/pipes/artemis-translate.pipe';
import dayjs from 'dayjs/esm';
import { MockComponent, MockPipe, MockProvider } from 'ng-mocks';
import { MockComponent, MockModule, MockPipe, MockProvider } from 'ng-mocks';
import { MockRouter } from '../../../helpers/mocks/mock-router';
import { CompetencySelectionComponent } from 'app/shared/competency-selection/competency-selection.component';
import { ArtemisTestModule } from '../../../test.module';
import { NgbTooltipModule } from '@ng-bootstrap/ng-bootstrap';
import { OwlDateTimeModule, OwlNativeDateTimeModule } from '@danielmoncada/angular-datetime-picker';

@Component({ selector: 'jhi-markdown-editor-monaco', template: '' })
class MarkdownEditorStubComponent {
Expand Down Expand Up @@ -43,11 +46,11 @@ describe('TextUnitFormComponent', () => {
});

TestBed.configureTestingModule({
imports: [ReactiveFormsModule, FormsModule],
imports: [ArtemisTestModule, ReactiveFormsModule, FormsModule, MockModule(NgbTooltipModule), MockModule(OwlDateTimeModule), MockModule(OwlNativeDateTimeModule)],
declarations: [
TextUnitFormComponent,
MarkdownEditorStubComponent,
MockComponent(FormDateTimePickerComponent),
FormDateTimePickerComponent,
MockPipe(ArtemisTranslatePipe),
MockComponent(CompetencySelectionComponent),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { VideoUnitFormComponent, VideoUnitFormData } from 'app/lecture/lecture-u
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { ArtemisTranslatePipe } from 'app/shared/pipes/artemis-translate.pipe';
import { MockComponent, MockPipe } from 'ng-mocks';
import { MockComponent, MockModule, MockPipe } from 'ng-mocks';
import { FormDateTimePickerComponent } from 'app/shared/date-time-picker/date-time-picker.component';
import { FaIconComponent } from '@fortawesome/angular-fontawesome';
import { CompetencySelectionComponent } from 'app/shared/competency-selection/competency-selection.component';
import { OwlDateTimeModule, OwlNativeDateTimeModule } from '@danielmoncada/angular-datetime-picker';
import { ArtemisTestModule } from '../../../test.module';
import { NgbTooltipModule } from '@ng-bootstrap/ng-bootstrap';

describe('VideoUnitFormComponent', () => {
const validYouTubeUrl = 'https://www.youtube.com/watch?v=8iU8LPEa4o0';
Expand All @@ -16,11 +19,11 @@ describe('VideoUnitFormComponent', () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [ReactiveFormsModule, FormsModule],
imports: [ArtemisTestModule, ReactiveFormsModule, FormsModule, MockModule(NgbTooltipModule), MockModule(OwlDateTimeModule), MockModule(OwlNativeDateTimeModule)],
declarations: [
VideoUnitFormComponent,
FormDateTimePickerComponent,
MockPipe(ArtemisTranslatePipe),
MockComponent(FormDateTimePickerComponent),
MockComponent(FaIconComponent),
MockComponent(CompetencySelectionComponent),
],
Expand Down

0 comments on commit bd9295c

Please sign in to comment.