added editablity

This commit is contained in:
JakubWalkowiak 2021-01-03 23:30:50 +01:00
parent 51fded7271
commit 83d84ee5fd
11 changed files with 47 additions and 13 deletions

View File

@ -23,7 +23,8 @@
<h3>Users:</h3>
<app-group-users
[groupUsers]="group.users"
[adminId]="group.admin.id"></app-group-users>
[adminId]="group.admin.id"
(deleteUser)="onDeleteUser($event)"></app-group-users>
</div>
<div *ngIf="group.admin.id === currentUserId && group.groupCandidates" class="mb-5 border p-3">
<h3>Group candidates:</h3>

View File

@ -37,7 +37,7 @@ export class GroupEditComponent implements OnInit {
}
onLeaveGroup(): void {
this.groupService.leaveGroup(this.groupId).subscribe(() => {
this.groupService.leaveGroup(this.groupId, localStorage.getItem('userId')).subscribe(() => {
this.router.navigateByUrl('/group');
});
}
@ -55,4 +55,10 @@ export class GroupEditComponent implements OnInit {
});
}
onDeleteUser(userId: string) {
this.groupService.leaveGroup(this.groupId, userId).subscribe(() => {
this.group$ = this.groupService.getGroup(this.groupId);
});
}
}

View File

