diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 54eae011826b429c49877e3ceb093d84ed45e7ce..9b9e30d8dfb4bd5da2cb07181a97530b66c25292 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,4 +1,6 @@ import { Component } from '@angular/core'; +import { ProfileService } from './profile/services/profile.service'; +import { AuthService } from './services/auth.service'; import { PrintService } from './shared/service/print.service'; @Component({ @@ -9,5 +11,13 @@ import { PrintService } from './shared/service/print.service'; export class AppComponent { title = 'pamn'; - constructor(public printService: PrintService) {} + constructor( + public printService: PrintService, + private authService: AuthService, + private profilService: ProfileService + ) { + if (this.authService.isLoggedIn()) { + this.profilService.getProfile(); + } + } } diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 1a02174b10116efa3ecd4d721e85175b3d4acd57..b412fc8acc7babcee6a1dff913ead669fb3e960e 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -43,7 +43,6 @@ import { ResetPasswordComponent } from './reset-password/reset-password.componen LegalNoticeComponent, AboutComponent, MenuPhoneComponent, - FormComponent, UserVerificationComponent, ResetEmailComponent, ResetPasswordComponent, diff --git a/src/app/form/form.component.html b/src/app/form/form.component.html index 7d4eae8956ce7bc4b18ced1eed48be9fa9242917..e662a3d76d6a81059aec8aeb5d30337f1ec6ad8a 100644 --- a/src/app/form/form.component.html +++ b/src/app/form/form.component.html @@ -35,8 +35,9 @@ </label> </div> </div> - <p>Description</p> + <p>Description*</p> <textarea rows="4" style="width: 100%" maxlength="500" formControlName="description"></textarea> + <app-validator-form [control]="getStructureControl('description')"></app-validator-form> <p> Afin de rendre visible l'offre de formation numérique proposée lors de ce nouveau confinement, merci d'indiquer ici, les activités que vous avez pu maintenir @@ -77,9 +78,6 @@ <input type="text" formControlName="contactName" /> <p>Prénom</p> <input type="text" formControlName="contactSurname" /> - <p>Courriel*</p> - <input type="email" formControlName="contactMail" /> - <app-validator-form [control]="getStructureControl('contactMail')"></app-validator-form> <p>Fonction</p> <select formControlName="fonction"> <option value="">---Sélectionner---</option> diff --git a/src/app/form/form.component.ts b/src/app/form/form.component.ts index 85485bd4aa1ac94ef055186ac04ec345fecac4ee..dedb2ae2baa74350d9c3283dfbfdb5d4f757ce80 100644 --- a/src/app/form/form.component.ts +++ b/src/app/form/form.component.ts @@ -11,6 +11,7 @@ import { EquipmentAccess } from '../shared/enum/equipmentAccess.enum'; import { WeekDayEnum } from '../shared/enum/weekDay.enum'; import { typeStructureEnum } from '../shared/enum/typeStructure.enum'; import { FonctionContactEnum } from '../shared/enum/fonctionContact.enum'; +import { User } from '../models/user.model'; @Component({ selector: 'app-structureForm', @@ -18,7 +19,8 @@ import { FonctionContactEnum } from '../shared/enum/fonctionContact.enum'; styleUrls: ['./form.component.scss'], }) export class FormComponent implements OnInit { - @Input() public idStructure: number; + @Input() public idStructure?: number; + @Input() public profile?: User; @Output() closeEvent = new EventEmitter<Structure>(); public structureForm: FormGroup; public equipmentAccess = EquipmentAccess; @@ -36,10 +38,14 @@ export class FormComponent implements OnInit { constructor(private structureService: StructureService, private searchService: SearchService) {} ngOnInit(): void { - this.structureService.getStructure(this.idStructure).subscribe((structure) => { - this.initForm(structure); - this.structureId = structure.id; - }); + if (this.idStructure) { + this.structureService.getStructure(this.idStructure).subscribe((structure) => { + this.initForm(structure); + this.structureId = structure.id; + }); + } else { + this.initForm(new Structure()); + } this.searchService.getCategoriesAccompaniment().subscribe((categories: Category[]) => { this.proceduresAccompaniment = categories[0]; }); @@ -77,10 +83,12 @@ export class FormComponent implements OnInit { private initForm(structure: Structure): void { // Init form this.structureForm = new FormGroup({ + id: new FormControl(structure.id), + coord: new FormControl(structure.coord), structureType: this.loadArrayForCheckbox(structure.structureType, true), structureName: new FormControl(structure.structureName, Validators.required), structureRepresentation: new FormControl(structure.structureRepresentation, Validators.required), - description: new FormControl(structure.description), + description: new FormControl(structure.description, Validators.required), lockdownActivity: new FormControl(structure.lockdownActivity), address: new FormGroup({ numero: new FormControl(structure.address.numero), @@ -222,12 +230,21 @@ export class FormComponent implements OnInit { } public onSubmit(structureForm: FormGroup): void { if (structureForm.valid) { - this.structureService.postStructure(this.structureId, structureForm.value).subscribe( - (structure: Structure) => { - this.closeEvent.emit(structure); - }, - (err) => {} - ); + if (this.structureId) { + this.structureService.postStructure(this.structureId, structureForm.value).subscribe( + (structure: Structure) => { + this.closeEvent.emit(structure); + }, + (err) => {} + ); + } else { + this.structureService.createStructure(structureForm.value, this.profile).subscribe( + (structure: Structure) => { + this.closeEvent.emit(structure); + }, + (err) => {} + ); + } } else { } } diff --git a/src/app/models/address.model.ts b/src/app/models/address.model.ts index 185be31968d9f84d366f051d29ff35984313f08d..d04d83abedde79b20dbf1d7ae484d34c5e4c4b57 100644 --- a/src/app/models/address.model.ts +++ b/src/app/models/address.model.ts @@ -1,5 +1,5 @@ export class Address { - numero: string; - street: string; - commune: string; + numero: string = null; + street: string = null; + commune: string = null; } diff --git a/src/app/models/day.model.ts b/src/app/models/day.model.ts index 122b812db74a4c566559fd0cadf6fb9b880790b7..005ccfa242ebb935a533cd0d2f151dded3377e1f 100644 --- a/src/app/models/day.model.ts +++ b/src/app/models/day.model.ts @@ -1,12 +1,12 @@ import { Time } from './time.model'; export class Day { - open: boolean; + open: boolean = false; time: Time[]; constructor(obj?: any) { Object.assign(this, obj, { - time: obj && obj.time ? obj.time.map((time) => new Time(time)) : null, + time: obj && obj.time ? obj.time.map((time) => new Time(time)) : [], }); } } diff --git a/src/app/models/openingDay.model.ts b/src/app/models/openingDay.model.ts index 5e286fe22705037afc1956df73c75136f1726936..71ba8d444c97b55c4393830dfa483caf5cff4845 100644 --- a/src/app/models/openingDay.model.ts +++ b/src/app/models/openingDay.model.ts @@ -1,8 +1,8 @@ export class OpeningDay { - day: string; - schedule: string; + day: string = null; + schedule: string = null; - constructor(day: string, schedule: string) { + constructor(day?: string, schedule?: string) { this.day = day; this.schedule = schedule; } diff --git a/src/app/models/structure.model.ts b/src/app/models/structure.model.ts index 7d2708fb0294682893f9b14b0d3bfb2afa9dc008..060157da0e8a2c3fd81015334e74747cc7d12943 100644 --- a/src/app/models/structure.model.ts +++ b/src/app/models/structure.model.ts @@ -1,60 +1,61 @@ +import { WebElement } from 'protractor'; import { Weekday } from '../structure-list/enum/weekday.enum'; import { Address } from './address.model'; import { Day } from './day.model'; import { OpeningDay } from './openingDay.model'; import { Week } from './week.model'; export class Structure { - public id: number; - public numero: string; - public createdAt: string; - public updatedAt: string; - public structureRepresentation: string; - public structureName: string; - public structureType: string[]; - public description: string; - public address: Address; - public contactPhone: string; - public contactMail: string; - public website: string; - public facebook: string; - public twitter: string; - public instagram: string; - public gender: string; - public contactName: string; - public contactSurname: string; - public fonction: string; - public lockdownActivity: string; - public pmrAccess: boolean; - public publicsAccompaniment: string[]; - public proceduresAccompaniment: string[]; - public accessModality: string[]; - public documentsMeeting: string; - public labelsQualifications: string[]; - public publics: string[]; - public nbComputers: number; - public nbPrinters: number; - public nbTablets: number; - public nbNumericTerminal: number; - public exceptionalClosures: string; - public equipmentsAndServices: string[]; + public id: number = null; + public numero: string = null; + public createdAt: string = null; + public updatedAt: string = null; + public structureRepresentation: string = null; + public structureName: string = null; + public structureType: string[] = []; + public description: string = null; + public address: Address = new Address(); + public contactPhone: string = null; + public contactMail: string = null; + public website: string = null; + public facebook: string = null; + public twitter: string = null; + public instagram: string = null; + public gender: string = null; + public contactName: string = null; + public contactSurname: string = null; + public fonction: string = null; + public lockdownActivity: string = null; + public pmrAccess: boolean = false; + public publicsAccompaniment: string[] = []; + public proceduresAccompaniment: string[] = []; + public accessModality: string[] = []; + public documentsMeeting: string = null; + public labelsQualifications: string[] = []; + public publics: string[] = []; + public nbComputers: number = null; + public nbPrinters: number = null; + public nbTablets: number = null; + public nbNumericTerminal: number = null; + public exceptionalClosures: string = null; + public equipmentsAndServices: string[] = []; public hours: Week; - public equipmentsDetails: string; - public equipmentsAccessType: string[]; + public equipmentsDetails: string = null; + public equipmentsAccessType: string[] = []; - public isOpen: boolean; - public openedOn: OpeningDay; - public baseSkills: string[]; - public accessRight: string[]; - public parentingHelp: string[]; - public socialAndProfessional: string[]; - public digitalCultureSecurity: string[]; + public isOpen: boolean = false; + public openedOn: OpeningDay = new OpeningDay(); + public baseSkills: string[] = []; + public accessRight: string[] = []; + public parentingHelp: string[] = []; + public socialAndProfessional: string[] = []; + public digitalCultureSecurity: string[] = []; public distance?: number; - public coord?: number[]; + public coord?: number[] = []; constructor(obj?: any) { Object.assign(this, obj, { - hours: obj && obj.hours ? new Week(obj.hours) : null, + hours: obj && obj.hours ? new Week(obj.hours) : new Week(), }); } diff --git a/src/app/models/user.model.ts b/src/app/models/user.model.ts index aa2391853d058ee08bd800715ec4bd620220678b..a60ee2247268e621cfe6a30259a4603c4d9fdd93 100644 --- a/src/app/models/user.model.ts +++ b/src/app/models/user.model.ts @@ -5,4 +5,5 @@ export class User { emailVerified: boolean; role: number; validationToken: string; + structuresLink: number[]; } diff --git a/src/app/models/week.model.ts b/src/app/models/week.model.ts index b2176e78550122b9bfbffa04905700122b54080b..742c749d8c8ae7df37c1f2c1876dd84572888b5f 100644 --- a/src/app/models/week.model.ts +++ b/src/app/models/week.model.ts @@ -11,13 +11,13 @@ export class Week { constructor(obj?: any) { Object.assign(this, obj, { - monday: obj && obj.monday ? new Day(obj.monday) : null, - tuesday: obj && obj.tuesday ? new Day(obj.tuesday) : null, - wednesday: obj && obj.wednesday ? new Day(obj.wednesday) : null, - thursday: obj && obj.thursday ? new Day(obj.thursday) : null, - friday: obj && obj.friday ? new Day(obj.friday) : null, - saturday: obj && obj.saturday ? new Day(obj.saturday) : null, - sunday: obj && obj.sunday ? new Day(obj.sunday) : null, + monday: obj && obj.monday ? new Day(obj.monday) : new Day(), + tuesday: obj && obj.tuesday ? new Day(obj.tuesday) : new Day(), + wednesday: obj && obj.wednesday ? new Day(obj.wednesday) : new Day(), + thursday: obj && obj.thursday ? new Day(obj.thursday) : new Day(), + friday: obj && obj.friday ? new Day(obj.friday) : new Day(), + saturday: obj && obj.saturday ? new Day(obj.saturday) : new Day(), + sunday: obj && obj.sunday ? new Day(obj.sunday) : new Day(), }); } diff --git a/src/app/profile/profile.component.html b/src/app/profile/profile.component.html index d85790c5f574a43b2182dc6fd633eb638e176910..7e26fd2292017a79dfa4a9c57e9f7e8878a43b21 100644 --- a/src/app/profile/profile.component.html +++ b/src/app/profile/profile.component.html @@ -4,6 +4,7 @@ <div *ngIf="userProfile" fxLayout="column" fxLayoutAlign="center" fxLayoutGap="10px"> <p>Id: {{ userProfile._id }}</p> <p>Email: {{ userProfile.email }}</p> + <button (click)="toogleAddStructure()">Ajouter une structure</button> <button (click)="toogleChangeEmail()">Changer d'email</button> <form *ngIf="changeEmail" @@ -77,4 +78,10 @@ </form> </div> </div> + <app-structureForm + *ngIf="addStructure" + [profile]="userProfile" + (closeEvent)="toogleAddStructure($event)" + (clickOutside)="toogleAddStructure()" + ></app-structureForm> </div> diff --git a/src/app/profile/profile.component.ts b/src/app/profile/profile.component.ts index f9183d465112cc9c5ee38ce693646df618ba93ee..e340e7e7078ec4372d84c13961c1f8394f6d4bc1 100644 --- a/src/app/profile/profile.component.ts +++ b/src/app/profile/profile.component.ts @@ -1,5 +1,6 @@ import { Component, OnInit } from '@angular/core'; import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms'; +import { Structure } from '../models/structure.model'; import { User } from '../models/user.model'; import { MustMatch } from '../shared/validator/form'; import { ProfileService } from './services/profile.service'; @@ -17,11 +18,12 @@ export class ProfileComponent implements OnInit { public loading = false; public changeEmail = false; public formEmail: FormGroup; + public addStructure = false; constructor(private formBuilder: FormBuilder, private profileService: ProfileService) {} ngOnInit(): void { - this.profileService.getProfile().subscribe((profile) => { + this.profileService.getProfile().then((profile) => { this.userProfile = profile; }); this.initForm(); @@ -55,6 +57,9 @@ export class ProfileComponent implements OnInit { public toogleChangePassword(): void { this.changePassword = !this.changePassword; } + public toogleAddStructure(): void { + this.addStructure = !this.addStructure; + } public toogleChangeEmail(): void { this.changeEmail = !this.changeEmail; diff --git a/src/app/profile/profile.module.ts b/src/app/profile/profile.module.ts index a9427003f2507d4092e966617363aa223996b4f5..1f86c78ac628b6b082a4fcc81f6eead59e0b8e92 100644 --- a/src/app/profile/profile.module.ts +++ b/src/app/profile/profile.module.ts @@ -3,10 +3,11 @@ import { ProfileComponent } from './profile.component'; import { SharedModule } from '../shared/shared.module'; import { CommonModule } from '@angular/common'; import { BrowserModule } from '@angular/platform-browser'; +import { FormComponent } from '../form/form.component'; @NgModule({ imports: [CommonModule, BrowserModule, SharedModule], - declarations: [ProfileComponent], - exports: [ProfileComponent], + declarations: [ProfileComponent, FormComponent], + exports: [ProfileComponent, FormComponent], }) export class ProfileModule {} diff --git a/src/app/profile/services/profile.service.ts b/src/app/profile/services/profile.service.ts index ef4d4537a3f3267d23918e12c2a8218e5ecde3e5..819eb0d3f9630f4e409bf31d67849a763965386a 100644 --- a/src/app/profile/services/profile.service.ts +++ b/src/app/profile/services/profile.service.ts @@ -2,17 +2,27 @@ import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; import { User } from '../../models/user.model'; +import { AuthService } from '../../services/auth.service'; @Injectable({ providedIn: 'root', }) export class ProfileService { private readonly baseUrl = 'api/users'; + private currentProfile: User = null; + constructor(private http: HttpClient, private authService: AuthService) {} - constructor(private http: HttpClient) {} + public async getProfile(): Promise<User> { + // Get profil by API only on first time + if (!this.currentProfile) { + const profile = await this.http.get<User>(`${this.baseUrl}/profile`).toPromise(); + this.currentProfile = profile; + } + return this.currentProfile; + } - public getProfile(): Observable<User> { - return this.http.get<User>(`${this.baseUrl}/profile`); + public isLinkedToStructure(idStructure: number): boolean { + return this.currentProfile.structuresLink.includes(idStructure); } public changePassword(newPassword: string, oldPassword: string): Observable<User> { diff --git a/src/app/services/structure.service.ts b/src/app/services/structure.service.ts index f881eb0a6875a6b3ebbff59e81880a2b39f15d9a..0c8192ceaabdb7b91c7138e4dcb275b136595be0 100644 --- a/src/app/services/structure.service.ts +++ b/src/app/services/structure.service.ts @@ -11,6 +11,7 @@ import { OpeningDay } from '../models/openingDay.model'; import { Weekday } from '../structure-list/enum/weekday.enum'; import { Time } from '../models/time.model'; import { Filter } from '../structure-list/models/filter.model'; +import { User } from '../models/user.model'; @Injectable({ providedIn: 'root', @@ -18,6 +19,11 @@ import { Filter } from '../structure-list/models/filter.model'; export class StructureService { constructor(private http: HttpClient) {} + public createStructure(structure: Structure, profile: User): Observable<Structure> { + const idUser = profile.email; + return this.http.post('/api/structures', { structure, idUser }).pipe(map((item: Structure) => new Structure(item))); + } + public postStructure(id: number, structure: Structure): Observable<Structure> { structure.updatedAt = new Date().toString(); return this.http.post('/api/structures/' + id, structure).pipe(map((item: Structure) => new Structure(item))); diff --git a/src/app/structure-list/components/structure-details/structure-details.component.html b/src/app/structure-list/components/structure-details/structure-details.component.html index 77524b63675180e727120379d31e34d65780c2f7..98fe48644febca8f5affbd845b2f764b3a1e8d5f 100644 --- a/src/app/structure-list/components/structure-details/structure-details.component.html +++ b/src/app/structure-list/components/structure-details/structure-details.component.html @@ -7,6 +7,9 @@ <div class="structrue-details-container" *ngIf="structure"> <!-- Header info --> <div fxLayout="row" fxLayoutAlign="end center"> + <button *ngIf="profileService.isLinkedToStructure(structure.id)" (click)="displayForm()"> + Modifier la structure + </button> <div (click)="close()" class="ico-close-details"></div> </div> <div fxLayout="row" class="structure-details-block" fxLayoutAlign="baseline baseline" fxLayoutGap="8px"> diff --git a/src/app/structure-list/components/structure-details/structure-details.component.ts b/src/app/structure-list/components/structure-details/structure-details.component.ts index db1de038cba5e3601564cee110ca5e37c696bf86..45e106ef78215e04f359d5191b1f28ac989e8899 100644 --- a/src/app/structure-list/components/structure-details/structure-details.component.ts +++ b/src/app/structure-list/components/structure-details/structure-details.component.ts @@ -9,6 +9,8 @@ import { ActivatedRoute } from '@angular/router'; import { PrintService } from '../../../shared/service/print.service'; import { Equipment } from '../../enum/equipment.enum'; import { typeStructureEnum } from '../../../shared/enum/typeStructure.enum'; +import { ProfileService } from '../../../profile/services/profile.service'; +import { User } from '../../../models/user.model'; @Component({ selector: 'app-structure-details', templateUrl: './structure-details.component.html', @@ -27,8 +29,14 @@ export class StructureDetailsComponent implements OnInit { public printMode = false; public isOtherSection = false; public showForm = false; + public currentProfile: User; - constructor(route: ActivatedRoute, private printService: PrintService, private searchService: SearchService) { + constructor( + route: ActivatedRoute, + private printService: PrintService, + private profileService: ProfileService, + private searchService: SearchService + ) { route.url.subscribe((url) => { if (url[0].path === 'structure') { this.structure = this.printService.structure; @@ -38,6 +46,18 @@ export class StructureDetailsComponent implements OnInit { } ngOnInit(): void { + this.profileService.getProfile().then((p: User) => { + this.currentProfile = p; + }); + this.setReferentiels(); + const index = this.structure.proceduresAccompaniment.indexOf('autres'); + if (index > -1) { + this.structure.proceduresAccompaniment.splice(index, 1); + this.isOtherSection = true; + } + } + + private setReferentiels(): void { this.searchService.getCategoriesTraining().subscribe((referentiels) => { referentiels.forEach((referentiel) => { if (referentiel.isBaseSkills()) { @@ -51,13 +71,7 @@ export class StructureDetailsComponent implements OnInit { this.printService.onDataReady(); } }); - const index = this.structure.proceduresAccompaniment.indexOf('autres'); - if (index > -1) { - this.structure.proceduresAccompaniment.splice(index, 1); - this.isOtherSection = true; - } } - public getLabelTypeStructure(typeStructure: string[]): string { let label = ''; typeStructure.forEach((type) => { @@ -118,8 +132,9 @@ export class StructureDetailsComponent implements OnInit { public updateStructure(s: Structure): void { this.structure = new Structure({ ...this.structure, ...s }); - this.displayForm(); this.updatedStructure.emit(this.structure); + this.setReferentiels(); + this.displayForm(); } public getAccessIcon(accessModality: AccessModality): string { switch (accessModality) {