Commit da460a8e authored by Hamza HRAMCHI's avatar Hamza HRAMCHI
Browse files

FIX #17436 TIME 3:30 can add groups after creation

parent 79480f37
......@@ -143,7 +143,8 @@ export class GroupComponent implements OnInit {
const modal = await this.modalController.create({
component: GroupModalComponent,
componentProps: {
groups: this.groups
groups: this.groups,
editUser: false
}
});
await modal.present();
......
<ion-header [translucent]="true">
<ion-toolbar color="primary">
<ion-title>{{'lang.groupsToManage' | translate}}</ion-title>
<ion-title>{{(editUser ? 'lang.groups' : 'lang.groupsToManage') | translate}}</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
......
......@@ -9,6 +9,7 @@ import { ModalController } from '@ionic/angular';
export class GroupModalComponent implements OnInit {
@Input() groups: any;
@Input() editUser: boolean;
constructor(
public modalController: ModalController,
......
......@@ -18,7 +18,7 @@
<ion-label>{{'lang.informations' | translate}}</ion-label>
<ion-icon name="information-circle"></ion-icon>
</ion-segment-button>
<ion-segment-button value="groups">
<ion-segment-button *ngIf="!creationMode" value="groups">
<ion-label>{{'lang.manage_groups' | translate}}</ion-label>
<ion-icon name="people-sharp"></ion-icon>
</ion-segment-button>
......@@ -118,19 +118,27 @@
<ng-container *ngIf="currentTool === 'groups'">
<ion-content>
<ion-item lines="none" style="position: sticky;top:0px;z-index:1;">
<ng-container style="display: flex;align-items: center;justify-content: center;width: 100%;background: white;">
<div style="flex: 1;text-align: right;">
<ion-button slot="end" color="primary" fille="outline" shape="round" [disabled]="userGroupsClone.length === 0" (click)="openGroupsList()">
{{'lang.add' | translate}}
</ion-button>
</div>
</ng-container>
</ion-item>
<ion-list *ngIf="user.groups.length > 0">
<ion-item *ngFor="let group of user.groups">
<ion-label>{{group.label}}</ion-label>
<ion-checkbox *ngIf="creationMode" slot="end" [checked]="group.checked" [value]="group.id" (ionChange)="group.checked = !group.checked"></ion-checkbox>
<ion-button *ngIf="!creationMode" slot="end" fill="clear" shape="round"
(click)="unlinkGroup(group.id);" [title]="'lang.unlinkGroup' | translate">
<ion-button slot="end" fill="clear" shape="round"
(click)="unlinkGroup(group);" [title]="'lang.unlinkGroup' | translate">
<ion-icon color="danger" slot="icon-only" name="close-outline"></ion-icon>
</ion-button>
</ion-item>
</ion-list>
<ion-list class="no-result" *ngIf="user.groups.length === 0">
<ion-item lines="none">
<ion-label class="no-result-label" color="medium">{{(creationMode ? 'lang.emptyGroups' : 'lang.noAssociatedGroup') | translate}}</ion-label>
<ion-label class="no-result-label" color="medium">{{(userGroupsClone.length === 0 ? 'lang.emptyGroups' : 'lang.noAssociatedGroup') | translate}}</ion-label>
</ion-item>
</ion-list>
</ion-content>
......
import { Component, OnInit } from '@angular/core';
import { Component, OnInit, ViewChild } from '@angular/core';
import { SignaturesContentService } from '../../service/signatures.service';
import { NotificationService } from '../../service/notification.service';
import { HttpClient, HttpHeaders } from '@angular/common/http';
......@@ -9,7 +9,8 @@ import { ConfirmComponent } from '../../plugins/confirm.component';
import { TranslateService } from '@ngx-translate/core';
import { AuthService } from '../../service/auth.service';
import { of } from 'rxjs';
import { AlertController } from '@ionic/angular';
import { AlertController, ModalController } from '@ionic/angular';
import { GroupModalComponent } from '../group/modal/group-modal.component';
export interface User {
......@@ -77,6 +78,9 @@ export class UserComponent implements OnInit {
errorMsg: ''
};
hasGroup: boolean = false;
userGroupsClone: any;
constructor(
public http: HttpClient,
private translate: TranslateService,
......@@ -86,52 +90,64 @@ export class UserComponent implements OnInit {
public notificationService: NotificationService,
public dialog: MatDialog,
public authService: AuthService,
public alertController: AlertController
public alertController: AlertController,
public modalController: ModalController,
) {
}
ngOnInit(): void {
async ngOnInit(): Promise<void> {
this.route.params.subscribe(async (params: any) => {
if (params['id'] === undefined) {
this.creationMode = true;
this.title = this.translate.instant('lang.userCreation');
this.user = {
id: '',
firstname: '',
lastname: '',
login: '',
email: '',
phone: '',
picture: '',
signatureModes: ['stamp'],
isRest: false,
canSendActivationNotification: false
};
await this.getManageableGroups();
this.loading = false;
await this.isCreationMode();
} else {
this.creationMode = false;
this.http.get('../rest/users/' + params['id'])
.pipe(
map((data: any) => data.user),
finalize(() => this.loading = false)
)
.subscribe({
next: data => {
this.user = data;
this.userClone = JSON.parse(JSON.stringify(this.user));
this.title = this.user.firstname + ' ' + this.user.lastname;
if (this.user.isRest) {
this.getPassRules({ detail: {
checked: true
} });
}
},
});
this.isUpdateMode(params['id']);
this.loading = false;
}
});
}
async isCreationMode() {
this.creationMode = true;
this.title = this.translate.instant('lang.userCreation');
this.user = {
id: '',
firstname: '',
lastname: '',
login: '',
email: '',
phone: '',
picture: '',
signatureModes: ['stamp'],
isRest: false,
canSendActivationNotification: false
};
await this.getManageableGroups();
this.loading = false;
}
isUpdateMode(id: any) {
this.creationMode = false;
this.http.get(`../rest/users/${id}`)
.pipe(
map((data: any) => data.user)
)
.subscribe({
next: async data => {
this.user = data;
this.hasGroup = data.groups.length > 0;
this.userGroupsClone = JSON.parse(JSON.stringify(data.groups));
await this.getManageableGroups();
this.userClone = JSON.parse(JSON.stringify(this.user));
this.title = this.user.firstname + ' ' + this.user.lastname;
if (this.user.isRest) {
this.getPassRules({ detail: {
checked: true
} });
}
},
});
}
canValidate() {
if (this.user.isRest && this.passwordRest.newPassword !== '' && (this.handlePassword.error || this.passwordRest.passwordConfirmation !== this.passwordRest.newPassword)) {
return false;
......@@ -152,7 +168,8 @@ export class UserComponent implements OnInit {
modifyUser() {
this.loading = true;
this.http.put('../rest/users/' + this.user.id, this.user)
const objToSend = {...this.user, groups: this.user['groups'].map((group: any) => group.id)};
this.http.put('../rest/users/' + this.user.id, objToSend)
.pipe(
finalize(() => this.loading = false),
tap(() => {
......@@ -162,7 +179,7 @@ export class UserComponent implements OnInit {
if (this.passwordRest.newPassword !== '') {
this.updateRestUser();
}
this.router.navigate(['/administration/users']);
this.router.navigate(['/administration/users/' + this.user.id]);
this.notificationService.success('lang.userUpdated');
}),
catchError((err: any) => {
......@@ -188,10 +205,6 @@ export class UserComponent implements OnInit {
createUser() {
this.loading = true;
this.user['groups'] = this.user['groups'].filter((group: any) => group.checked).map((item: any) => ({
id: item.id,
label: item.label
}));
this.http.post('../rest/users', this.user)
.pipe(
finalize(() => this.loading = false),
......@@ -200,7 +213,7 @@ export class UserComponent implements OnInit {
this.user.id = data.id;
this.updateRestUser();
}
this.router.navigate(['/administration/users']);
this.router.navigate([`/administration/users/${data.id}`]);
this.notificationService.success('lang.userAdded');
}),
catchError((err: any) => {
......@@ -371,9 +384,15 @@ export class UserComponent implements OnInit {
return new Promise((resolve) => {
this.http.get('../rest/manageablegroups').pipe(
tap((data: any) => {
this.user['groups'] = data.groups;
if (this.hasGroup) {
const groupIds: number[] = this.userGroupsClone.map((group: any) => group.id);
this.userGroupsClone = JSON.parse(JSON.stringify(data.groups.filter((group: any) => groupIds.indexOf(group.id) === -1)));
} else {
this.userGroupsClone = JSON.parse(JSON.stringify(data.groups));
}
resolve(true);
}),
finalize(() => this.loading = false),
catchError((err: any) => {
this.notificationService.handleErrors(err);
return of(false);
......@@ -382,7 +401,7 @@ export class UserComponent implements OnInit {
});
}
async unlinkGroup(groupId: number) {
async unlinkGroup(group: any) {
const alert = await this.alertController.create({
header: this.translate.instant('lang.confirmMsg'),
buttons: [
......@@ -395,13 +414,17 @@ export class UserComponent implements OnInit {
{
text: this.translate.instant('lang.yes'),
handler: () => {
this.http.delete(`../rest/groups/${groupId}/users/${this.user.id}`, {})
this.http.delete(`../rest/groups/${group.id}/users/${this.user.id}`, {})
.pipe(
tap(() => {
this.isUpdateMode(this.user.id);
this.userGroupsClone.push(group);
}),
finalize(() => this.loading = false)
)
.subscribe({
next: data => {
const indexToDelete = this.user['groups'].findIndex((group: any) => group.id === groupId);
const indexToDelete = this.user['groups'].findIndex((item: any) => item.id === group.id);
this.user['groups'].splice(indexToDelete, 1);
this.notificationService.success('lang.groupDeleted');
},
......@@ -415,4 +438,25 @@ export class UserComponent implements OnInit {
});
await alert.present();
}
async openGroupsList() {
const modal = await this.modalController.create({
component: GroupModalComponent,
componentProps: {
groups: this.userGroupsClone,
editUser: true
}
});
await modal.present();
const { data } = await modal.onWillDismiss();
if (data !== undefined) {
this.user['groups'] = this.user['groups'].concat(data.filter((group: any) => group.checked).map((item: any) => ({
id: item.id,
label: item.label
})));
const groupIds: number[] = this.user['groups'].map((group: any) => group.id);
this.userGroupsClone = this.userGroupsClone.filter((group: any) => groupIds.indexOf(group.id) === -1);
this.modifyUser();
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment