refactor: improve readability of UploadVideoComponent functions and add JSDoc comments
This commit is contained in:
parent
8ee05a2148
commit
c86fc6f6c2
1 changed files with 62 additions and 27 deletions
|
|
@ -23,8 +23,9 @@ import { GenreService } from '../../../services/genre.service';
|
||||||
export class UploadVideoComponent {
|
export class UploadVideoComponent {
|
||||||
@Output() toggleUploadVideoOverview = new EventEmitter<boolean>();
|
@Output() toggleUploadVideoOverview = new EventEmitter<boolean>();
|
||||||
@Output() uploadedVideo = new EventEmitter<Video>();
|
@Output() uploadedVideo = new EventEmitter<Video>();
|
||||||
|
|
||||||
errorMsgFileSize: string | null = null;
|
errorMsgFileSize: string | null = null;
|
||||||
maxFileSizeMB = 20;
|
readonly maxFileSizeMB = 20;
|
||||||
|
|
||||||
videoData = {
|
videoData = {
|
||||||
title: '',
|
title: '',
|
||||||
|
|
@ -36,63 +37,97 @@ export class UploadVideoComponent {
|
||||||
|
|
||||||
genres$ = this.genreService.getGenres();
|
genres$ = this.genreService.getGenres();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the HomeComponent with the ErrorService, VideoService and GenreService.
|
||||||
|
*/
|
||||||
constructor(
|
constructor(
|
||||||
public errorService: ErrorService,
|
public errorService: ErrorService,
|
||||||
private videoService: VideoService,
|
private videoService: VideoService,
|
||||||
private genreService: GenreService
|
private genreService: GenreService
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
stopPropagation(event: MouseEvent) {
|
/**
|
||||||
|
* Stops event propagation for click events
|
||||||
|
* @param event MouseEvent
|
||||||
|
*/
|
||||||
|
stopPropagation(event: MouseEvent): void {
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
}
|
}
|
||||||
|
|
||||||
closeVideoUploadOverview() {
|
/**
|
||||||
|
* Close the video upload overview when the user decides
|
||||||
|
*/
|
||||||
|
closeVideoUploadOverview(): void {
|
||||||
this.toggleUploadVideoOverview.emit(false);
|
this.toggleUploadVideoOverview.emit(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
onFileChange(event: any) {
|
/**
|
||||||
this.isOneFile(event);
|
* Handles file input changes. Checks for single file selection and file size validation.
|
||||||
this.isFileSize(event);
|
* @param event File input change event
|
||||||
|
*/
|
||||||
|
onFileChange(event: Event): void {
|
||||||
|
const input = event.target as HTMLInputElement;
|
||||||
|
if (input?.files?.length) {
|
||||||
|
this.isOneFile(input);
|
||||||
|
this.isFileSize(input);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
isOneFile(event: any) {
|
/**
|
||||||
const file = event.target.files[0];
|
* Validates that only one file is selected
|
||||||
|
* @param input File input element
|
||||||
|
*/
|
||||||
|
private isOneFile(input: HTMLInputElement): void {
|
||||||
|
const file = input.files?.[0];
|
||||||
if (file) {
|
if (file) {
|
||||||
this.videoData.file = file;
|
this.videoData.file = file;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
isFileSize(event: any) {
|
|
||||||
const file = event.target.files[0];
|
/**
|
||||||
|
* Validates the size of the selected file
|
||||||
|
* @param input File input element
|
||||||
|
*/
|
||||||
|
private isFileSize(input: HTMLInputElement): void {
|
||||||
|
const file = input.files?.[0];
|
||||||
if (file) {
|
if (file) {
|
||||||
const fileSizeMB = file.size / (1024 * 1024);
|
const fileSizeMB = file.size / (1024 * 1024);
|
||||||
if (fileSizeMB > this.maxFileSizeMB) {
|
if (fileSizeMB > this.maxFileSizeMB) {
|
||||||
this.errorMsgFileSize = `The file must not be larger than ${this.maxFileSizeMB} MB.`;
|
this.errorMsgFileSize = `The file must not be larger than ${this.maxFileSizeMB} MB.`;
|
||||||
event.target.value = '';
|
input.value = '';
|
||||||
} else {
|
} else {
|
||||||
this.errorMsgFileSize = null;
|
this.errorMsgFileSize = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async onSubmit(ngForm: NgForm) {
|
/**
|
||||||
if (!ngForm.submitted || !ngForm.form.valid) return;
|
* Handles the video upload form submission.
|
||||||
|
* @param ngForm The video upload form
|
||||||
try {
|
*/
|
||||||
this.videoData.send = true;
|
async onSubmit(ngForm: NgForm): Promise<void> {
|
||||||
const formData = this.createFormData();
|
if (ngForm.submitted && ngForm.form.valid) {
|
||||||
const uploaded = await this.videoService.uploadVideo(formData);
|
try {
|
||||||
|
this.videoData.send = true;
|
||||||
this.uploadedVideo.emit(uploaded);
|
const uploadedVideo = await this.videoService.uploadVideo(
|
||||||
|
this.createFormData()
|
||||||
ngForm.resetForm();
|
);
|
||||||
this.closeVideoUploadOverview();
|
this.uploadedVideo.emit(uploadedVideo);
|
||||||
this.videoData.send = false;
|
ngForm.resetForm();
|
||||||
this.errorService.clearError();
|
this.closeVideoUploadOverview();
|
||||||
} catch (error) {
|
this.errorService.clearError();
|
||||||
this.errorService.handleError(error);
|
} catch (error) {
|
||||||
|
this.errorService.handleError(error);
|
||||||
|
} finally {
|
||||||
|
this.videoData.send = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a FormData object containing the video data.
|
||||||
|
* @returns The FormData for the video upload
|
||||||
|
*/
|
||||||
private createFormData(): FormData {
|
private createFormData(): FormData {
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append('title', this.videoData.title);
|
formData.append('title', this.videoData.title);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue