Added User Management to Admin Panel, reorganized some stuff
This commit is contained in:
1 parent
d83992d3ce
commit
83260af2e6
22 files changed
+439
-36
No files matched your search
@@ -89,7 +89,7 @@
|
||||
margin-bottom: 30px;
|
||||
max-width: 300px;
|
||||
}
|
||||
.delete-article-tile input {
|
||||
.delete-article-tile input, #editUserId, #deleteUserId {
|
||||
max-width: 80px;
|
||||
margin-right: 20px;
|
||||
margin-left: 20px;
|
||||
@@ -110,7 +110,7 @@ textarea {
|
||||
border-radius: 25px;
|
||||
}
|
||||
|
||||
.admin-actions button, .create-article-tile button, .delete-article-tile button, .edit-article-tile button {
|
||||
button {
|
||||
background-color: #bd3948;
|
||||
color: white;
|
||||
padding: 12px 20px;
|
||||
@@ -120,11 +120,11 @@ textarea {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.admin-actions button:hover:enabled, .create-article-tile button:hover:enabled, .delete-article-tile button:hover:enabled, .edit-article-tile button:hover:enabled {
|
||||
button:hover:enabled {
|
||||
background-color: #e3485a;
|
||||
}
|
||||
|
||||
.create-article-tile button:disabled, .delete-article-tile button:disabled, .edit-article-tile button:disabled {
|
||||
button:disabled {
|
||||
background-color: #ccc;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
@@ -143,6 +143,26 @@ textarea {
|
||||
background-color: #592840;
|
||||
}
|
||||
|
||||
button.toggle-button {
|
||||
background-color: transparent;
|
||||
padding: 0;
|
||||
color: #007bff;
|
||||
margin: 10px;
|
||||
border: none;
|
||||
border-radius: 25px;
|
||||
cursor: pointer;
|
||||
transform: translateY(+5px);
|
||||
}
|
||||
|
||||
button.toggle-button i {
|
||||
font-size: 27px;
|
||||
}
|
||||
|
||||
button.toggle-button:hover:active, button.toggle-button:hover {
|
||||
background-color: transparent;
|
||||
color: #0056b3;
|
||||
}
|
||||
|
||||
.articles-list-tile ul {
|
||||
list-style-type: none;
|
||||
padding: 0;
|
||||
@@ -164,6 +184,7 @@ textarea {
|
||||
|
||||
.articles-list-tile h2 {
|
||||
margin-bottom: 30px;
|
||||
transform: translateX( +15px);
|
||||
}
|
||||
|
||||
.articles-list-tile {
|
||||
|
||||
@@ -26,8 +26,44 @@
|
||||
<div class="admin-dashboard-tile">
|
||||
<h2>Admin Dashboard</h2>
|
||||
<div class="admin-actions">
|
||||
<button (click)="openModal('create')">Create User</button>
|
||||
<button (click)="openModal('edit')">Edit User</button>
|
||||
<button (click)="openModal('delete')">Delete User</button>
|
||||
<button class="logout" (click)="logout()">Logout</button>
|
||||
</div>
|
||||
|
||||
<!-- User Actions Modal -->
|
||||
<app-modal
|
||||
[isOpen]="isModalOpen"
|
||||
[title]="modalTitle"
|
||||
(onClose)="closeModal()"
|
||||
>
|
||||
<ng-container *ngIf="modalAction === 'create'">
|
||||
<form (submit)="createUser(createUserForm)" #createUserForm="ngForm" id="createUserForm">
|
||||
<input id="createUsername" ngModel required name="createUsername" placeholder="Username"/>
|
||||
<input id="createPassword" type="password" ngModel required name="createPassword" placeholder="Password"/>
|
||||
|
||||
<button type="submit" [disabled]="createUserForm.invalid">Create User</button>
|
||||
</form>
|
||||
</ng-container>
|
||||
|
||||
<ng-container *ngIf="modalAction === 'edit'">
|
||||
<form (submit)="editUser(editUserForm)" #editUserForm="ngForm" id="editUserForm">
|
||||
<input id="editUserId" type="number" min="1" ngModel required name="editUserId" placeholder="User ID"/>
|
||||
<input id="editUsername" ngModel name="editUsername" placeholder="New Username (Optional)"/>
|
||||
<input id="editPassword" ngModel type="password" name="editPassword" placeholder="New Password (Optional)"/>
|
||||
<button type="submit" [disabled]="editUserForm.invalid">Edit User</button>
|
||||
</form>
|
||||
</ng-container>
|
||||
|
||||
<ng-container *ngIf="modalAction === 'delete'">
|
||||
<form (submit)="deleteUser(deleteUserForm)" #deleteUserForm="ngForm" id="deleteUserForm">
|
||||
<input id="deleteUserId" type="number" min="1" required name="deleteUserId" ngModel placeholder="User ID"/>
|
||||
|
||||
<button type="submit" [disabled]="deleteUserForm.invalid">Delete User</button>
|
||||
</form>
|
||||
</ng-container>
|
||||
</app-modal>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -57,7 +93,55 @@
|
||||
</form>
|
||||
</div>
|
||||
<div class="articles-list-tile">
|
||||
<h2>Articles</h2>
|
||||
<h2>
|
||||
{{ isViewingArticles ? 'Articles' : 'Users' }}
|
||||
<button (click)="toggleView()" class="toggle-button">
|
||||
<i class="material-icons">{{ isViewingArticles ? 'person' : 'article' }}</i>
|
||||
</button>
|
||||
</h2>
|
||||
|
||||
<div *ngIf="isViewingArticles">
|
||||
<div *ngIf="noArticles">
|
||||
<p>No articles available.</p>
|
||||
</div>
|
||||
<div *ngIf="!noArticles">
|
||||
<ul>
|
||||
<li *ngFor="let article of articles" (click)="navigateToArticle(article.slug)">
|
||||
<strong>ID {{ article.id }}</strong> - {{ article.title }}
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Display users list -->
|
||||
<div *ngIf="!isViewingArticles">
|
||||
<div *ngIf="users.length === 0">
|
||||
<p>No users available.</p>
|
||||
</div>
|
||||
<div *ngIf="users.length > 0">
|
||||
<ul>
|
||||
<li *ngFor="let user of users">
|
||||
<strong>ID {{ user.id }}</strong> - {{ user.username }}
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</ng-container>
|
||||
</div>
|
||||
|
||||
<!-- Right Section: Articles List Tile -->
|
||||
<ng-container *ngIf="!isMobile">
|
||||
<div class="right-section">
|
||||
<div class="articles-list-tile">
|
||||
<h2>
|
||||
{{ isViewingArticles ? 'Articles' : 'Users' }}
|
||||
<button (click)="toggleView()" class="toggle-button">
|
||||
<i class="material-icons">{{ isViewingArticles ? 'person' : 'article' }}</i>
|
||||
</button>
|
||||
</h2>
|
||||
|
||||
<div *ngIf="isViewingArticles">
|
||||
<div *ngIf="noArticles">
|
||||
<p>No articles available.</p>
|
||||
</div>
|
||||
@@ -69,23 +153,19 @@
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</ng-container>
|
||||
</div>
|
||||
|
||||
<!-- Right Section: Articles List Tile -->
|
||||
<ng-container *ngIf="!isMobile">
|
||||
<div class="right-section">
|
||||
<div class="articles-list-tile">
|
||||
<h2>Articles</h2>
|
||||
<div *ngIf="noArticles">
|
||||
<p>No articles available.</p>
|
||||
</div>
|
||||
<div *ngIf="!noArticles">
|
||||
<ul>
|
||||
<li *ngFor="let article of articles" (click)="navigateToArticle(article.slug)">
|
||||
<strong>ID {{ article.id }}</strong> - {{ article.title }}
|
||||
</li>
|
||||
</ul>
|
||||
<!-- Display users list -->
|
||||
<div *ngIf="!isViewingArticles">
|
||||
<div *ngIf="users.length === 0">
|
||||
<p>No users available.</p>
|
||||
</div>
|
||||
<div *ngIf="users.length > 0">
|
||||
<ul>
|
||||
<li *ngFor="let user of users">
|
||||
<strong>ID {{ user.id }}</strong> - {{ user.username }}
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import {Component, OnInit} from '@angular/core';
|
||||
import {Router} from '@angular/router';
|
||||
import {ArticleService} from '../services/article.service';
|
||||
import {UserService} from '../services/user.service';
|
||||
import {NgFor, NgIf, NgStyle} from '@angular/common';
|
||||
import { Observer} from 'rxjs';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {DeviceDetectorService} from '../services/device-detector.service';
|
||||
import {Title} from '@angular/platform-browser';
|
||||
import {ModalComponent} from '../shared/modal/modal.component';
|
||||
|
||||
@Component({
|
||||
selector: 'app-admin',
|
||||
@@ -15,19 +17,36 @@ import {Title} from '@angular/platform-browser';
|
||||
NgIf,
|
||||
NgFor,
|
||||
FormsModule,
|
||||
NgStyle
|
||||
NgStyle,
|
||||
ModalComponent
|
||||
]
|
||||
})
|
||||
export class AdminComponent implements OnInit {
|
||||
articles: any[] = [];
|
||||
users: any[] = [];
|
||||
noArticles: boolean = false;
|
||||
|
||||
isMobile: boolean = false;
|
||||
|
||||
title: string = '';
|
||||
content: string = '';
|
||||
|
||||
modalTitle: string = '';
|
||||
modalAction: string = '';
|
||||
isModalOpen: boolean = false;
|
||||
isViewingArticles: boolean = true;
|
||||
|
||||
userData = {
|
||||
username: '',
|
||||
password: '',
|
||||
id: '',
|
||||
};
|
||||
|
||||
token: string | null = localStorage.getItem('token');
|
||||
|
||||
constructor(
|
||||
private articleService: ArticleService,
|
||||
private userService: UserService,
|
||||
private router: Router,
|
||||
private deviceService: DeviceDetectorService,
|
||||
private titleService: Title
|
||||
@@ -41,7 +60,8 @@ export class AdminComponent implements OnInit {
|
||||
this.navigateToLogin();
|
||||
return;
|
||||
}
|
||||
this.loadArticles()
|
||||
this.loadArticles();
|
||||
this.loadArticles();
|
||||
}
|
||||
|
||||
navigateToLogin() {
|
||||
@@ -77,11 +97,11 @@ export class AdminComponent implements OnInit {
|
||||
(response) => {
|
||||
alert('Article created successfully!');
|
||||
this.loadArticles();
|
||||
console.log(response); // You can log the response or take further actions like redirecting
|
||||
console.log(response);
|
||||
},
|
||||
(error) => {
|
||||
alert('Error creating article');
|
||||
console.error(error); // Log the error for debugging
|
||||
console.error(error);
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -105,7 +125,6 @@ export class AdminComponent implements OnInit {
|
||||
return;
|
||||
}
|
||||
|
||||
// Prompt for title and content with placeholders
|
||||
const newTitle = form.value.editTitle || form.value.editTitleMobile;
|
||||
const newContent = form.value.editContent || form.value.editContentMobile;
|
||||
|
||||
@@ -121,11 +140,13 @@ export class AdminComponent implements OnInit {
|
||||
},
|
||||
(error) => {
|
||||
alert(`Error updating article ${id}: ${error.message}`);
|
||||
console.error('Error:', error)
|
||||
}
|
||||
);
|
||||
},
|
||||
(error: { message: any; }) => {
|
||||
alert(`Failed to fetch article with ID ${id}: ${error.message}`);
|
||||
console.error('Error:', error)
|
||||
}
|
||||
);
|
||||
} else {
|
||||
@@ -149,6 +170,7 @@ export class AdminComponent implements OnInit {
|
||||
},
|
||||
(error) => {
|
||||
alert(`Error deleting article ${articleId}: ${error.message}`);
|
||||
console.error('Error deleting article:', error)
|
||||
}
|
||||
);
|
||||
} else {
|
||||
@@ -157,7 +179,18 @@ export class AdminComponent implements OnInit {
|
||||
}
|
||||
|
||||
navigateToArticle(slug: string): void {
|
||||
this.router.navigate([`/blog/${slug}`]);
|
||||
this.router.navigate([`/blog/${slug}`]).then(
|
||||
(success) => {
|
||||
if (success) {
|
||||
console.log('Navigation to article succeeded');
|
||||
} else {
|
||||
console.warn('Navigation to article failed');
|
||||
}
|
||||
},
|
||||
(error) => {
|
||||
console.error('Navigation error:', error);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
loadArticles(): void {
|
||||
@@ -176,8 +209,148 @@ export class AdminComponent implements OnInit {
|
||||
this.articleService.getAllArticles().subscribe(articleObserver);
|
||||
}
|
||||
|
||||
loadUsers() {
|
||||
this.userService.getAllUsers(this.token).subscribe((data: any[]) => {
|
||||
this.users = data;
|
||||
});
|
||||
}
|
||||
|
||||
toggleView() {
|
||||
this.isViewingArticles = !this.isViewingArticles;
|
||||
if (!this.isViewingArticles && this.users.length === 0) {
|
||||
this.loadUsers();
|
||||
this.loadArticles();
|
||||
}
|
||||
}
|
||||
|
||||
logout() {
|
||||
localStorage.removeItem('token');
|
||||
this.navigateToLogin();
|
||||
}
|
||||
|
||||
openModal(action: string) {
|
||||
this.isModalOpen = true;
|
||||
this.modalAction = action;
|
||||
|
||||
switch (action) {
|
||||
case 'create':
|
||||
this.modalTitle = 'Create User';
|
||||
break;
|
||||
case 'edit':
|
||||
this.modalTitle = 'Edit User';
|
||||
break;
|
||||
case 'delete':
|
||||
this.modalTitle = 'Delete User';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
closeModal() {
|
||||
this.isModalOpen = false;
|
||||
this.userData = {
|
||||
username: '',
|
||||
password: '',
|
||||
id: '',
|
||||
};
|
||||
}
|
||||
|
||||
createUser(form: any): void {
|
||||
if (!this.token) {
|
||||
this.navigateToLogin();
|
||||
return;
|
||||
}
|
||||
|
||||
const username = form.value.createUsername;
|
||||
const password = form.value.createPassword;
|
||||
|
||||
if (!username || !password) {
|
||||
alert('Both username and password are required!');
|
||||
return;
|
||||
}
|
||||
|
||||
this.userService.createUser(this.token, username, password).subscribe(
|
||||
(response) => {
|
||||
alert('User created successfully!');
|
||||
this.loadUsers();
|
||||
console.log(response);
|
||||
},
|
||||
(error) => {
|
||||
alert('Error creating user');
|
||||
console.error(error);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
editUser(form: any): void {
|
||||
if (!this.token) {
|
||||
this.navigateToLogin();
|
||||
return;
|
||||
}
|
||||
const userId = form.value.editUserId;
|
||||
|
||||
if (userId && !isNaN(Number(userId))) {
|
||||
const id = Number(userId);
|
||||
|
||||
this.userService.getUserById(id, this.token).subscribe(
|
||||
(user) => {
|
||||
if (!user) {
|
||||
alert(`User with ID ${id} not found.`);
|
||||
return;
|
||||
}
|
||||
|
||||
const newUsername = form.value.editUsername;
|
||||
const newPassword = form.value.editPassword;
|
||||
|
||||
// Use current values if left blank
|
||||
const updatedUsername = newUsername?.trim();
|
||||
const updatedPassword = newPassword?.trim();
|
||||
|
||||
this.userService.editUser(id, this.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)
|
||||
}
|
||||
);
|
||||
},
|
||||
(error: { message: any; }) => {
|
||||
alert(`Failed to fetch user with ID ${id}: ${error.message}`);
|
||||
console.error('Error:', error)
|
||||
}
|
||||
);
|
||||
} else {
|
||||
alert('Invalid input. Please enter a valid article ID.');
|
||||
}
|
||||
this.closeModal();
|
||||
}
|
||||
|
||||
deleteUser(form: any): void {
|
||||
if (!this.token) {
|
||||
this.navigateToLogin();
|
||||
return;
|
||||
}
|
||||
|
||||
const userId = form.value.deleteUserId;
|
||||
|
||||
if (userId && !isNaN(Number(userId))) {
|
||||
this.userService.deleteUser(userId, this.token).subscribe(
|
||||
() => {
|
||||
alert(`User ${userId} deleted successfully`);
|
||||
this.loadUsers();
|
||||
},
|
||||
(error) => {
|
||||
alert(`Error deleting user ${userId}: ${error.message}`);
|
||||
console.error('Error deleting user:', error)
|
||||
}
|
||||
);
|
||||
} else {
|
||||
alert('Invalid input. Please enter a valid user ID.');
|
||||
console.error('Invalid input:', userId);
|
||||
}
|
||||
this.closeModal();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user