forked from ttc/micro-frontend
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 78ce686f97 | |||
| f25488370a |
@@ -61,6 +61,7 @@
|
|||||||
"bootstrap": "^5.3.8",
|
"bootstrap": "^5.3.8",
|
||||||
"chart.js": "^4.5.1",
|
"chart.js": "^4.5.1",
|
||||||
"dotenv": "^17.2.3",
|
"dotenv": "^17.2.3",
|
||||||
|
"jwt-decode": "^4.0.0",
|
||||||
"ng2-charts": "^6.0.1",
|
"ng2-charts": "^6.0.1",
|
||||||
"postcss": "^8.5.6",
|
"postcss": "^8.5.6",
|
||||||
"rxjs": "~7.8.0",
|
"rxjs": "~7.8.0",
|
||||||
@@ -73,6 +74,7 @@
|
|||||||
"@angular/cli": "^20.3.9",
|
"@angular/cli": "^20.3.9",
|
||||||
"@angular/compiler-cli": "^20.3.10",
|
"@angular/compiler-cli": "^20.3.10",
|
||||||
"@capacitor/cli": "latest",
|
"@capacitor/cli": "latest",
|
||||||
|
"@types/jasmine": "~5.1.0",
|
||||||
"cross-env": "^10.1.0",
|
"cross-env": "^10.1.0",
|
||||||
"electron": "^39.0.0",
|
"electron": "^39.0.0",
|
||||||
"electron-builder": "^26.0.12",
|
"electron-builder": "^26.0.12",
|
||||||
@@ -84,7 +86,6 @@
|
|||||||
"karma-jasmine-html-reporter": "~2.1.0",
|
"karma-jasmine-html-reporter": "~2.1.0",
|
||||||
"ngx-toastr": "^19.1.0",
|
"ngx-toastr": "^19.1.0",
|
||||||
"postcss": "^8.5.3",
|
"postcss": "^8.5.3",
|
||||||
"typescript": "~5.9.3",
|
"typescript": "~5.9.3"
|
||||||
"@types/jasmine": "~5.1.0"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
11
accounting-ng-nuttakit/src/app/config/caching.config.ts
Normal file
11
accounting-ng-nuttakit/src/app/config/caching.config.ts
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
export const CACHEABLE_URLS = {
|
||||||
|
GET: [
|
||||||
|
// Add GET URIs here that you want to cache
|
||||||
|
// e.g., '/api/data'
|
||||||
|
],
|
||||||
|
POST: [
|
||||||
|
'/api/web/accountingSearch'
|
||||||
|
// Add POST URIs here that you want to cache
|
||||||
|
// e.g., '/api/search'
|
||||||
|
]
|
||||||
|
};
|
||||||
@@ -47,31 +47,53 @@ export class MainDashboardContentComponent implements OnInit {
|
|||||||
constructor(private generalService: GeneralService) {}
|
constructor(private generalService: GeneralService) {}
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
this.fetchChartData();
|
let token = localStorage.getItem('access_token')
|
||||||
|
this.OnSearchAct(token, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchChartData(): void {
|
OnSearchAct(value: any, setupFirst: boolean): void {
|
||||||
// NOTE: Using a placeholder endpoint as the actual one was not provided.
|
const uri = '/api/web/accountingSearch';
|
||||||
const uri = '/api/dashboard/summary-last-6-months';
|
let request = {
|
||||||
|
token: value
|
||||||
this.generalService.getRequest(uri).subscribe({
|
}
|
||||||
|
this.generalService.postRequest(uri, request).subscribe({
|
||||||
next: (result: any) => {
|
next: (result: any) => {
|
||||||
if (result.code === '200' && result.data) {
|
if (result.code === '200') {
|
||||||
this.processChartData(result.data);
|
this.generalService.trowApi(result);
|
||||||
} else {
|
console.log(`✅ OTP ส่งไปที่ ${value.email}`);
|
||||||
console.warn('Could not fetch chart data:', result.message_th);
|
|
||||||
// Optionally, display placeholder data or an error message
|
|
||||||
this.setupPlaceholderData();
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
error: (error: any) => {
|
error: (error: any) => {
|
||||||
console.error('Error fetching chart data:', error);
|
|
||||||
// Display placeholder data on error to show the graph structure
|
},
|
||||||
this.setupPlaceholderData();
|
complete: () => {
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fetchChartData(): void {
|
||||||
|
// // NOTE: Using a placeholder endpoint as the actual one was not provided.
|
||||||
|
// const uri = '/api/dashboard/summary-last-6-months';
|
||||||
|
|
||||||
|
// this.generalService.getRequest(uri).subscribe({
|
||||||
|
// next: (result: any) => {
|
||||||
|
// if (result.code === '200' && result.data) {
|
||||||
|
// this.processChartData(result.data);
|
||||||
|
// } else {
|
||||||
|
// console.warn('Could not fetch chart data:', result.message_th);
|
||||||
|
// // Optionally, display placeholder data or an error message
|
||||||
|
// this.setupPlaceholderData();
|
||||||
|
// }
|
||||||
|
// },
|
||||||
|
// error: (error: any) => {
|
||||||
|
// console.error('Error fetching chart data:', error);
|
||||||
|
// // Display placeholder data on error to show the graph structure
|
||||||
|
// this.setupPlaceholderData();
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
|
||||||
processChartData(data: any[]): void {
|
processChartData(data: any[]): void {
|
||||||
const labels = data.map(item => item.month);
|
const labels = data.map(item => item.month);
|
||||||
const revenues = data.map(item => item.revenue);
|
const revenues = data.map(item => item.revenue);
|
||||||
|
|||||||
@@ -1,12 +1,30 @@
|
|||||||
import { inject } from '@angular/core';
|
import { inject } from '@angular/core';
|
||||||
import { CanActivateFn, Router } from '@angular/router';
|
import { CanActivateFn, Router } from '@angular/router';
|
||||||
|
import { jwtDecode } from 'jwt-decode';
|
||||||
|
|
||||||
export const authGuard: CanActivateFn = (route, state) => {
|
export const authGuard: CanActivateFn = (route, state) => {
|
||||||
const router = inject(Router);
|
const router = inject(Router);
|
||||||
const accessToken = localStorage.getItem('access_token');
|
const accessToken = localStorage.getItem('access_token');
|
||||||
|
|
||||||
if (accessToken) {
|
if (accessToken) {
|
||||||
|
try {
|
||||||
|
const decodedToken: any = jwtDecode(accessToken);
|
||||||
|
const currentTime = Date.now() / 1000;
|
||||||
|
|
||||||
|
if (decodedToken.exp < currentTime) {
|
||||||
|
// Token expired
|
||||||
|
localStorage.removeItem('access_token');
|
||||||
|
router.navigate(['/login']);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
// Error decoding token
|
||||||
|
localStorage.removeItem('access_token');
|
||||||
|
router.navigate(['/login']);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
router.navigate(['/login']);
|
router.navigate(['/login']);
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import {
|
||||||
|
HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse
|
||||||
|
} from '@angular/common/http';
|
||||||
|
import { Observable, of } from 'rxjs';
|
||||||
|
import { tap } from 'rxjs/operators';
|
||||||
|
import { CachingService } from './caching.service';
|
||||||
|
import { CACHEABLE_URLS } from '../config/caching.config';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class CachingInterceptor implements HttpInterceptor {
|
||||||
|
|
||||||
|
constructor(private cache: CachingService) {}
|
||||||
|
|
||||||
|
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
||||||
|
if (!this.isCacheable(req)) {
|
||||||
|
return next.handle(req);
|
||||||
|
}
|
||||||
|
|
||||||
|
const cachedResponse = this.cache.get(this.getCacheKey(req));
|
||||||
|
if (cachedResponse) {
|
||||||
|
return of(cachedResponse.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
return next.handle(req).pipe(
|
||||||
|
tap(event => {
|
||||||
|
if (event instanceof HttpResponse) {
|
||||||
|
this.cache.put(this.getCacheKey(req), event.clone());
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private isCacheable(req: HttpRequest<any>): boolean {
|
||||||
|
if (req.method === 'GET') {
|
||||||
|
return CACHEABLE_URLS.GET.some(url => req.urlWithParams.includes(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.method === 'POST') {
|
||||||
|
return CACHEABLE_URLS.POST.some(url => req.urlWithParams.includes(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private getCacheKey(req: HttpRequest<any>): string {
|
||||||
|
if (req.method === 'POST') {
|
||||||
|
return req.urlWithParams + JSON.stringify(req.body);
|
||||||
|
}
|
||||||
|
return req.urlWithParams;
|
||||||
|
}
|
||||||
|
}
|
||||||
41
accounting-ng-nuttakit/src/app/services/caching.service.ts
Normal file
41
accounting-ng-nuttakit/src/app/services/caching.service.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import { HttpResponse } from '@angular/common/http';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root'
|
||||||
|
})
|
||||||
|
export class CachingService {
|
||||||
|
private cache = new Map<string, [Date, HttpResponse<any>]>();
|
||||||
|
private cacheDurationInMs = 600000; // 5 minutes
|
||||||
|
|
||||||
|
constructor() { }
|
||||||
|
|
||||||
|
get(key: string): HttpResponse<any> | null {
|
||||||
|
const tuple = this.cache.get(key);
|
||||||
|
if (!tuple) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const expires = tuple[0];
|
||||||
|
const httpResponse = tuple[1];
|
||||||
|
|
||||||
|
// Don't observe expired keys
|
||||||
|
const now = new Date();
|
||||||
|
if (expires && expires.getTime() < now.getTime()) {
|
||||||
|
this.cache.delete(key);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return httpResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
put(key: string, value: HttpResponse<any>): void {
|
||||||
|
const expires = new Date();
|
||||||
|
expires.setMilliseconds(expires.getMilliseconds() + this.cacheDurationInMs);
|
||||||
|
this.cache.set(key, [expires, value]);
|
||||||
|
}
|
||||||
|
|
||||||
|
clear(): void {
|
||||||
|
this.cache.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user