@ -28,8 +28,8 @@ export class GroupService {
return this.http.get<Group[]>(`${environment.host}${environment.apiEndpoints.groups.getCurrentUserGroups}/${localStorage.getItem('userId')}`);
}
leaveGroup(groupId: number): Observable<void> {
return this.http.get<void>(`${environment.host}${environment.apiEndpoints.groups.leave}/${groupId}/${localStorage.getItem('userId')}`);
leaveGroup(groupId: number, userId: string): Observable<void> {
return this.http.get<void>(`${environment.host}${environment.apiEndpoints.groups.leave}/${groupId}/${userId}`);
}
delete(groupId: number): Observable<void> {

View File

@ -42,7 +42,7 @@
</div>
</div>
<div class="">
<button (click)="onAddNewTest()" class="btn btn-primary">Add new test</button>
<button [disabled]="!editable" (click)="onAddNewTest()" class="btn btn-primary">Add new test</button>
</div>
<div *ngFor="let assignment of subjectForm.get('assignments')['controls']; let i = index" class="list-group list-group-flush">
<div class="list-group-item">
@ -68,7 +68,7 @@
</div>
</div>
<div class="">
<button (click)="onAddNewAssignment()" class="btn btn-primary">Add new assignment</button>
<button [disabled]="!editable" (click)="onAddNewAssignment()" class="btn btn-primary">Add new assignment</button>
</div>
<div *ngFor="let comment of subjectForm.get('comments')['controls']; let i = index" class="list-group list-group-flush">
<div class="list-group-item">
@ -82,10 +82,10 @@
</div>
</div>
<div class="">
<button (click)="onAddNewComment()" class="btn btn-primary">Add new comment</button>
<button [disabled]="!editable" (click)="onAddNewComment()" class="btn btn-primary">Add new comment</button>
</div>
<div class="mt-4">
<button (click)="onSubjectSave()" class="btn btn-primary">Save</button>
<button [disabled]="!editable" (click)="onSubjectSave()" class="btn btn-primary">Save</button>
</div>
</form>
</div>

View File

@ -1,3 +1,4 @@
import { GroupService } from './../../../group/services/group.service';
import { Assignment } from './../../interfaces/assignment.interface';
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, Validators, FormArray } from '@angular/forms';
@ -18,9 +19,11 @@ export class SubjectEditComponent implements OnInit {
subjectForm: FormGroup;
subjectId: number;
groupId: number;
editable: boolean;
constructor(private activatedRoute: ActivatedRoute,
private subjectService: SubjectService,
private groupService: GroupService,
private router: Router) { }
ngOnInit(): void {
@ -29,9 +32,13 @@ export class SubjectEditComponent implements OnInit {
this.subjectForm = this.createSubjectForm();
this.subjectId = this.activatedRoute.snapshot.params.id;
if (this.subjectId) {
this.subjectService.getById(this.subjectId).subscribe((subjectPayload) => {
this.subjectService.getById(this.subjectId, localStorage.getItem('userId')).subscribe((subjectPayload) => {
this.subject = subjectPayload;
this.updateSubjectForm(this.subject);
this.editable = this.subject.editedBy === localStorage.getItem('userId');
if (!this.editable) {
this.subjectForm.disable();
}
});
}
}
@ -45,6 +52,7 @@ export class SubjectEditComponent implements OnInit {
labTeacher: '',
mainExam: false,
examDate: new Date(),
editedBy: null,
tests: [],
assignments: [],
comments: []
@ -60,6 +68,7 @@ export class SubjectEditComponent implements OnInit {
labTeacher: new FormControl(''),
mainExam: new FormControl(false),
examDate: new FormControl(this.parseDateTimeToISOFormat(new Date())),
editedBy: new FormControl(null),
tests: new FormArray([]),
assignments: new FormArray([]),
comments: new FormArray([]),

View File

@ -23,6 +23,7 @@
</div>
<div class="subject-buttons d-flex align-items-center">
<button (click)="onEdit(subject.id)" class="btn btn-primary">Edit</button>
<button *ngIf="adminId === userId && subject.editedBy != null" (click)="onUnlock(subject.id)" class="btn btn-primary ml-2">Unlock</button>
<button *ngIf="adminId === userId" (click)="onDelete(subject.id)" class="btn btn-primary ml-2">Delete</button>
<button *ngIf="adminId !== userId" (click)="onDeleteRequest(subject.id)" class="btn btn-primary ml-2">Delete request</button>
</div>

View File

@ -15,6 +15,7 @@ export class ListComponent implements OnInit {
@Output() deleteSubject = new EventEmitter<number>();
@Output() addNewSubject = new EventEmitter<void>();
@Output() editSubject = new EventEmitter<number>();
@Output() unlockSubject = new EventEmitter<number>();
displayedColumns: string[];
constructor() { }
@ -39,4 +40,8 @@ export class ListComponent implements OnInit {
this.editSubject.emit(id);
}
onUnlock(id: number): void {
this.unlockSubject.emit(id);
}
}

View File

@ -63,7 +63,13 @@ export class SubjectMainComponent implements OnInit {
}
onEditSubject(id: number): void {
this.router.navigateByUrl(`subject/edit/${id}/${this.groupId}`);
this.router.navigateByUrl(`subject/edit/${id}/${this.userId}`);
}
onUnlockSubject(id: number): void {
this.subjectService.unlock(id).subscribe(() => {
this.subjects$ = this.subjectService.getListByGroupId(this.groupId);
});
}
}

View File

@ -10,6 +10,7 @@ export interface Subject {
labTeacher: string;
mainExam: boolean;
examDate: Date;
editedBy?: string;
tests: Test[];
assignments: Assignment[];
comments: Comment[];

View File

@ -19,8 +19,8 @@ export class SubjectService {
return this.http.delete<void>(`${environment.host + environment.apiEndpoints.subjects.delete}/${id}`);
}
getById(id: number): Observable<Subject> {
return this.http.get<Subject>(`${environment.host + environment.apiEndpoints.subjects.getById}/${id}`);
getById(id: number, userId: string): Observable<Subject> {
return this.http.get<Subject>(`${environment.host + environment.apiEndpoints.subjects.getById}/${id}/${userId}`);
}
getListByGroupId(groupId: number): Observable<Subject[]> {
@ -30,4 +30,8 @@ export class SubjectService {
update(subject: Subject): Observable<void> {
return this.http.put<void>(`${environment.host + environment.apiEndpoints.subjects.update}/${subject.id}`, subject);
}
unlock(id: number): Observable<void> {
return this.http.get<void>(`${environment.host + environment.apiEndpoints.subjects.unlock}/${id}`);
}
}

View File

@ -7,7 +7,8 @@ export const environment = {
getById: 'api/subjects/get-by-id',
delete: 'api/subjects/delete',
add: 'api/subjects/add',
update: 'api/subjects/update'
update: 'api/subjects/update',
unlock: 'api/subjects/unlock'
},
deleteRequests: {
getListByGroupId: 'api/subjectDeleteRequests/list-by-group-id',