File size: 3,582 Bytes
1bc149f
 
 
 
 
 
 
 
 
 
 
 
 
27e40c0
1bc149f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27e40c0
 
1bc149f
 
 
 
27e40c0
1bc149f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27e40c0
1bc149f
 
27e40c0
1bc149f
 
 
 
 
 
bd51bb8
 
 
 
1bc149f
 
 
 
 
 
 
 
 
 
 
27e40c0
 
 
1bc149f
27e40c0
 
 
 
 
 
 
 
 
 
 
 
1bc149f
 
 
 
 
27e40c0
1bc149f
 
 
27e40c0
 
 
 
 
 
 
1bc149f
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import {Component, OnInit} from '@angular/core';
import {MatButton} from "@angular/material/button";
import {MatCard, MatCardActions, MatCardContent} from "@angular/material/card";
import {MatError, MatFormField, MatLabel} from "@angular/material/form-field";
import {MatInput} from "@angular/material/input";
import {MatOption} from "@angular/material/autocomplete";
import {MatSelect} from "@angular/material/select";
import {AsyncPipe, NgIf} from "@angular/common";
import {FormsModule, ReactiveFormsModule} from "@angular/forms";
import {AppStateService} from "../../../state_management/services/app-state.service";
import {map, Observer} from "rxjs";
import {FormGroup, FormBuilder, Validators} from "@angular/forms";
import {error} from "@angular/compiler-cli/src/transformers/util";
import {SubmittingGuideComponent} from "./submitting-guide/submitting-guide.component";


@Component({
  selector: 'app-submitting',
  standalone: true,
  imports: [
    MatButton,
    MatCard,
    MatCardActions,
    MatCardContent,
    MatFormField,
    MatInput,
    MatLabel,
    MatOption,
    MatSelect,
    NgIf,
    ReactiveFormsModule,
    FormsModule,
    AsyncPipe,
    MatError,
    SubmittingGuideComponent
  ],
  templateUrl: './submitting.component.html',
  styleUrl: './submitting.component.css'
})
export class SubmittingComponent implements OnInit {

  tasks = this.stateService.state$.pipe(map(state => state.tasks));
  datasets = this.stateService.state$.pipe(map(state => state.datasets));

  chosenFileName = '';
  message = '';

  form: FormGroup;
  fileContent = '';

  constructor(
    private fb: FormBuilder,
    private stateService: AppStateService
  ) {
    this.form = this.fb.group({
      modelName: ['', Validators.required],
      modelLink: [''],
      task: ['', Validators.required],
      dataset: ['', Validators.required],
      file: ['', Validators.required],
      teamName: [''],
      contactEmail: ['', Validators.email],
      isPublic: [false, Validators.required]
    });
  }


  ngOnInit() {
  }

  onSubmit() {
    this.message = '';
    if (this.form.invalid) {
      this.message = 'Invalid form';
      return;
    }
    this.message = 'Submitting...';
    if (this.form.value.task === 'Summary Generation'){
      this.message = 'Summary Generation is not supported yet';
      return;
    }
    this.stateService.submit(
      this.form.value.modelName,
      this.form.value.modelLink,
      this.form.value.teamName,
      this.form.value.contactEmail,
      this.form.value.task,
      this.form.value.dataset,
      this.form.value.isPublic,
      this.fileContent
    ).subscribe(
      {
        next: (response: any) => {
          console.log(response);
          this.message = response[0].message;
        },
        error: (error: any) => {
          switch (error.status) {
            case 400:
              this.message = 'Submission rejected';
              break;
            case 500:
              this.message = 'Internal server error';
              break;
            default:
              this.message = 'An unexpected error occurred. Please try again later.';
              break;
          }
        }
      }
    );
  }

  onFileSelected(event: any) {
    const file: File = event.target.files[0];
    if (file) {
      this.chosenFileName = file.name;
      // read file as text
      const reader = new FileReader();
      reader.onload = (e: any) => {
        this.fileContent = e.target.result;
      };
      reader.readAsText(file);
    } else {
      this.chosenFileName = 'Invalid file';
    }
  }
}