Sup_File/src/app/main/main.component.ts

120 lines
3.3 KiB
TypeScript
Raw Normal View History

2018-05-07 18:30:37 +00:00
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
2018-05-31 13:25:36 +00:00
import { HttpClient, HttpHeaders } from '@angular/common/http';
2018-05-07 18:30:37 +00:00
@Component({
selector: 'app-main',
templateUrl: './main.component.html',
styleUrls: ['./main.component.scss']
})
export class MainComponent implements OnInit {
2018-05-31 13:25:36 +00:00
httpOptions: any;
allUserAppFolder: any;
allUserAppFile: any;
mainFolder: any;
currentUser: any;
2018-06-08 02:57:29 +00:00
socialIdUser: String;
2018-05-31 13:25:36 +00:00
constructor(private router: Router, private route: ActivatedRoute, private http: HttpClient) { }
2018-05-07 18:30:37 +00:00
logout() {
localStorage.removeItem('jwtToken');
2018-05-31 00:58:03 +00:00
this.router.navigate(['home']);
2018-05-07 18:30:37 +00:00
}
ngOnInit() {
2018-05-31 13:25:36 +00:00
this.httpOptions = {
2018-06-08 02:57:29 +00:00
headers: new HttpHeaders({
'Authorization': localStorage.getItem('jwtToken'),
'Access-Control-Allow-Origin' : '*',
'Access-Control-Allow-Methods' : 'GET, POST, OPTIONS, PUT, PATCH, DELETE',
'Access-Control-Allow-Headers' : 'Origin, X-Requested-With, Content-Type, Accept'
}),
2018-05-31 13:25:36 +00:00
pathFolder: ''
};
2018-06-08 02:57:29 +00:00
2018-05-31 13:25:36 +00:00
this.http.get('/api/getCurrentUser', this.httpOptions).subscribe(user => {
this.currentUser = user;
if (user) {
this.getAllUserAppFolder();
}
}, err => {
if (err.status === 401) {
2018-06-08 02:57:29 +00:00
this.router.navigate(['/']);
2018-05-31 13:25:36 +00:00
}
});
2018-06-08 02:57:29 +00:00
// this.httpOptions = {
// headers: new HttpHeaders({ 'Authorization': localStorage.getItem('jwtToken'), 'Access-Control-Allow-Origin' : '*'}),
// pathFolder: ''
// };
2018-05-31 13:25:36 +00:00
2018-05-07 18:30:37 +00:00
}
2018-06-07 06:44:15 +00:00
2018-06-25 16:44:24 +00:00
deleteUserMongo() {
this.http.post('/api/deleteUserMongo', this.currentUser).subscribe(user => {
console.log('User is delete');
this.logout();
});
}
2018-06-07 06:44:15 +00:00
getItems(path) {
var test = [];
if(this.allUserAppFile !== undefined){
test = this.allUserAppFile.filter((file) => file.path === path);
}
return test;
}
setOriginFolder(path) {
var pathChunk = path.split('/');
var origin = pathChunk.length === 1 ? pathChunk[0] : pathChunk[pathChunk.length - 2]
2018-06-08 02:57:29 +00:00
2018-06-07 06:44:15 +00:00
return origin;
}
getItemsFolder(name) {
var tempp;
var test = [];
if(this.allUserAppFolder !== undefined) {
tempp = this.allUserAppFolder;
test = tempp.filter((folder) => folder.origin === name);
2018-06-07 17:47:13 +00:00
2018-06-07 06:44:15 +00:00
}
return test;
}
2018-05-31 13:25:36 +00:00
getAllUserAppFiles() {
this.http.post('/api/getFileAppList', {owner: this.currentUser.username.toString()}).subscribe(files => {
if (files) {
this.allUserAppFile = files;
// return files;
} else {
this.allUserAppFile = [];
}
});
}
getAllUserAppFolder(){
this.http.post('/api/getFolderAppList', {owner: this.currentUser.username.toString()}).subscribe(folders => {
if (folders) {
2018-06-07 06:44:15 +00:00
2018-05-31 13:25:36 +00:00
this.allUserAppFolder = folders;
2018-06-07 06:44:15 +00:00
for (let f of this.allUserAppFolder){
f.origin = this.setOriginFolder(f.path);
2018-06-07 17:47:13 +00:00
//console.log('origin : ' + f.origin);
2018-06-07 06:44:15 +00:00
}
2018-05-31 13:25:36 +00:00
this.getAllUserAppFiles();
2018-06-07 06:44:15 +00:00
2018-05-31 13:25:36 +00:00
} else {
this.allUserAppFolder = [];
}
});
2018-06-07 06:44:15 +00:00
2018-05-31 13:25:36 +00:00
}
2018-05-07 18:30:37 +00:00
}