Skip to content
Snippets Groups Projects
Commit fc4eb4f1 authored by Jennifer Lindgren's avatar Jennifer Lindgren
Browse files

Frontend: Only get relevant project information (dont fetch project files and...

Frontend: Only get relevant project information (dont fetch project files and messages when not neccessary).
parent c905cd18
Branches
Tags
No related merge requests found
......@@ -23,13 +23,16 @@ export class ProjectComponent implements OnInit {
) { }
async ngOnInit() {
this.projectService.initProjectService();
this.projectService.getProjectsRequest(this.signedInUser.id).then(allProjects => {
this.projectService.emitAllProjectsChange(allProjects);
this.signedInUser = this.authService.getSignedInUser();
this.authService.userSignedIn$.subscribe(user => {
this.signedInUser = user;
this.mayEditProject = this.projectService.getMayEditProject(this.signedInUser);
this.shownProject = this.projectService.getShownProject();
});
this.shownProject = this.projectService.getShownProject();
this.mayEditProject = this.projectService.getMayEditProject(this.signedInUser);
this.projectService.initProjectService();
this.projectService.shownProject$.subscribe(project => {
this.shownProject = project;
if (project !== null) {
......@@ -38,11 +41,5 @@ export class ProjectComponent implements OnInit {
this.route.navigate(['/projects']);
}
});
this.signedInUser = this.authService.getSignedInUser();
this.authService.userSignedIn$.subscribe(user => {
this.signedInUser = user;
this.mayEditProject = this.projectService.getMayEditProject(this.signedInUser);
});
}
}
......@@ -23,7 +23,7 @@ export class ProjectsComponent implements OnInit {
async ngOnInit() {
this.signedInUser = this.authService.getSignedInUser();
this.projectService.emitAllProjectsChange(
await this.projectService.getProjectsRequest(this.signedInUser.id));
await this.projectService.getProjectsRequest(this.signedInUser.id, false));
this.projectService.initProjectService();
}
......
......@@ -35,19 +35,6 @@ export class ProjectGuard implements CanActivate {
if (user === null) {
return false;
}
return this.isCreator(user, project) || this.isCollaborator(user, project);
}
isCreator(user: User, project: Project): boolean {
return user.id === project.creator.id;
}
isCollaborator(user: User, project: Project): boolean {
for (const collaborator of project.collaborators) {
if (collaborator.user.id === user.id) {
return true;
}
}
return false;
return this.projectService.isCreator(user, project) || this.projectService.isCollaborator(user, project);
}
}
......@@ -90,7 +90,7 @@ export class ProjectService {
}
async handleCollaboratorChanged(collaborator: Collaborator) {
this.getProjectRequest(collaborator.projectId).then(changedProject => {
this.getProjectRequest(collaborator.projectId).then(async changedProject => {
this.allProjects = this.allProjects.map(project => {
return project.id === changedProject.id ? changedProject : project;
});
......@@ -100,7 +100,7 @@ export class ProjectService {
this.shownProject !== null &&
this.shownProject.id === collaborator.projectId
) {
const newShownProject = this.getProject(this.shownProject.id);
const newShownProject = await this.getProjectRequest(this.shownProject.id);
const signedInUser = this.authService.getSignedInUser();
if (
collaborator.user.id === signedInUser.id &&
......@@ -161,6 +161,10 @@ export class ProjectService {
return project.archived === true;
}
isCreator(user: User, project: Project): boolean {
return user.id === project.creator.id;
}
isCollaborator(user, project: Project) {
if (project.collaborators) {
for (const collaborator of project.collaborators) {
......@@ -271,40 +275,48 @@ export class ProjectService {
this.handleCollaboratorChanged(collaborator);
}
async parseProject(project: Project) {
/**
* Parses a project by turning date strings into Date objects and fetching
* collaborator, files and messages data from the server.
* Set [fetchAll] to false to skip fetching files and messages data.
*/
async parseProject(project: Project, fetchAll: boolean = true) {
project.created = new Date(project.created);
project.edited = project.edited ? new Date(project.edited) : null;
project.collaborators = await this.getProjectCollaboratorsRequest(project.id);
project.collaborators = project.collaborators ? project.collaborators : [];
project.files = await this.getProjectFilesRequest(project.id);
project.files = project.files ? project.files : [];
project.messages = await this.getProjectMessagesRequest(project.id);
project.messages = project.messages ? project.messages.map(message => {
message.time = new Date(message.time);
return message;
}) : [];
if (fetchAll) {
project.files = await this.getProjectFilesRequest(project.id);
project.files = project.files ? project.files : [];
project.messages = await this.getProjectMessagesRequest(project.id);
project.messages = project.messages ? project.messages.map(message => {
message.time = new Date(message.time);
return message;
}) : [];
}
}
/** SERVER REQUESTS */
async getProjectsRequest(userId: number) {
async getProjectsRequest(userId: number, fetchAll: boolean = true) {
return this.http.get(this.apiUrl + 'get_projects/' + userId, this.httpOptions)
.toPromise()
.then(async response => {
const allProjects = response.body as Project[];
for (const project of allProjects) {
await this.parseProject(project);
await this.parseProject(project, fetchAll);
}
return allProjects;
});
}
async getProjectRequest(id: number) {
async getProjectRequest(id: number, fetchAll: boolean = true) {
return await this.http.get(this.apiUrl + 'get_project/' + id, this.httpOptions)
.toPromise()
.then(async response => {
const project = response.body as Project;
if ('id' in project) {
await this.parseProject(project);
await this.parseProject(project, fetchAll);
}
return project;
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment