+
+

+ @if (!isNarrow) {
+
+
Admin Dashboard
+ }
+
+
+
- }
-
-
-
-
Admin Dashboard
-
-
-
-
-
-
+
-
-
+
+
+
- @if (modalAction === 'edit') {
-
- }
-
- @if (modalAction === 'delete') {
-
- }
-
+
+ @if (!isNarrow) {
+
+
+
+ {{ isViewingArticles ? 'Articles' : 'Users' }}
+
+
+
-
-
Create New Article
-
-
- @if (isNarrow) {
-
-
Delete Article
-
+ @if (isViewingArticles) {
+
+ @if (noArticles) {
+
No articles available.
+ }
+ @if (!noArticles) {
+
+ @for (article of articles; track article) {
+ -
+ ID {{ article.id }} - {{ article.title }}
+
+ }
+
+ }
-
-
Edit Article
-
-
-
-
- {{ isViewingArticles ? 'Articles' : 'Users' }}
-
-
- @if (isViewingArticles) {
+ }
+
+ @if (!isViewingArticles) {
+
+ @if (users.length === 0) {
- @if (noArticles) {
-
-
No articles available.
-
- }
- @if (!noArticles) {
-
-
- @for (article of articles; track article) {
- -
- ID {{ article.id }} - {{ article.title }}
-
- }
-
-
- }
+
No users available.
}
-
- @if (!isViewingArticles) {
+ @if (users.length > 0) {
- @if (users.length === 0) {
-
- }
- @if (users.length > 0) {
-
-
- @for (user of users; track user) {
- -
- ID {{ user.id }} - {{ user.username }}
-
- }
-
-
- }
+
+ @for (user of users; track user) {
+ -
+ ID {{ user.id }} - {{ user.username }}
+
+ }
+
}
}
-
-
- @if (!isNarrow) {
-
-
-
- {{ isViewingArticles ? 'Articles' : 'Users' }}
-
-
- @if (isViewingArticles) {
-
- @if (noArticles) {
-
-
No articles available.
-
- }
- @if (!noArticles) {
-
-
- @for (article of articles; track article) {
- -
- ID {{ article.id }} - {{ article.title }}
-
- }
-
-
- }
-
- }
-
- @if (!isViewingArticles) {
-
- @if (users.length === 0) {
-
- }
- @if (users.length > 0) {
-
-
- @for (user of users; track user) {
- -
- ID {{ user.id }} - {{ user.username }}
-
- }
-
-
- }
-
- }
-
-
- }
-
+ }
+
+
+
+
+
diff --git a/src/app/admin/admin.component.ts b/src/app/admin/admin.component.ts
index f6a8513..3a2cf68 100644
--- a/src/app/admin/admin.component.ts
+++ b/src/app/admin/admin.component.ts
@@ -1,8 +1,8 @@
import {Component, OnDestroy, OnInit} from '@angular/core';
-import {Router} from '@angular/router';
+import {Router, RouterLink} from '@angular/router';
import {ArticleService} from '../shared/services/article.service';
import {UserService} from '../shared/services/user.service';
-import { NgStyle } from '@angular/common';
+import {NgStyle, TitleCasePipe} from '@angular/common';
import {Observer, Subscription, interval} from 'rxjs';
import {FormsModule, NgForm} from '@angular/forms';
import {ViewportService} from '../shared/services/viewport.service';
@@ -17,8 +17,10 @@ import {AuthService} from '../shared/services/auth.service';
imports: [
FormsModule,
NgStyle,
- ModalComponent
-]
+ ModalComponent,
+ TitleCasePipe,
+ RouterLink
+ ]
})
export class AdminComponent implements OnInit, OnDestroy {
tokenValidationInterval: Subscription = new Subscription();
@@ -33,9 +35,9 @@ export class AdminComponent implements OnInit, OnDestroy {
content: string = '';
modalTitle: string = '';
- modalAction: string = '';
isModalOpen: boolean = false;
- isViewingArticles: boolean = true;
+ userMode: 'create' | 'edit' | 'delete' = 'create';
+ isUserDropdownOpen = false;
userData = {
username: '',
@@ -43,6 +45,11 @@ export class AdminComponent implements OnInit, OnDestroy {
id: '',
};
+ isViewingArticles: boolean = true;
+
+ articleMode: 'create' | 'edit' | 'delete' = 'create';
+ isDropdownOpen = false;
+
constructor(
private articleService: ArticleService,
private userService: UserService,
@@ -70,11 +77,8 @@ export class AdminComponent implements OnInit, OnDestroy {
});
}
- createArticle(form: any) {
- const title = form.value.title;
- const content = form.value.content;
-
- if (!title || !content) {
+ createArticle(title:string, content: string) {
+ if (!title?.trim() || !content?.trim()) {
alert('Both title and content are required!');
return;
}
@@ -83,22 +87,21 @@ export class AdminComponent implements OnInit, OnDestroy {
if (!valid) return;
const token = this.authService.token!;
- this.articleService.postArticle(token, title, content).subscribe(
- () => {
- alert('Article created successfully!');
+
+ this.articleService.postArticle(token, title.trim(), content.trim()).subscribe({
+ next: () => {
+ alert('Article created successfully.');
this.loadArticles();
},
- error => {
+ error: err => {
alert('Error creating article');
- console.error(error);
+ console.log(err);
}
- );
+ });
});
}
- editArticle(form: NgForm): void {
- const articleId = form.value.editId;
-
+ editArticle(articleId: number, title?: string, content?: string): void {
if (!articleId || isNaN(Number(articleId))) {
alert('Invalid input. Please enter a valid article ID.');
return;
@@ -118,11 +121,8 @@ export class AdminComponent implements OnInit, OnDestroy {
return;
}
- const newTitle = form.value.editTitle || form.value.editTitleMobile;
- const newContent = form.value.editContent || form.value.editContentMobile;
-
- const updatedTitle = newTitle?.trim() || article.title;
- const updatedContent = newContent?.trim() || article.content;
+ const updatedTitle = title?.trim() || article.title;
+ const updatedContent = content?.trim() || article.content;
this.articleService.editArticle(id, token, updatedTitle, updatedContent).subscribe(
(response) => {
@@ -144,9 +144,7 @@ export class AdminComponent implements OnInit, OnDestroy {
});
}
- deleteArticle(form: any): void {
- const articleId = form.value.deleteId || form.value.deleteIdMobile;
-
+ deleteArticle(articleId: number): void {
if (!articleId || isNaN(Number(articleId))) {
alert('Invalid input. Please enter a valid article ID.');
return;
@@ -210,20 +208,49 @@ export class AdminComponent implements OnInit, OnDestroy {
}
}
- openModal(action: string) {
+ openUserModal() {
this.isModalOpen = true;
- this.modalAction = action;
+ this.modalTitle = 'Manage Users';
+ this.userMode = 'create';
+ this.isUserDropdownOpen = false;
+ }
- switch (action) {
- case 'create':
- this.modalTitle = 'Create User';
- break;
- case 'edit':
- this.modalTitle = 'Edit User';
- break;
- case 'delete':
- this.modalTitle = 'Delete User';
- break;
+ toggleUserDropdown(): void {
+ this.isUserDropdownOpen = !this.isUserDropdownOpen;
+ }
+
+ setUserMode(mode: 'create' | 'edit' | 'delete'): void {
+ this.userMode = mode;
+ this.isUserDropdownOpen = false;
+ }
+
+ isUserSubmitDisabled(form: NgForm): boolean {
+ const { userId, username, password } = form.value;
+
+ if (this.userMode === 'create') {
+ return !username?.trim() || !password?.trim();
+ }
+
+ if (this.userMode === 'edit') {
+ return !userId;
+ }
+
+ if (this.userMode === 'delete') {
+ return !userId;
+ }
+
+ return true;
+ }
+
+ submitUser(form: NgForm): void {
+ const { userId, username, password } = form.value;
+
+ if (this.userMode === 'create') {
+ this.createUserFromValues(username, password);
+ } else if (this.userMode === 'edit') {
+ this.editUserFromValues(Number(userId), username, password);
+ } else if (this.userMode === 'delete') {
+ this.deleteUserById(Number(userId));
}
}
@@ -236,113 +263,128 @@ export class AdminComponent implements OnInit, OnDestroy {
};
}
- createUser(form: any): void {
- const username = form.value.createUsername;
- const password = form.value.createPassword;
-
- if (!username || !password) {
+ createUserFromValues(username: string, password: string): void {
+ if (!username?.trim() || !password?.trim()) {
alert('Both username and password are required!');
return;
}
- this.authService.validateToken().subscribe(valid => {
- if (!valid) return;
+ this.authService.validateToken().subscribe({
+ next: valid => {
+ if (!valid) return;
- const token = this.authService.token!;
+ const token = this.authService.token!;
- this.userService.createUser(token, username, password).subscribe(
- (response) => {
- alert('User created successfully!');
- this.loadUsers();
- console.log(response);
- },
-
- (error) => {
- alert('Error creating user');
- console.error(error);
+ this.userService.createUser(token, username.trim(), password.trim()).subscribe({
+ next: response => {
+ alert('User created successfully!');
+ this.loadUsers();
+ console.log(response);
+ this.closeModal();
+ },
+ error: error => {
+ alert('Error creating user');
+ console.error(error);
+ }
});
- this.closeModal();
+ }
});
}
- editUser(form: any): void {
- const userId = form.value.editUserId;
-
+ editUserFromValues(userId: number, username?: string, password?: string): void {
if (!userId || isNaN(Number(userId))) {
alert('Invalid input. Please enter a valid user ID.');
- this.closeModal();
return;
}
const id = Number(userId);
- this.authService.validateToken().subscribe(valid => {
- if (!valid) return;
+ this.authService.validateToken().subscribe({
+ next: valid => {
+ if (!valid) return;
- const token = this.authService.token!;
+ const token = this.authService.token!;
- this.userService.getUserById(id, token).subscribe(
- (user) => {
- if (!user) {
- alert(`User with ID ${id} not found.`);
- return;
- }
-
- const newUsername = form.value.editUsername;
- const newPassword = form.value.editPassword;
-
- const updatedUsername = newUsername?.trim();
- const updatedPassword = newPassword?.trim();
-
- this.userService.editUser(id, token, updatedUsername, updatedPassword).subscribe(
- (response) => {
- alert(`User ${id} updated successfully!`);
- console.log('Updated User:', response);
- this.loadUsers();
- },
- (error) => {
- alert(`Error updating user ${id}: ${error.message}`);
- console.error('Error:', error);
+ this.userService.getUserById(id, token).subscribe({
+ next: user => {
+ if (!user) {
+ alert(`User with ID ${id} not found.`);
+ return;
}
- );
- },
- (error: { message: any }) => {
- alert(`Failed to fetch user with ID ${id}: ${error.message}`);
- console.error('Error:', error);
- }
- );
- });
- this.closeModal();
+ const updatedUsername = username?.trim() || undefined;
+ const updatedPassword = password?.trim() || undefined;
+
+ this.userService.editUser(id, token, updatedUsername, updatedPassword).subscribe({
+ next: response => {
+ alert(`User ${id} updated successfully!`);
+ console.log('Updated User:', response);
+ this.loadUsers();
+ this.closeModal();
+ },
+ error: error => {
+ alert(`Error updating user ${id}: ${error.message}`);
+ console.error(error);
+ }
+ });
+ },
+ error: error => {
+ alert(`Failed to fetch user with ID ${id}: ${error.message}`);
+ console.error(error);
+ }
+ });
+ }
+ });
}
- deleteUser(form: any): void {
- const userId = form.value.deleteUserId;
-
+ deleteUserById(userId: number): void {
if (!userId || isNaN(Number(userId))) {
alert('Invalid input. Please enter a valid user ID.');
- console.error('Invalid input:', userId);
- this.closeModal();
return;
}
- this.authService.validateToken().subscribe(valid => {
- if (!valid) return;
+ this.authService.validateToken().subscribe({
+ next: valid => {
+ if (!valid) return;
- const token = this.authService.token!;
+ const token = this.authService.token!;
- this.userService.deleteUser(Number(userId), token).subscribe(
- () => {
- alert(`User ${userId} deleted successfully`);
- this.loadUsers();
- },
- (error) => {
- alert(`Error deleting user ${userId}: ${error.message}`);
- console.error('Error deleting user:', error);
- }
- );
+ this.userService.deleteUser(userId, token).subscribe({
+ next: () => {
+ alert(`User ${userId} deleted successfully`);
+ this.loadUsers();
+ this.closeModal();
+ },
+ error: error => {
+ alert(`Error deleting user ${userId}: ${error.message}`);
+ console.error(error);
+ }
+ });
+ }
});
+ }
- this.closeModal();
+ toggleDropdown() {
+ this.isDropdownOpen = !this.isDropdownOpen;
+ }
+
+ setMode(mode: 'create' | 'edit' | 'delete') {
+ this.articleMode = mode;
+ this.isDropdownOpen = false;
+ }
+
+ submitArticle(form: NgForm) {
+ if (form.invalid) return;
+
+ const { title, articleId, content } = form.value;
+ const mode = this.articleMode;
+
+ if (mode === 'create') {
+ this.createArticle(title, content);
+ } else if (mode === 'edit') {
+ this.editArticle(articleId, title, content);
+ } else if (mode === 'delete') {
+ this.deleteArticle(articleId);
+ }
}
}
diff --git a/src/app/login/login.component.css b/src/app/login/login.component.css
index 45f6f38..99e508c 100644
--- a/src/app/login/login.component.css
+++ b/src/app/login/login.component.css
@@ -22,7 +22,7 @@
.login-container {
color: white;
flex-direction: column;
- background: #26262c;
+ background: #202025;
border-radius: 25px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
padding: 40px;
@@ -42,9 +42,15 @@ input {
padding: 12px;
margin: 10px 0;
color: white;
- background: #1c1c23;
+ background: #26262c;
border: none;
border-radius: 25px;
+ transition: all ease-in-out 0.2s;
+}
+
+input:focus {
+ outline: none;
+ background: #2b2b32;
}
button {
@@ -56,15 +62,16 @@ button {
border: none;
border-radius: 25px;
cursor: pointer;
+ transition: all ease-in-out 0.2s;
}
button:disabled {
- background-color: #221a1a;
+ background-color: rgba(64, 41, 41, 0.25);
cursor: not-allowed;
}
button:hover:enabled {
- background-color: #ff277a;
+ background-color: #a36cff;
}
.error-message {
diff --git a/src/app/shared/modal/modal.component.css b/src/app/shared/modal/modal.component.css
index de2fb80..c62995f 100644
--- a/src/app/shared/modal/modal.component.css
+++ b/src/app/shared/modal/modal.component.css
@@ -8,25 +8,38 @@
display: flex;
justify-content: center;
align-items: center;
+ text-align: center;
z-index: 1000;
}
.modal {
- background: #fff;
- padding: 20px;
+ background-color: #202025;
+ color: #fff;
border-radius: 25px;
+ padding: 15px;
min-width: 300px;
max-width: 300px;
position: relative;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
+ z-index: 1001;
}
.close-btn {
position: absolute;
- top: 10px;
- right: 10px;
+ top: 4px;
+ right: 8px;
background: none;
+ color: white;
border: none;
- font-size: 20px;
+ font-size: 28px;
cursor: pointer;
+ transition: all ease-in-out 0.2s;
+}
+
+.close-btn:hover {
+ color: #ebe1fa;
+}
+
+.close-btn:active {
+ color: #a172e8;
}
diff --git a/src/app/shared/modal/modal.component.html b/src/app/shared/modal/modal.component.html
index 3b6ff87..aca3bd2 100644
--- a/src/app/shared/modal/modal.component.html
+++ b/src/app/shared/modal/modal.component.html
@@ -1,7 +1,7 @@
@if (isOpen) {
-
+
{{ title }}
diff --git a/src/app/shared/services/user.service.ts b/src/app/shared/services/user.service.ts
index cf253ef..f52549c 100644
--- a/src/app/shared/services/user.service.ts
+++ b/src/app/shared/services/user.service.ts
@@ -31,7 +31,7 @@ export class UserService {
}, { headers });
}
- editUser(id: number, token: string | null, username: string, password: string): Observable
{
+ editUser(id: number, token: string | null, username?: string, password?: string): Observable {
const headers = new HttpHeaders().set('Authorization', `Bearer ${token}`);
const body: any = {};