Added blog functionality, reworked backend api requests

This commit is contained in:
itzMiney committed 2024-12-29 23:32:35 +01:00
1 parent a4b69c5b9a
commit dae95810f2
16 files changed
+696 -171

No files matched your search

+83 -24
View File
@@ -4,27 +4,67 @@
position: fixed;
top: 0;
left: 0;
width: 100vw; /* Full viewport width */
height: 100vh; /* Full viewport height */
z-index: -100; /* Place the background behind content */
}
.admin-container {
background: #fff;
border-radius: 25px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
padding: 40px;
width: 350px;
text-align: center;
margin: 50px auto;
width: 100vw;
height: 100vh;
z-index: -100;
}
.height-container {
min-height: 100vh;
align-content: center;
display: flex;
justify-content: space-between;
align-items: center;
height: 100vh;
width: 100vw;
}
.admin-actions button {
.center-section,
.left-section,
.right-section {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 33.33%;
padding: 30px;
height: auto;
box-sizing: border-box;
}
.admin-dashboard-tile,
.create-article-tile,
.articles-list-tile {
background: #fff;
border-radius: 25px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
padding: 20px;
text-align: center;
max-width: 400px;
width: 100%;
}
.admin-dashboard-tile,
.create-article-tile {
margin-bottom: 30px;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.articles-list-tile {
overflow-y: auto;
max-width: 300px;
}
input,
textarea {
width: 90%;
padding: 10px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 25px;
}
.admin-actions button, .create-article-tile button {
background-color: #bd3948;
color: white;
padding: 12px 20px;
@@ -34,11 +74,11 @@
cursor: pointer;
}
.admin-actions button:hover {
.admin-actions button:hover, .create-article-tile button:hover {
background-color: #e3485a;
}
.logout button {
.admin-actions button.logout {
background-color: #713252;
color: white;
padding: 12px 20px;
@@ -48,18 +88,37 @@
cursor: pointer;
}
.logout button:hover {
.admin-actions button.logout:hover {
background-color: #592840;
}
.articles-list {
margin-top: 20px;
text-align: left;
.articles-list-tile ul {
list-style-type: none;
padding: 0;
overflow-y: auto;
max-height: 100%;
}
.article {
.articles-list-tile li {
border: 1px solid #ccc;
padding: 10px;
margin-bottom: 10px;
border-radius: 4px;
border-radius: 25px;
}
.articles-list-tile li:last-child {
margin-bottom: 0;
}
.articles-list-tile h2 {
margin-bottom: 30px;
}
.articles-list-tile {
padding-bottom: 5px;
}
textarea {
resize: none;
min-height: 100px;
}
+47 -24
View File
@@ -1,29 +1,52 @@
<div id="admin-bg"></div>
<div class="height-container">
<div class="admin-container">
<h2>Admin Dashboard</h2>
<div class="admin-actions">
<button (click)="createArticle()">Create Article</button>
<button (click)="viewArticles()">View Articles</button>
<button (click)="editArticle()">Edit Article</button>
<button (click)="deleteArticle()">Delete Article</button>
</div>
<div id="admin-bg">
<div class="height-container">
<!-- Left Section: Placeholder -->
<div class="left-section">
<!-- Placeholder for future content -->
</div>
<div *ngIf="noArticles">
<p>No articles available.</p> <!-- Show message if no articles -->
</div>
<!-- Center Section: Admin Dashboard and Create Article Tiles -->
<div class="center-section">
<div class="admin-dashboard-tile">
<h2>Admin Dashboard</h2>
<div class="admin-actions">
<button (click)="editArticle()">Edit Article</button>
<button (click)="deleteArticle()">Delete Article</button>
<button class="logout" (click)="logout()">Logout</button>
</div>
</div>
<div *ngIf="!noArticles">
<ul>
<li *ngFor="let article of articles">
<!-- Display article details here -->
{{ article.title }}
</li>
</ul>
</div>
<div class="create-article-tile">
<h2>Create New Article</h2>
<form (ngSubmit)="createArticle()">
<div>
<label for="title">Title:</label>
<input type="text" id="title" [(ngModel)]="title" name="title" required />
</div>
<div>
<label for="content">Content:</label>
<textarea id="content" [(ngModel)]="content" name="content" required></textarea>
</div>
<button type="submit" [disabled]="!title || !content">Create Article</button>
</form>
</div>
</div>
<div class="logout">
<button (click)="logout()">Logout</button>
<!-- Right Section: Articles List Tile -->
<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">
<strong>ID {{ article.id }}</strong> - {{ article.title }}
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
+131 -53
View File
@@ -3,6 +3,7 @@ import {Router} from '@angular/router';
import {ArticleService} from '../services/article.service';
import {NgFor, NgIf} from '@angular/common';
import {Observer} from 'rxjs';
import {FormsModule} from '@angular/forms';
@Component({
selector: 'app-admin',
@@ -10,72 +11,33 @@ import {Observer} from 'rxjs';
styleUrls: ['./admin.component.css'],
imports: [
NgIf,
NgFor
NgFor,
FormsModule,
]
})
export class AdminComponent implements OnInit {
articles: any[] = [];
noArticles: boolean = false;
constructor(private articleService: ArticleService, private router: Router) {}
title: string = '';
content: string = '';
token: string | null = localStorage.getItem('token');
ngOnInit() {
this.fetchArticles();
}
constructor(
private articleService: ArticleService,
private router: Router
) {}
// Fetch articles from backend
fetchArticles() {
const token = localStorage.getItem('token');
if (!token) {
this.router.navigate(['/login']).then(
(success) => {
if (success) {
console.log('Navigation to login succeeded');
} else {
console.warn('Navigation to login failed');
}
},
(error) => {
console.error('Navigation error:', error);
}
);
ngOnInit() {
if (!this.token) {
this.navigateToLogin();
return;
}
const articleObserver: Observer<any> = {
next: (data) => {
this.articles = data;
this.noArticles = this.articles.length === 0;
},
error: (error) => {
console.error('Failed to load articles', error);
},
complete: () => {
console.log('Article loading complete');
}
};
this.articleService.getArticles(token).subscribe(articleObserver);
this.loadArticles()
}
createArticle() {
alert('Create article functionality goes here');
}
editArticle() {
alert('Delete article functionality goes here');
}
deleteArticle() {
alert('Delete article functionality goes here');
}
viewArticles() {
this.fetchArticles();
}
logout() {
localStorage.removeItem('token');
navigateToLogin() {
this.router.navigate(['/login']).then(
(success) => {
if (success) {
@@ -89,4 +51,120 @@ export class AdminComponent implements OnInit {
}
);
}
createArticle() {
if (!this.token) {
this.navigateToLogin();
return;
}
if (!this.title || !this.content) {
alert('Both title and content are required!');
return;
}
this.articleService.postArticle(this.token, this.title, this.content).subscribe(
(response) => {
alert('Article created successfully!');
this.loadArticles();
console.log(response); // You can log the response or take further actions like redirecting
},
(error) => {
alert('Error creating article');
console.error(error); // Log the error for debugging
}
);
}
editArticle(): void {
if (!this.token) {
this.navigateToLogin();
return;
}
const articleId = prompt('Enter the article ID to edit:');
if (articleId && !isNaN(Number(articleId))) {
const id = Number(articleId);
// Fetch the current article to get existing values
this.articleService.getArticleById(id).subscribe(
(article) => {
if (!article) {
alert(`Article with ID ${id} not found.`);
return;
}
// Prompt for title and content with placeholders
const newTitle = prompt('Enter the new title (Leave blank to leave as is):');
const newContent = prompt('Enter the new content (Leave blank to leave as is):');
// Use current values if left blank
const updatedTitle = newTitle?.trim() || article.title;
const updatedContent = newContent?.trim() || article.content;
this.articleService.editArticle(id, this.token, updatedTitle, updatedContent).subscribe(
(response) => {
alert(`Article ${id} updated successfully!`);
console.log('Updated Article:', response);
this.loadArticles();
},
(error) => {
alert(`Error updating article ${id}: ${error.message}`);
}
);
},
(error: { message: any; }) => {
alert(`Failed to fetch article with ID ${id}: ${error.message}`);
}
);
} else {
alert('Invalid input. Please enter a valid article ID.');
}
}
deleteArticle(): void {
if (!this.token) {
this.navigateToLogin();
return;
}
const articleId = prompt('Enter the article ID to delete:');
if (articleId && !isNaN(Number(articleId))) {
const id = Number(articleId);
this.articleService.deleteArticle(id, this.token).subscribe(
() => {
alert(`Article ${id} deleted successfully`);
this.loadArticles();
},
(error) => {
alert(`Error deleting article ${id}: ${error.message}`);
}
);
} else {
alert('Invalid input. Please enter a valid article ID.');
}
}
loadArticles(): void {
const articleObserver: Observer<any> = {
next: (data) => {
this.articles = data;
this.noArticles = this.articles.length === 0;
},
error: (error) => {
console.error('Failed to load articles', error);
},
complete: () => {
console.log('Article loading complete');
}
};
this.articleService.getAllArticles().subscribe(articleObserver);
}
logout() {
localStorage.removeItem('token');
this.navigateToLogin();
}
}