file-upload.service.ts 2.37 KB
Newer Older
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
1
import { isPlatformBrowser } from '@angular/common';
2 3
import { Inject, Injectable, PLATFORM_ID } from '@angular/core';
import { Router } from '@angular/router';
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
4 5
import { IDuplicateQuiz } from 'arsnova-click-v2-types/dist/common';
import { QuizEntity } from '../../../lib/entities/QuizEntity';
6
import { DbState } from '../../../lib/enums/enums';
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
7
import { IMessage } from '../../../lib/interfaces/communication/IMessage';
8
import { QuizApiService } from '../api/quiz/quiz-api.service';
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
9
import { QuizService } from '../quiz/quiz.service';
10
import { IndexedDbService } from '../storage/indexed.db.service';
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

@Injectable()
export class FileUploadService {
  private _renameFilesQueue: FormData;

  get renameFilesQueue(): FormData {
    return this._renameFilesQueue;
  }

  private _duplicateQuizzes: Array<IDuplicateQuiz>;

  get duplicateQuizzes(): Array<IDuplicateQuiz> {
    return this._duplicateQuizzes;
  }

  constructor(
    @Inject(PLATFORM_ID) private platformId: Object,
    private router: Router,
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
29
    private quizService: QuizService,
30
    private quizApiService: QuizApiService, private indexedDbService: IndexedDbService,
31
  ) {
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
32 33 34
    if (isPlatformBrowser(this.platformId)) {
      this._renameFilesQueue = new FormData();
    }
35
  }
36

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
37
  public uploadFile(formData: FormData): void {
38 39
    this._renameFilesQueue = new FormData();
    this._duplicateQuizzes = [];
40

41 42 43 44
    this.quizApiService.postQuizUpload(formData).subscribe((data: IMessage) => {
      if (data.payload.duplicateQuizzes.length) {
        this._duplicateQuizzes = data.payload.duplicateQuizzes;
        data.payload.duplicateQuizzes.forEach((duplicateQuiz: IDuplicateQuiz) => {
45
          this._renameFilesQueue.append('uploadFiles[]', <File>formData.getAll('uploadFiles[]').find((file) => {
46
            return (<File>file).name === duplicateQuiz.fileName;
47
          }), duplicateQuiz.fileName);
48 49
        });
        this.router.navigate(['/quiz', 'rename']);
50
      }
51

52 53 54 55 56
      if (data.payload.quizData.length) {
        data.payload.quizData.forEach(quizData => {
          this.quizService.quiz = new QuizEntity(quizData.quiz);
          this.quizService.persist();
          this.quizService.quiz = null;
57
        });
58 59 60 61

        if (!data.payload.duplicateQuizzes.length) {
          this.indexedDbService.stateNotifier.next(DbState.Revalidate);
        }
62
      }
63

64
    }, error => {
65
      console.log('FileUploadService: PostQuizUpload failed', error);
66
    });
67 68
  }
}