-
Notifications
You must be signed in to change notification settings - Fork 2
/
create-project.component.ts
77 lines (67 loc) · 2.07 KB
/
create-project.component.ts
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
import { Component } from '@angular/core';
import { ProjectService } from '../services/project.service';
import {Router} from "@angular/router";
@Component({
selector: 'app-create-project',
templateUrl: './create-project.component.html',
styleUrl: './create-project.component.css'
})
export class CreateProjectComponent {
constructor(private projectService: ProjectService, private router: Router) { }
name: string = '';
description: string = '';
date: string = '';
category: string = '';
url: string = '';
file: File[] = [];
goal: number = 0;
currentAmount: number = 0;
categories: any = []
ngOnInit() {
this.projectService.getCategories().subscribe((data: any) => {
this.categories = data;
});
}
onFileUploaded(event: Event): void {
const input = event.target as HTMLInputElement;
if (input && input.files && input.files.length) {
for (let i = 0; i < input.files.length; i++){
this.file.push(input.files[i]);
}
}
}
createProject() {
this.projectService.createProject({
name: this.name,
description: this.description,
date: this.date,
category: this.category,
url: this.url,
goal: this.goal,
currentAmount: this.currentAmount
}).subscribe({
next: (data: any) => {
if(this.file.length > 0){
const formData = new FormData();
this.file.forEach((file) => {
formData.append('file', file, file.name);
});
console.log(formData.get('file'));
this.projectService.uploadImages(data.id, formData).subscribe({
next: () => {
console.log('Images uploaded');
this.router.navigate([`/details/${data.id}`]);
},
error: (error: any) => {
console.error('Images upload failure', error);
this.router.navigate(['/error']);
}
});}
this.router.navigate([``]);},
error: (error: any) => {
console.error('Project creation failure', error);
this.router.navigate(['/error']);
}
});
}
}