diff --git a/client/src/app/categories/admin-category/admin-category.component.html b/client/src/app/categories/admin-category/admin-category.component.html index bdbf67b78318dff4188c6b8894ee27e6b1df1b15..c5106d5e17216e6f67c71e923a41cf56e692f78c 100644 --- a/client/src/app/categories/admin-category/admin-category.component.html +++ b/client/src/app/categories/admin-category/admin-category.component.html @@ -1 +1,9 @@ -<p>admin-category works!</p> +<div class="categoryForm"> + <h3>Legg til kategori</h3> + + <app-text-input [(inputModel)]="name" label="Kategorinavn" (blur)="checkForm()"></app-text-input> + + <p>{{statusMessage}}</p> + + <app-button (click)="addCategory" text="Legg til"></app-button> +</div> \ No newline at end of file diff --git a/client/src/app/categories/admin-category/admin-category.component.ts b/client/src/app/categories/admin-category/admin-category.component.ts index 526d7de0cf11fcf8a18694d8d03d27d33588d754..344b98eff5a9346b7c36f28b0d782b742b260496 100644 --- a/client/src/app/categories/admin-category/admin-category.component.ts +++ b/client/src/app/categories/admin-category/admin-category.component.ts @@ -1,4 +1,7 @@ import { Component, OnInit } from '@angular/core'; +import { Router } from '@angular/router'; +import { AuthService } from 'src/app/authentication/auth.service'; +import { PostService } from 'src/app/posts/post.service'; @Component({ selector: 'app-admin-category', @@ -6,10 +9,49 @@ import { Component, OnInit } from '@angular/core'; styleUrls: ['./admin-category.component.scss'] }) export class AdminCategoryComponent implements OnInit { + user: new User(); + name: string = ""; + statusMessage: string = ""; - constructor() { } + constructor(private postService: PostService, private authService: AuthService) { } ngOnInit(): void { + this.user = this.authService.getCurrentUser(); + if (this.user.get) } + /** + * Validates the form + */ + checkForm(): boolean { + if (this.name == "") { + this.setStatusMessage("Brukernavn kan ikke vƦre tom"); + return false; + } + + this.setStatusMessage(""); + return true; + } + + /** + * Publishes and registers the user if given arguments are valid + */ + registerCategory() { + if (this.checkForm()) { + // Adds user to database and redirects to the homepage afterwards + this.postService.addCategory(name).then(status => { + console.log("Category was added: " + JSON.stringify(status)); + loadCategories(); + }).catch(error => { + console.log("Error adding category: " + error); + }); + } + } + + /** + * Sets the status message for user feedback on form submit + */ + setStatusMessage(message: string) { + this.statusMessage = message; + } }