diff --git a/frontend/src/app/app.routes.ts b/frontend/src/app/app.routes.ts index 1010752..631dc29 100644 --- a/frontend/src/app/app.routes.ts +++ b/frontend/src/app/app.routes.ts @@ -13,6 +13,10 @@ export const routes: Routes = [ path: 'disconnect', loadComponent: () => import('./pages/disconnection/disconnection.component').then(module => module.DisconnectionComponent) }, + { + path: 'publications/new', + loadChildren: () => import('./pages/publication-creation/publication-creation.routes').then(module => module.ROUTES) + }, { path: 'publications/:publicationId', loadComponent: () => import('./pages/publication/publication.component').then(module => module.PublicationComponent) diff --git a/frontend/src/app/components/publication-edition/publication-edition.component.html b/frontend/src/app/components/publication-edition/publication-edition.component.html index 990332a..e4e2e01 100644 --- a/frontend/src/app/components/publication-edition/publication-edition.component.html +++ b/frontend/src/app/components/publication-edition/publication-edition.component.html @@ -1,6 +1,6 @@
-

Modification de l'article {{ publication.title }}

+

{{title}}

@@ -19,7 +19,12 @@
- + @if (publication.illustrationId.length) { + + } @else { + + } +
diff --git a/frontend/src/app/components/publication-edition/publication-edition.component.ts b/frontend/src/app/components/publication-edition/publication-edition.component.ts index b20fb6f..af690d4 100644 --- a/frontend/src/app/components/publication-edition/publication-edition.component.ts +++ b/frontend/src/app/components/publication-edition/publication-edition.component.ts @@ -36,6 +36,8 @@ export class PublicationEditionComponent implements OnChanges, OnDestroy { @Input() publication!: Publication; @Input() + title!: string; + @Input() isSaving$: Observable = of(false); @Output() publicationSave = new EventEmitter(); diff --git a/frontend/src/app/core/rest-services/publications/publication.rest-service.ts b/frontend/src/app/core/rest-services/publications/publication.rest-service.ts index 8c47858..91e94ec 100644 --- a/frontend/src/app/core/rest-services/publications/publication.rest-service.ts +++ b/frontend/src/app/core/rest-services/publications/publication.rest-service.ts @@ -17,6 +17,10 @@ export class PublicationRestService { return lastValueFrom(this.httpClient.get(`/api/publications/${publicationId}`)); } + create(publication: Publication): Promise { + return lastValueFrom(this.httpClient.post('/api/publications', publication)); + } + update(publication: Publication): Promise { return lastValueFrom(this.httpClient.put(`/api/publications/${publication.id}`, publication)); } diff --git a/frontend/src/app/pages/publication-creation/publication-creation.component.html b/frontend/src/app/pages/publication-creation/publication-creation.component.html new file mode 100644 index 0000000..18365be --- /dev/null +++ b/frontend/src/app/pages/publication-creation/publication-creation.component.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/frontend/src/app/pages/publication-creation/publication-creation.component.scss b/frontend/src/app/pages/publication-creation/publication-creation.component.scss new file mode 100644 index 0000000..e69de29 diff --git a/frontend/src/app/pages/publication-creation/publication-creation.component.ts b/frontend/src/app/pages/publication-creation/publication-creation.component.ts new file mode 100644 index 0000000..b0deeb2 --- /dev/null +++ b/frontend/src/app/pages/publication-creation/publication-creation.component.ts @@ -0,0 +1,76 @@ +import { Component, inject, OnInit } from "@angular/core"; +import { PublicationEditionComponent } from "../../components/publication-edition/publication-edition.component"; +import { PublicationRestService } from "../../core/rest-services/publications/publication.rest-service"; +import { ActivatedRoute, Router } from "@angular/router"; +import { MatSnackBar } from "@angular/material/snack-bar"; +import { BehaviorSubject, Observable, Subscription } from "rxjs"; +import { Publication } from "../../core/rest-services/publications/model/publication"; +import { AuthenticationService } from "../../core/service/authentication.service"; +import { Author } from "../../core/rest-services/publications/model/author"; +import { CommonModule } from "@angular/common"; + +@Component({ + selector: 'app-publication-creation', + standalone: true, + templateUrl: './publication-creation.component.html', + styleUrl: './publication-creation.component.scss', + imports: [ + CommonModule, + PublicationEditionComponent + ] +}) +export class PublicationCreationComponent implements OnInit { + private readonly authenticationService = inject(AuthenticationService); + private readonly publicationRestService = inject(PublicationRestService); + private readonly router = inject(Router); + private readonly snackBar = inject(MatSnackBar); + private isSavingSubject = new BehaviorSubject(false); + private subscriptions: Subscription[] = []; + publication!: Publication; + + get isSaving$(): Observable { + return this.isSavingSubject.asObservable(); + } + + ngOnInit(): void { + const authenticatedUser = this.authenticationService.getAuthenticatedUser(); + if (authenticatedUser) { + const author: Author = { + id: authenticatedUser.id, + name: authenticatedUser.pseudo, + image: authenticatedUser.photoId ?? '' + }; + this.publication = { + id: '', + key: '', + title: '', + text: '', + parsedText: '', + description: '', + creationDate: new Date(), + illustrationId: '', + categoryId: '', + author + }; + } + } + + ngOnDestroy(): void { + this.subscriptions.forEach(subscription => subscription?.unsubscribe()); + } + + onPublicationSave(publication: Publication): void { + this.isSavingSubject.next(true); + this.publicationRestService.create(publication) + .then(() => { + this.snackBar.open('Publication created succesfully!', 'Close', { duration: 5000 }); + this.router.navigate(['/my-publications']); + }) + .catch(error => { + const errorMessage = 'An error occured while saving new publication.'; + console.error(errorMessage, error); + this.snackBar.open(errorMessage, 'Close', { duration: 5000 }); + }) + .finally(() => this.isSavingSubject.next(false)); + } +} \ No newline at end of file diff --git a/frontend/src/app/pages/publication-creation/publication-creation.routes.ts b/frontend/src/app/pages/publication-creation/publication-creation.routes.ts new file mode 100644 index 0000000..5df6af3 --- /dev/null +++ b/frontend/src/app/pages/publication-creation/publication-creation.routes.ts @@ -0,0 +1,7 @@ +import { Route } from "@angular/router"; +import { authenticationGuard } from "../../core/guard/authentication.guard"; +import { PublicationCreationComponent } from "./publication-creation.component"; + +export const ROUTES: Route[] = [ + { path: '', component: PublicationCreationComponent, canActivate: [authenticationGuard] } +] \ No newline at end of file diff --git a/frontend/src/app/pages/publication-update/publication-update.component.html b/frontend/src/app/pages/publication-update/publication-update.component.html index b690b72..f6f275d 100644 --- a/frontend/src/app/pages/publication-update/publication-update.component.html +++ b/frontend/src/app/pages/publication-update/publication-update.component.html @@ -3,7 +3,7 @@ } @else { @if (publication) { - + } @else {
diff --git a/frontend/src/assets/images/default-picture.png b/frontend/src/assets/images/default-picture.png new file mode 100644 index 0000000..5f313f0 Binary files /dev/null and b/frontend/src/assets/images/default-picture.png differ