merged shortener page into admin page, refactored into modal
This commit is contained in:
1 parent
d1d72022da
commit
6110248668
14 files changed
+363
-337
No files matched your search
+1
-1
@@ -43,7 +43,7 @@
|
||||
},
|
||||
{
|
||||
"type": "anyComponentStyle",
|
||||
"maximumWarning": "4kB",
|
||||
"maximumWarning": "5kB",
|
||||
"maximumError": "8kB"
|
||||
}
|
||||
],
|
||||
|
||||
@@ -296,7 +296,7 @@ button {
|
||||
background-color: #8a6bbf;
|
||||
color: white;
|
||||
padding: 12px 22px;
|
||||
margin: 10px;
|
||||
margin-right: 15px;
|
||||
border: none;
|
||||
border-radius: 25px;
|
||||
cursor: pointer;
|
||||
@@ -335,7 +335,8 @@ button.toggle-button:hover:active, button.toggle-button:hover {
|
||||
|
||||
button.logout {
|
||||
background-color: #713252;
|
||||
margin: 0 0 0 5px;
|
||||
margin: 0;
|
||||
padding: 12px 16px;
|
||||
}
|
||||
|
||||
button.logout:hover {
|
||||
|
||||
@@ -2,15 +2,16 @@
|
||||
<!-- Main Section -->
|
||||
<div class="main-section" [ngStyle]="{'width': isNarrow ? '100%' : 'calc(100% - 340px)'}">
|
||||
<div class="dashboard-titlebar" [ngStyle]="{'justify-content': isNarrow ? 'center' : 'space-between'}">
|
||||
<img alt="Logo" src="/assets/logo_m.svg" [routerLink]="'/'">
|
||||
@if (!isNarrow) {
|
||||
<img alt="Logo" src="/assets/logo_m.svg" [routerLink]="'/'">
|
||||
<div class="title">
|
||||
<h2>Admin Dashboard</h2>
|
||||
</div>
|
||||
}
|
||||
<div class="admin-actions">
|
||||
<button (click)="openShortenerModal()">Short URLs</button>
|
||||
<button (click)="openUserModal()">Manage Users</button>
|
||||
<button class="logout" (click)="this.authService.logout()">Logout</button>
|
||||
<button class="logout" (click)="this.authService.logout()"><i class="fa-solid fa-arrow-right-from-bracket"></i></button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -129,7 +130,7 @@
|
||||
|
||||
<!-- User Actions Modal -->
|
||||
<app-modal
|
||||
[isOpen]="isModalOpen"
|
||||
[isOpen]="isUserModalOpen"
|
||||
[title]="modalTitle"
|
||||
(onClose)="closeModal()"
|
||||
>
|
||||
@@ -187,3 +188,11 @@
|
||||
</button>
|
||||
</form>
|
||||
</app-modal>
|
||||
|
||||
<app-shortener
|
||||
[isOpen]="isShortenerModalOpen"
|
||||
[title]="modalTitle"
|
||||
(onClose)="closeShortenerModal()"
|
||||
>
|
||||
|
||||
</app-shortener>
|
||||
@@ -9,6 +9,7 @@ import {ViewportService} from '../shared/services/viewport.service';
|
||||
import {Title} from '@angular/platform-browser';
|
||||
import {ModalComponent} from '../shared/modal/modal.component';
|
||||
import {AuthService} from '../shared/services/auth.service';
|
||||
import {ShortenerComponent} from '../shared/shortener/shortener.component';
|
||||
|
||||
@Component({
|
||||
selector: 'app-admin',
|
||||
@@ -19,7 +20,8 @@ import {AuthService} from '../shared/services/auth.service';
|
||||
NgStyle,
|
||||
ModalComponent,
|
||||
TitleCasePipe,
|
||||
RouterLink
|
||||
RouterLink,
|
||||
ShortenerComponent
|
||||
]
|
||||
})
|
||||
export class AdminComponent implements OnInit, OnDestroy {
|
||||
@@ -35,10 +37,12 @@ export class AdminComponent implements OnInit, OnDestroy {
|
||||
content: string = '';
|
||||
|
||||
modalTitle: string = '';
|
||||
isModalOpen: boolean = false;
|
||||
isUserModalOpen: boolean = false;
|
||||
userMode: 'create' | 'edit' | 'delete' = 'create';
|
||||
isUserDropdownOpen = false;
|
||||
|
||||
isShortenerModalOpen: boolean = false;
|
||||
|
||||
userData = {
|
||||
username: '',
|
||||
password: '',
|
||||
@@ -72,7 +76,7 @@ export class AdminComponent implements OnInit, OnDestroy {
|
||||
}
|
||||
|
||||
startTokenValidation() {
|
||||
this.tokenValidationInterval = interval(300000).subscribe(() => {
|
||||
this.tokenValidationInterval = interval(60000).subscribe(() => {
|
||||
this.authService.validateToken().subscribe();
|
||||
});
|
||||
}
|
||||
@@ -209,12 +213,25 @@ export class AdminComponent implements OnInit, OnDestroy {
|
||||
}
|
||||
|
||||
openUserModal() {
|
||||
this.isModalOpen = true;
|
||||
this.isUserModalOpen = true;
|
||||
this.modalTitle = 'Manage Users';
|
||||
this.userMode = 'create';
|
||||
this.isUserDropdownOpen = false;
|
||||
}
|
||||
|
||||
closeModal() {
|
||||
this.isUserModalOpen = false;
|
||||
this.userData = {
|
||||
username: '',
|
||||
password: '',
|
||||
id: '',
|
||||
};
|
||||
}
|
||||
|
||||
closeShortenerModal() {
|
||||
this.isShortenerModalOpen = false;
|
||||
}
|
||||
|
||||
toggleUserDropdown(): void {
|
||||
this.isUserDropdownOpen = !this.isUserDropdownOpen;
|
||||
}
|
||||
@@ -254,15 +271,6 @@ export class AdminComponent implements OnInit, OnDestroy {
|
||||
}
|
||||
}
|
||||
|
||||
closeModal() {
|
||||
this.isModalOpen = false;
|
||||
this.userData = {
|
||||
username: '',
|
||||
password: '',
|
||||
id: '',
|
||||
};
|
||||
}
|
||||
|
||||
createUserFromValues(username: string, password: string): void {
|
||||
if (!username?.trim() || !password?.trim()) {
|
||||
alert('Both username and password are required!');
|
||||
@@ -387,4 +395,8 @@ export class AdminComponent implements OnInit, OnDestroy {
|
||||
this.deleteArticle(articleId);
|
||||
}
|
||||
}
|
||||
|
||||
openShortenerModal() {
|
||||
this.isShortenerModalOpen = true;
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,6 @@ import {SocialsComponent} from './socials/socials.component';
|
||||
// import {ServicesComponent} from './services/services.component';
|
||||
// import {TosComponent} from './tos/tos.component';
|
||||
import {ImprintComponent} from './imprint/imprint.component';
|
||||
import {ShortenerComponent} from './shortener/shortener.component';
|
||||
import {PrivacyComponent} from './privacy/privacy.component';
|
||||
import {SuccessComponent} from './checkout/success/success.component';
|
||||
import {CancelComponent} from './checkout/cancel/cancel.component';
|
||||
@@ -28,7 +27,6 @@ export const routes: Routes = [
|
||||
{ path: 'privacy', component: PrivacyComponent },
|
||||
{ path: 'imprint', component: ImprintComponent },
|
||||
{ path: 'blog/:slug', component: ArticlePageComponent },
|
||||
{ path: 'shortener', component: ShortenerComponent },
|
||||
{ path: 'checkout/success', pathMatch: 'full', component: SuccessComponent },
|
||||
{ path: 'checkout/cancel', pathMatch: 'full', component: CancelComponent },
|
||||
{ path: '**', pathMatch: 'full', component: NotFoundComponent }
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import {Component, Inject, OnInit, PLATFORM_ID} from '@angular/core';
|
||||
import {Component, OnInit} from '@angular/core';
|
||||
import {AuthService} from '../shared/services/auth.service';
|
||||
import {ActivatedRoute, Router} from '@angular/router';
|
||||
import {Router} from '@angular/router';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {ViewportService} from '../shared/services/viewport.service';
|
||||
import { Title } from '@angular/platform-browser';
|
||||
import { take } from 'rxjs/operators';
|
||||
import {ParallaxComponent} from '../shared/parallax/parallax.component';
|
||||
|
||||
@Component({
|
||||
@@ -21,35 +20,18 @@ export class LoginComponent implements OnInit {
|
||||
username = '';
|
||||
password = '';
|
||||
errorMessage = '';
|
||||
private readonly allowedAdminRoutes = new Set([
|
||||
'/admin',
|
||||
'/shortener'
|
||||
]);
|
||||
isNarrow = false;
|
||||
redirectUrl = '/admin';
|
||||
|
||||
constructor(
|
||||
private authService: AuthService,
|
||||
private router: Router,
|
||||
private route: ActivatedRoute,
|
||||
private deviceService: ViewportService,
|
||||
private titleService: Title,
|
||||
@Inject(PLATFORM_ID) private platformId: Object
|
||||
) {}
|
||||
|
||||
ngOnInit() {
|
||||
this.titleService.setTitle('Login | itzMiney')
|
||||
this.isNarrow = this.deviceService.isNarrow;
|
||||
|
||||
this.route.queryParams.pipe(take(1)).subscribe(params => {
|
||||
const raw = params['redirectUrl'];
|
||||
|
||||
if (typeof raw === 'string' && this.allowedAdminRoutes.has(raw)) {
|
||||
this.redirectUrl = raw;
|
||||
} else {
|
||||
this.redirectUrl = '/admin';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
onLogin() {
|
||||
@@ -57,7 +39,7 @@ export class LoginComponent implements OnInit {
|
||||
|
||||
this.authService.login(this.username.trim(), this.password).subscribe({
|
||||
next: () => {
|
||||
void this.router.navigateByUrl(this.redirectUrl);
|
||||
void this.router.navigate(['/admin']);
|
||||
},
|
||||
error: (err) => {
|
||||
this.errorMessage =
|
||||
|
||||
@@ -26,12 +26,12 @@
|
||||
|
||||
.close-btn {
|
||||
position: absolute;
|
||||
top: 4px;
|
||||
right: 8px;
|
||||
top: 25px;
|
||||
right: 25px;
|
||||
background: none;
|
||||
color: white;
|
||||
border: none;
|
||||
font-size: 28px;
|
||||
font-size: 40px;
|
||||
cursor: pointer;
|
||||
transition: all ease-in-out 0.2s;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
@if (isOpen) {
|
||||
<div class="modal-backdrop">
|
||||
<div class="modal">
|
||||
<button class="close-btn" (click)="closeModal()"><i class="fa-solid fa-xmark"></i></button>
|
||||
<div class="modal-backdrop" (click)="closeModal()">
|
||||
<button class="close-btn" (click)="closeModal()"><i class="fa-solid fa-xmark"></i></button>
|
||||
<div class="modal" (click)="$event.stopPropagation()">
|
||||
<h2>{{ title }}</h2>
|
||||
<ng-content></ng-content>
|
||||
</div>
|
||||
|
||||
@@ -18,10 +18,10 @@ export class AuthService {
|
||||
return localStorage.getItem('token');
|
||||
}
|
||||
|
||||
login(username: string, password: string): Observable<any> {
|
||||
return this.http.post(this.apiUrl, { username, password }).pipe(
|
||||
tap((response: any) => {
|
||||
if (response?.token) localStorage.setItem('token', response.token);
|
||||
login(username: string, password: string): Observable<{ token: string }> {
|
||||
return this.http.post<{ token: string }>(this.apiUrl, { username, password }).pipe(
|
||||
tap(response => {
|
||||
localStorage.setItem('token', response.token);
|
||||
})
|
||||
);
|
||||
}
|
||||
@@ -29,60 +29,47 @@ export class AuthService {
|
||||
logout(): void {
|
||||
if (this.redirectingToLogin) return;
|
||||
|
||||
localStorage.removeItem('token');
|
||||
|
||||
if (this.router.url.startsWith('/login')) {
|
||||
localStorage.removeItem('token');
|
||||
return;
|
||||
}
|
||||
|
||||
this.redirectingToLogin = true;
|
||||
|
||||
localStorage.removeItem('token');
|
||||
|
||||
const tree = this.router.parseUrl(this.router.url);
|
||||
delete tree.queryParams?.['redirectUrl'];
|
||||
const cleanRedirectUrl = tree.toString();
|
||||
|
||||
void this.router.navigate(['/login'], {
|
||||
queryParams: { redirectUrl: cleanRedirectUrl },
|
||||
replaceUrl: true
|
||||
}).finally(() => {
|
||||
void this.router.navigate(['/login'], { replaceUrl: true }).finally(() => {
|
||||
this.redirectingToLogin = false;
|
||||
});
|
||||
}
|
||||
|
||||
private isJwtNotExpired(token: string | null): boolean {
|
||||
if (!token) return false;
|
||||
|
||||
try {
|
||||
const payload = JSON.parse(atob(token.split('.')[1]));
|
||||
const expMs = payload.exp * 1000;
|
||||
return expMs > Date.now();
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
isTokenValid(): Observable<boolean> {
|
||||
validateToken(): Observable<boolean> {
|
||||
const token = this.token;
|
||||
|
||||
if (!this.isJwtNotExpired(token)) {
|
||||
if (!token) {
|
||||
this.logout();
|
||||
return of(false);
|
||||
}
|
||||
|
||||
return this.http.get('/api/users', {
|
||||
headers: { Authorization: `Bearer ${token}` }
|
||||
try {
|
||||
const payload = JSON.parse(atob(token.split('.')[1]));
|
||||
const isValid = payload.exp * 1000;
|
||||
|
||||
if (!isValid) {
|
||||
this.logout();
|
||||
return of(false);
|
||||
}
|
||||
} catch {
|
||||
this.logout();
|
||||
return of(false);
|
||||
}
|
||||
|
||||
return this.http.get('api/users', {
|
||||
headers: {Authorization: `Bearer ${token}`}
|
||||
}).pipe(
|
||||
map(() => true),
|
||||
catchError(() => of(false))
|
||||
);
|
||||
}
|
||||
|
||||
validateToken(): Observable<boolean> {
|
||||
return this.isTokenValid().pipe(
|
||||
tap(valid => {
|
||||
if (!valid) {
|
||||
this.logout();
|
||||
}
|
||||
catchError(() => {
|
||||
this.logout();
|
||||
return of(false);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
+66
-28
@@ -1,3 +1,16 @@
|
||||
.container-wrapper {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
height: 100vh;
|
||||
width: 100%;
|
||||
background: rgba(0, 0, 0, 0.5);
|
||||
z-index: 1000;
|
||||
}
|
||||
|
||||
.container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
@@ -5,11 +18,13 @@
|
||||
align-items: center;
|
||||
min-width: 300px;
|
||||
padding: 25px;
|
||||
background-color: #ffffff;
|
||||
background-color: #202025;
|
||||
color: #fff;
|
||||
border-radius: 35px;
|
||||
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
||||
max-height: 90vh;
|
||||
overflow: hidden;
|
||||
z-index: 1001;
|
||||
}
|
||||
|
||||
.container-content {
|
||||
@@ -17,18 +32,9 @@
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.container-wrapper {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
height: 100vh;
|
||||
background-color: #592840;
|
||||
}
|
||||
|
||||
h1 {
|
||||
margin-top: 40px;
|
||||
text-align: center;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
h3 {
|
||||
@@ -47,23 +53,36 @@ h3 {
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
input {
|
||||
color: white;
|
||||
background: #26262c;
|
||||
border: none;
|
||||
padding: 10px 20px;
|
||||
border-radius: 25px;
|
||||
transition: all ease-in-out 0.2s;
|
||||
box-sizing: border-box;
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
input:focus {
|
||||
outline: none;
|
||||
background: #2b2b32;
|
||||
}
|
||||
|
||||
input[type="url"],
|
||||
input[type="text"] {
|
||||
min-width: 240px;
|
||||
padding: 10px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 25px;
|
||||
font-size: 1rem;
|
||||
box-sizing: border-box;
|
||||
min-width: 250px;
|
||||
}
|
||||
|
||||
input[type="number"] {
|
||||
max-width: 80px;
|
||||
padding: 10px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 25px;
|
||||
font-size: 1rem;
|
||||
box-sizing: border-box;
|
||||
-moz-appearance: textfield;
|
||||
}
|
||||
|
||||
input[type="number"]::-webkit-inner-spin-button,
|
||||
input[type="number"]::-webkit-inner-spin-button {
|
||||
-webkit-appearance: none;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
button {
|
||||
@@ -75,12 +94,12 @@ button {
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
background-color: #007bff;
|
||||
background-color: #8a6bbf;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-danger {
|
||||
background-color: #dc3545;
|
||||
background-color: #BA243AFF;
|
||||
color: white;
|
||||
}
|
||||
|
||||
@@ -104,10 +123,9 @@ button:hover {
|
||||
|
||||
|
||||
.url-list-items li {
|
||||
background-color: #fff;
|
||||
background: #26262c;
|
||||
padding: 10px 15px;
|
||||
row-gap: 10px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 20px;
|
||||
line-height: 1.5rem;
|
||||
}
|
||||
@@ -117,7 +135,7 @@ li + li {
|
||||
}
|
||||
|
||||
.url-list-items a {
|
||||
color: #007bff;
|
||||
color: #D688ECFF;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
@@ -127,7 +145,7 @@ li + li {
|
||||
|
||||
.copy-btn {
|
||||
background: transparent;
|
||||
color: #007bff;
|
||||
color: #D688ECFF;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
font-size: 1rem;
|
||||
@@ -137,5 +155,25 @@ li + li {
|
||||
}
|
||||
|
||||
.copy-btn:active, .copy-btn:focus {
|
||||
color: #3e6c49;
|
||||
color: #7dc68e;
|
||||
}
|
||||
|
||||
.close-btn {
|
||||
position: absolute;
|
||||
top: 25px;
|
||||
right: 25px;
|
||||
background: none;
|
||||
color: white;
|
||||
border: none;
|
||||
font-size: 40px;
|
||||
cursor: pointer;
|
||||
transition: all ease-in-out 0.2s;
|
||||
}
|
||||
|
||||
.close-btn:hover {
|
||||
color: #ebe1fa;
|
||||
}
|
||||
|
||||
.close-btn:active {
|
||||
color: #a172e8;
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
@if (isOpen) {
|
||||
<div class="container-wrapper" (click)="closeShortenerModal()">
|
||||
<div class="container" (click)="$event.stopPropagation()">
|
||||
<button class="close-btn" (click)="closeShortenerModal()"><i class="fa-solid fa-xmark"></i></button>
|
||||
<div class="container-content">
|
||||
<h1>URL Shortener</h1>
|
||||
|
||||
<!-- URL Creation Form -->
|
||||
<div class="form-section">
|
||||
<form (ngSubmit)="createShortURL(shortURLForm)" #shortURLForm="ngForm">
|
||||
<div class="form-group">
|
||||
<input
|
||||
type="url"
|
||||
id="og_url"
|
||||
name="og_url"
|
||||
ngModel
|
||||
class="form-control"
|
||||
placeholder="Original URL"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<input
|
||||
type="text"
|
||||
id="short_name"
|
||||
name="short_name"
|
||||
ngModel
|
||||
class="form-control"
|
||||
placeholder="Short URL Name (Optional)"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-primary">Shorten URL</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<!-- Delete URL Section -->
|
||||
<div class="form-section">
|
||||
<h3>Delete Short URL</h3>
|
||||
<form (ngSubmit)="deleteShortURL(deleteURLForm)" #deleteURLForm="ngForm">
|
||||
<div class="form-group">
|
||||
<input
|
||||
type="number"
|
||||
id="deleteId"
|
||||
name="deleteId"
|
||||
ngModel
|
||||
class="form-control"
|
||||
placeholder="ID"
|
||||
required
|
||||
/>
|
||||
<button type="submit" class="btn btn-danger">Delete</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<h3>Existing Short URLs</h3>
|
||||
</div>
|
||||
|
||||
<!-- Scrollable URL list -->
|
||||
<div class="url-list-wrapper">
|
||||
@if (noURLs) {
|
||||
<div>
|
||||
<p>No short URLs available.</p>
|
||||
</div>
|
||||
} @else {
|
||||
<ul class="url-list-items">
|
||||
@for (url of urls; track url) {
|
||||
<li>
|
||||
<strong>ID {{ url.id }}</strong> // <strong>URL: </strong>
|
||||
<a [href]="'https://itz.sh/' + url.short_url" target="_blank"
|
||||
rel="noopener">{{ 'https://itz.sh/' + url.short_url }}</a>
|
||||
|
||||
<button type="button" class="copy-btn" (click)="copyShortUrl(url.short_url)"
|
||||
[attr.aria-label]="'Copy ' + ('https://itz.sh/' + url.short_url)" title="Copy Short URL">
|
||||
<span class="link-icon"><i class="fa-solid fa-link"></i></span>
|
||||
</button>
|
||||
<br>
|
||||
<strong>Original: </strong>
|
||||
<a [href]="url.og_url"
|
||||
target="_blank">{{ (url.og_url | slice:0:45) + (url.og_url.length > 40 ? '...' : '') }}</a><br>
|
||||
</li>
|
||||
}
|
||||
</ul>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
import {Component, OnInit, Input, Output, EventEmitter} from '@angular/core';
|
||||
import {ShortenerService} from '../services/shortener.service';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import { SlicePipe } from '@angular/common';
|
||||
import {AuthService} from '../services/auth.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-shortener',
|
||||
imports: [
|
||||
FormsModule,
|
||||
SlicePipe
|
||||
],
|
||||
templateUrl: './shortener.component.html',
|
||||
styleUrl: './shortener.component.css'
|
||||
})
|
||||
export class ShortenerComponent implements OnInit {
|
||||
@Input() isOpen: boolean = false;
|
||||
@Output() onClose = new EventEmitter<void>();
|
||||
|
||||
urls: any[] = [];
|
||||
noURLs: boolean = false;
|
||||
|
||||
og_url: string = '';
|
||||
short_name: string | null = null;
|
||||
deleteId: number | null = null;
|
||||
|
||||
copiedShort: string | null = null;
|
||||
|
||||
constructor(
|
||||
private shortenerService: ShortenerService,
|
||||
private authService: AuthService,
|
||||
) {}
|
||||
|
||||
ngOnInit() {
|
||||
this.loadURLs();
|
||||
}
|
||||
|
||||
loadURLs(): void {
|
||||
const token = this.authService.token;
|
||||
|
||||
if (!token) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.shortenerService.getAllURLs(token).subscribe({
|
||||
next: (data) => {
|
||||
this.urls = data;
|
||||
this.noURLs = data.length === 0;
|
||||
},
|
||||
error: (err) => {
|
||||
console.error('Error loading URLs:', err)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
createShortURL(form: any) {
|
||||
const og_url = form.value.og_url;
|
||||
const short_name = form.value.short_name ?? null;
|
||||
|
||||
if (!og_url) {
|
||||
alert('Original URL Required!');
|
||||
return;
|
||||
}
|
||||
|
||||
const token = this.authService.token;
|
||||
|
||||
if (!token) {
|
||||
return;
|
||||
}
|
||||
|
||||
const req$ = short_name
|
||||
? this.shortenerService.postURL(token, og_url, short_name)
|
||||
: this.shortenerService.postURL(token, og_url);
|
||||
|
||||
req$.subscribe({
|
||||
next: () => {
|
||||
alert('Short URL created successfully!');
|
||||
this.loadURLs();
|
||||
},
|
||||
error: (error) => {
|
||||
alert('Error creating Short URL');
|
||||
console.error(error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
deleteShortURL(form: any) {
|
||||
const id = form.value.deleteId;
|
||||
|
||||
if (!id || isNaN(Number(id))) {
|
||||
alert('Invalid input. Please enter a valid Short URL ID.');
|
||||
return;
|
||||
}
|
||||
|
||||
const token = this.authService.token!;
|
||||
|
||||
if (!token) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.shortenerService.deleteURL(Number(id), token).subscribe({
|
||||
next: () => {
|
||||
alert(`Short URL with ID ${id} deleted successfully`);
|
||||
this.loadURLs();
|
||||
},
|
||||
error: (error) => {
|
||||
alert(`Error deleting Short URL ${id}: ${error.message}`);
|
||||
console.error('Error deleting Short URL:', error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
copyShortUrl(shortCode: string): void {
|
||||
const shortUrl = `https://itz.sh/${shortCode}`;
|
||||
|
||||
navigator.clipboard.writeText(shortUrl).then(() => {
|
||||
this.copiedShort = shortCode;
|
||||
|
||||
setTimeout(() => {
|
||||
if (this.copiedShort === shortCode) this.copiedShort = null;
|
||||
}, 2000);
|
||||
}).catch(() => {
|
||||
});
|
||||
}
|
||||
|
||||
closeShortenerModal() {
|
||||
this.isOpen = false;
|
||||
this.onClose.emit();
|
||||
}
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
<div class="container-wrapper">
|
||||
<div class="container">
|
||||
<div class="container-content">
|
||||
<h1>URL Shortener</h1>
|
||||
|
||||
<!-- URL Creation Form -->
|
||||
<div class="form-section">
|
||||
<form (ngSubmit)="createShortURL(shortURLForm)" #shortURLForm="ngForm">
|
||||
<div class="form-group">
|
||||
<input
|
||||
type="url"
|
||||
id="og_url"
|
||||
name="og_url"
|
||||
ngModel
|
||||
class="form-control"
|
||||
placeholder="Original URL"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<input
|
||||
type="text"
|
||||
id="short_name"
|
||||
name="short_name"
|
||||
ngModel
|
||||
class="form-control"
|
||||
placeholder="Short URL Name (Optional)"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-primary">Shorten URL</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<!-- Delete URL Section -->
|
||||
<div class="form-section">
|
||||
<h3>Delete Short URL</h3>
|
||||
<form (ngSubmit)="deleteShortURL(deleteURLForm)" #deleteURLForm="ngForm">
|
||||
<div class="form-group">
|
||||
<input
|
||||
type="number"
|
||||
id="deleteId"
|
||||
name="deleteId"
|
||||
ngModel
|
||||
class="form-control"
|
||||
placeholder="ID"
|
||||
required
|
||||
/>
|
||||
<button type="submit" class="btn btn-danger">Delete</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<h3>Existing Short URLs</h3>
|
||||
</div>
|
||||
|
||||
<!-- Scrollable URL list -->
|
||||
<div class="url-list-wrapper">
|
||||
@if (noURLs) {
|
||||
<div>
|
||||
<p>No short URLs available.</p>
|
||||
</div>
|
||||
} @else {
|
||||
<ul class="url-list-items">
|
||||
@for (url of urls; track url) {
|
||||
<li>
|
||||
<strong>ID {{url.id}}</strong> // <strong>URL: </strong>
|
||||
<a [href]="'https://itz.sh/' + url.short_url" target="_blank" rel="noopener">{{ 'https://itz.sh/' + url.short_url }}</a>
|
||||
|
||||
<button type="button" class="copy-btn" (click)="copyShortUrl(url.short_url)" [attr.aria-label]="'Copy ' + ('https://itz.sh/' + url.short_url)" title="Copy Short URL">
|
||||
<span class="link-icon"><i class="fa-solid fa-link"></i></span>
|
||||
</button>
|
||||
<br>
|
||||
<strong>Original: </strong>
|
||||
<a [href]="url.og_url" target="_blank">{{ (url.og_url | slice:0:45) + (url.og_url.length > 40 ? '...' : '')}}</a><br>
|
||||
</li>
|
||||
}
|
||||
</ul>
|
||||
}
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
@@ -1,135 +0,0 @@
|
||||
import { Component, OnInit, OnDestroy} from '@angular/core';
|
||||
import {Title} from '@angular/platform-browser';
|
||||
import {ShortenerService} from '../shared/services/shortener.service';
|
||||
import {interval, Subscription, switchMap} from 'rxjs';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import { SlicePipe } from '@angular/common';
|
||||
import {AuthService} from '../shared/services/auth.service';
|
||||
import {filter} from 'rxjs/operators';
|
||||
|
||||
@Component({
|
||||
selector: 'app-shortener',
|
||||
imports: [
|
||||
FormsModule,
|
||||
SlicePipe
|
||||
],
|
||||
templateUrl: './shortener.component.html',
|
||||
styleUrl: './shortener.component.css'
|
||||
})
|
||||
export class ShortenerComponent implements OnInit, OnDestroy {
|
||||
tokenValidationInterval: Subscription = new Subscription();
|
||||
|
||||
urls: any[] = [];
|
||||
noURLs: boolean = false;
|
||||
|
||||
og_url: string = '';
|
||||
short_name: string | null = null;
|
||||
deleteId: number | null = null;
|
||||
|
||||
copiedShort: string | null = null;
|
||||
|
||||
constructor(
|
||||
private shortenerService: ShortenerService,
|
||||
private authService: AuthService,
|
||||
private titleService: Title
|
||||
) {}
|
||||
|
||||
ngOnInit() {
|
||||
this.titleService.setTitle('Shortener | itzMiney')
|
||||
this.loadURLs();
|
||||
this.startTokenValidation();
|
||||
}
|
||||
|
||||
ngOnDestroy() {
|
||||
this.tokenValidationInterval.unsubscribe();
|
||||
}
|
||||
|
||||
startTokenValidation() {
|
||||
this.tokenValidationInterval = interval(300000).subscribe(() => {
|
||||
this.authService.validateToken().subscribe();
|
||||
});
|
||||
}
|
||||
|
||||
loadURLs(): void {
|
||||
this.authService.validateToken().pipe(
|
||||
filter(valid => valid),
|
||||
switchMap(() => this.shortenerService.getAllURLs(this.authService.token!))
|
||||
).subscribe({
|
||||
next: (data) => {
|
||||
this.urls = data;
|
||||
this.noURLs = data.length === 0;
|
||||
},
|
||||
error: (err) => console.error(err)
|
||||
});
|
||||
}
|
||||
|
||||
createShortURL(form: any) {
|
||||
const og_url = form.value.og_url;
|
||||
const short_name = form.value.short_name ?? null;
|
||||
|
||||
if (!og_url) {
|
||||
alert('Original URL Required!');
|
||||
return;
|
||||
}
|
||||
|
||||
this.authService.validateToken().subscribe(valid => {
|
||||
if (!valid) return;
|
||||
|
||||
const token = this.authService.token!;
|
||||
|
||||
const req$ = short_name
|
||||
? this.shortenerService.postURL(token, og_url, short_name)
|
||||
: this.shortenerService.postURL(token, og_url);
|
||||
|
||||
req$.subscribe(
|
||||
() => {
|
||||
alert('Short URL created successfully!');
|
||||
this.loadURLs();
|
||||
},
|
||||
(error) => {
|
||||
alert('Error creating Short URL');
|
||||
console.error(error);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
deleteShortURL(form: any) {
|
||||
const id = form.value.deleteId;
|
||||
|
||||
if (!id || isNaN(Number(id))) {
|
||||
alert('Invalid input. Please enter a valid Short URL ID.');
|
||||
return;
|
||||
}
|
||||
|
||||
this.authService.validateToken().subscribe(valid => {
|
||||
if (!valid) return;
|
||||
|
||||
const token = this.authService.token!;
|
||||
|
||||
this.shortenerService.deleteURL(Number(id), token).subscribe(
|
||||
() => {
|
||||
alert(`Short URL with ID ${id} deleted successfully`);
|
||||
this.loadURLs();
|
||||
},
|
||||
(error) => {
|
||||
alert(`Error deleting Short URL ${id}: ${error.message}`);
|
||||
console.error('Error deleting Short URL:', error);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
copyShortUrl(shortCode: string): void {
|
||||
const shortUrl = `https://itz.sh/${shortCode}`;
|
||||
|
||||
navigator.clipboard.writeText(shortUrl).then(() => {
|
||||
this.copiedShort = shortCode;
|
||||
|
||||
setTimeout(() => {
|
||||
if (this.copiedShort === shortCode) this.copiedShort = null;
|
||||
}, 2000);
|
||||
}).catch(() => {
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user